class Node
{
public:
 string name;
  list<string> edges;
};
list<Node> nodes;

Hello, Can you help. I used this class that you gave but i've struggling to add anything.

I tried
nodes.name.push_back(str);

but no use, and also i tried
nodes::name.push_back(str);

i also tried to compare sections by trying
nodes.name == temp;

but no use, Please help.

Recommended Answers

All 11 Replies

Since nodes is of type list of Node you can only add Node into it. For example :

list<Node> nodes;
Node n;
n.name = "test";
nodes.push_back( n );

Thank you!

I sadly still have a issue of comparing the name section of nodes with the file being read in.

n.name = temp;
if(nodes[0] == n)

Is what i have tried, but it says that nodes has no operator that uses that.

That's because you haven't overloaded operator==. You need to define an operator function for the equality operator.

bool Node::operator==(const Node &other) {
  //fill this in....
}

I have never setup a operator before, I will look it up though. Hopefully it will help.

This is a common tutorial regarding the process:
http://www.cplusplus.com/doc/tutorial/classes2/

It doesn't cover operator== specifically, but it should give you the basic idea. The important thing for you to take away from it is the format of the function's definition.

Sorry for being useless, but i'm still struggling to set this up.

class Node
{
public:
	string name;
	list<string> edges;
	list<int> dist;
	Node operator == (Node);
};
bool Node::operator==(Node n) {
 // It errors /\ here 
}

change it from bool to node seem to help...

Yes, your declaration within your class must be changed to a bool return. If the declaration doesn't match the definition, it will lead to problems.

I would also consider changing the argument to a const reference rather than a non-const pass by value, it's "safer" code.

After much rage i decide to take a print screen of my problem. The code isn't my final code.

As i have a list of my "Node" data type i am trying to search through it to know if i already have one that is already currently in there.

Here my attempt and rage!
http://img850.imageshack.us/f/rage.png/

class Node
{
public:
 string name;
  list<string> edges;
  list<int> dist;
};


list<Node> nodes;

Omg, after hours of rage. I found out list's can't be used to access individual element.

This list class, unlike the vector class, is not designed for solutions that require a data structure for efficient individual element access. The ends of the list are accessed in an optimal way, O(1). However, all other access requires O(n) operations or O(lgn) when the list is sorted. In addition, there are no indices for the elements in a list. However, an elements position within the list can be obtained using iterators and the distance(start_itr, current_pos_itr). The following table describes individual element access expressions:

I found out list's can't be used to access individual element.

Well it can, but not as fast as arrays. For example :

std::list<int> numbers;
numbers.push_back(10); numbers.push_back(20);
std::list<int>::const_iterator itr = numbers.begin();
while( itr != numbers.end()){
  cout << *itr << endl;
  ++itr;
}

so using iterators you can access individual element but you will have to transverse all elements before it to get to it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.