Creating a list to store objects in C ++

Forgive me if this seems a bit naive, but I'm pretty new to C ++ and after several years of working in C and Java, I think my head got a little confused.

I am trying to create an array of unknown size, full of nodes that I 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 conceptual test), it refuses, throwing all kinds of errors.

I know this is something simple, and I just can't figure it out. Can anyone help? Thanks in advance!

edit: Here 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. Note: C ++ does not support default-int

error C2371: 'it': redefinition; various basic types

error C2440: 'initializing': cannot convert from 'std :: list <_Ty> :: _ Iterator <_Secure_validation>' to 'int'

error C2146: syntax error: missing ';' before the identifier 'dataSet'

error C2143: syntax error: missing ';' before '.'

error C4430: missing type specifier - int. Note: C ++ does not support default-int

error C2371: 'dataSet': override; various basic types

update: I changed the code a bit:

  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 
+4
source share
5 answers

Your list must be of type std::list<node*> or you must 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); 
+5
source

It looks like you need to declare that your list contains node pointers, i.e.:

 std::list<node*> dataSet std::list<node*>::iterator it; 

It's also worth noting that you can add items to the list without using an iterator:

 dataSet.push_back(aNode); 
+1
source

aNode is a pointer to a node object on the heap.

dataSet should be defined as:

 std::list<node*> dataSet; 

Same thing with your iterator:

 std::list<node*>::iterator it; 
+1
source

For the code you sent, you lost the semicolon after the last bracket

to try

 dataSet.insert(it, aNode); 
0
source

"missing type specifier - int is assumed" may be due to missing

#include <list>

or missing

#include "header for node"

0
source

Source: https://habr.com/ru/post/1494787/


All Articles