7

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
7
  • 4
    can you post the compiler error? Commented Aug 1, 2013 at 16:24
  • could you please show us the node class? Commented Aug 1, 2013 at 16:25
  • 3
    aren't you trying to insert a node* into a node list? Commented Aug 1, 2013 at 16:26
  • Tiago is right. Declare it as a local variable : node aNode(14, 32); then it should be happy to put it into the list Commented Aug 1, 2013 at 16:28
  • @GrahamGriffiths That didn't help :/ I added the errors and the node class to the main post. Commented Aug 1, 2013 at 16:31

5 Answers 5

8

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);
Sign up to request clarification or add additional context in comments.

2 Comments

This got rid of a bunch of the errors, but 3 still 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
@user1848437 You might post that as an "update" section of your question for everyone to see easily.
1

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

Comments

1

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;

Comments

0

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

try

dataSet.insert(it, aNode);

2 Comments

Since this may just be a copy/paste error, rather than what's actually being compiled by the OP, it's probably best to ask if the missing ; is in the source code or was a copy/paste error via a comment rather than an answer.
@user1848437 Might want to edit in a ; to your question then ;)
0

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

#include <list>

or a missing

#include "header for node"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.