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.
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
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:
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
You could create a list of Object like List<Object> list = new ArrayList<Object>() . As all classes implementation extends implicit or explicit from java. lang. Object class, this list can hold any object, including instances of Employee , Integer , String etc.
How to Add Items to a C# List? To add items and elements in a C# list, you need to use the Add() method. If you want to add elements of the custom classes, then you can do so by using the collection-initializer syntax.
To use the data and access functions defined in the class, you need to create objects. Syntax: ClassName ObjectName[number of objects]; The Array of Objects stores objects.
Reference variables hold the objects/values of reference types in Java. 3. Reference variable can also store null value. By default, if no object is passed to a reference variable then it will store a null value.
Your list should either be of type std::list<node*>
or you should insert a node object (and not a pointer to one).
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);
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);
aNode is pointer to a node object on the heap.
dataSet should be defined as:
std::list<node*> dataSet;
Same with your iterator:
std::list<node*>::iterator it;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With