Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a list to hold objects in C++

Tags:

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
like image 368
DragonVet Avatar asked Aug 01 '13 16:08

DragonVet


People also ask

How do you create a list of objects?

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.

Can I create a list of objects in c#?

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.

How do I create a list of objects in CPP?

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.

What do we call a variable which can hold objects of different types?

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.


3 Answers

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);
like image 126
PoByBolek Avatar answered Nov 06 '22 04:11

PoByBolek


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);
like image 26
Peter Bloomfield Avatar answered Nov 06 '22 03:11

Peter Bloomfield


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;
like image 20
AngelCastillo Avatar answered Nov 06 '22 03:11

AngelCastillo