[我正在尝试使用向量在c ++中创建2D数组,由于某种原因,在执行此操作时会出现此错误,
expression must have pointer to object type
错误显然在此行上:
std::cout << "item" << i << ": " << toSim[i][j] << std::endl;
当我查找有关2D向量的任何东西时,我总是看到人们使用我用来访问它们的数据的语法,但这给我一个错误,我在定义向量的行中也会遇到此错误,是否定义了它的内容错误地?
no instance of constructor "std::vector<_Ty, _Alloc>::vector [with _Ty=float, _Alloc=std::allocator<float>]" matches the argument list
也,这是完整的代码:
class movmentCalculator
{
private:
std::vector<float> toSim { {5, 5, 5, 5}, {6, 6, 6, 6}, {7, 7, 7, 7} };
public:
void printStack()
{
for (int i = 0; i < toSim.size(); i++)
for (int j = 0; j < toSim.size(); j++)
{
std::cout << "item" << i << ": " << toSim[i][j] << std::endl;
}
}
};
std::vector<float>
是一维向量。因此,您的错误。
要被编译器接受,您的代码可以按以下方式更正:
class movmentCalculator
{
private:
std::vector<std::vector<float>> toSim { {5, 5, 5, 5}, {6, 6, 6, 6}, {7, 7, 7, 7} };
public:
void printStack()
{
for (unsigned int i = 0; i < toSim.size(); ++i)
for (unsigned int j = 0; j < toSim[i].size(); ++j)
{
std::cout << "item" << i << ": " << toSim[i][j] << std::endl;
}
}
};
但是您可以很容易地自己解决(我认为)。