如果这看起来有点天真,请原谅我,但我是 C++ 的新手,在使用 C 和 Java 多年后,我想我的头脑有点困惑。
我正在尝试制作一个大小未知的数组,其中包含我创建的节点。
node *aNode = new node(14,32);
std::list<node> dataSet;
std::list<node>::iterator it;
it = dataSet.begin();
dataSet.insert(it, aNode)
但是,当我编译这个(概念验证测试)时,它拒绝并抛出各种错误。
我知道这很简单,但我就是想不通。 谁能帮忙?提前致谢!
编辑: 这是节点:
class node{
float startPoint;
float endPoint;
float value;
public:
node(float, float);
void setValues(float, float);
};
node::node(float start, float end){
startPoint = start;
endPoint = end;
}
和编译错误:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2371: 'it' : redefinition; different basic types
error C2440: 'initializing' : cannot convert from 'std::list<_Ty>::_Iterator<_Secure_validation>' to 'int'
error C2146: syntax error : missing ';' before identifier 'dataSet'
error C2143: syntax error : missing ';' before '.'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2371: 'dataSet' : redefinition; different basic types
更新: 我将一小段代码更改为:
node aNode(14, 32);
std::list<node> dataSet;
dataSet.insert(dataSet.begin(), aNode);
但是这 3 个错误仍然存在:
error C2143: syntax error : missing ';' before '.'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2371: 'dataSet' : redefinition; different basic types
最佳答案
您的列表应该是 std::list<node*>
类型或者您应该插入一个节点对象(而不是指向一个节点的指针)。
node *aNode = new node(14, 32);
std::list<node*> dataSet;
dataSet.insert(dataSet.begin(), aNode);
或
node aNode(14, 32);
std::list<node> dataSet;
dataSet.insert(dataSet.begin(), aNode);
关于c++ - 在 C++ 中创建一个列表来保存对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17999472/