在 C++ 中创建一个列表来保存对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17999472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a list to hold objects in C++
提问by DragonVet
Forgive me if this seems a bit naive, but I'm rather new to C++ and after years in C and in Java, I guess my head's a little confused.
如果这看起来有点天真,请原谅我,但我对 C++ 还很陌生,并且在使用 C 和 Java 多年之后,我想我的头有点困惑。
I'm trying to make an array of an unknown size full of nodes that I've created.
我正在尝试制作一个大小未知的数组,其中包含我创建的节点。
node *aNode = new node(14,32);
std::list<node> dataSet;
std::list<node>::iterator it;
it = dataSet.begin();
dataSet.insert(it, aNode)
However, when I compile this (proof of concept test), it refuses, throwing all sorts of errors.
但是,当我编译这个(概念验证测试)时,它拒绝了,抛出了各种错误。
I know it's something simple and I just can't figure it out. Can anyone help? Thanks in advance!
我知道这很简单,我就是想不通。任何人都可以帮忙吗?提前致谢!
edit: Here's node:
编辑:这是节点:
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;
}
and compiler errors:
和编译器错误:
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
错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int
错误 C2371:“它”:重新定义;不同的基本类型
错误 C2440:“正在初始化”:无法从“std::list<_Ty>::_Iterator<_Secure_validation>”转换为“int”
错误 C2146:语法错误:缺少“;” 在标识符 'dataSet' 之前
错误 C2143:语法错误:缺少“;” 前 '。'
错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int
错误 C2371:“数据集”:重新定义;不同的基本类型
update: I changed the little bit of code to:
更新:我将一点代码更改为:
node aNode(14, 32);
std::list<node> dataSet;
dataSet.insert(dataSet.begin(), aNode);
But these 3 errors remain:
但是这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
采纳答案by PoByBolek
Your list should either be of type std::list<node*>
or you should insert a node object (and not a pointer to one).
您的列表应该是类型,std::list<node*>
或者您应该插入一个节点对象(而不是指向一个的指针)。
node *aNode = new node(14, 32);
std::list<node*> dataSet;
dataSet.insert(dataSet.begin(), aNode);
or
或者
node aNode(14, 32);
std::list<node> dataSet;
dataSet.insert(dataSet.begin(), aNode);
回答by Peter Bloomfield
Looks like you need to declare your list to contain node pointers, i.e.:
看起来您需要声明您的列表以包含节点指针,即:
std::list<node*> dataSet
std::list<node*>::iterator it;
Also worth noting that you can add items to a list without using an iterator:
另外值得注意的是,您可以在不使用迭代器的情况下将项目添加到列表中:
dataSet.push_back(aNode);
回答by AngelCastillo
aNode is pointer to a node object on the heap.
aNode 是指向堆上节点对象的指针。
dataSet should be defined as:
数据集应定义为:
std::list<node*> dataSet;
Same with your iterator:
与您的迭代器相同:
std::list<node*>::iterator it;
回答by aamir
"missing type specifier - int assumed" can be due to missing
“缺少类型说明符 - 假设为 int”可能是由于缺少
#include <list>
#include <list>
or a missing
或失踪
#include "header for node"
#include "header for node"
回答by donsiuch
For the code you posted your missing a semicolon after the last parenthesis
对于您在最后一个括号后发布的缺少分号的代码
try
尝试
dataSet.insert(it, aNode);