DragonVet
DragonVet

Reputation: 409

Creating a list to hold objects in C++

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

Upvotes: 7

Views: 63725

Answers (5)

aamir
aamir

Reputation: 151

"missing type specifier - int assumed" can be due to missing

#include <list>

or a missing

#include "header for node"

Upvotes: 0

AngelCastillo
AngelCastillo

Reputation: 2435

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;

Upvotes: 1

donsiuch
donsiuch

Reputation: 493

For the code you posted your missing a semicolon after the last parenthesis

try

dataSet.insert(it, aNode);

Upvotes: 0

Peter Bloomfield
Peter Bloomfield

Reputation: 5766

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);

Upvotes: 1

PoByBolek
PoByBolek

Reputation: 3915

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);

Upvotes: 8

Related Questions