I have this iterator that I want to format so the output is something like this. < "Index of first node"(first value of iterator)<<"Second index"(second value of iterator). Can anyone help me with this.

void Level::printAstarList(list<int> AstarList)
{
  // cout<<"<";
   
   std::list<int>::const_reverse_iterator it;

  for (it = AstarList.rbegin(); it != AstarList.rend(); ++it) 
  
  
  {

 
  cout<<*it;

  }
  
  cout<<"<";
  
}

Recommended Answers

All 13 Replies

how about a for loop?

for (int i=0; i<it; i++)
{
	cout << "<";
} cout << endl;

try something like this :

char *Index[2] = { "Index of first(", "Index of second(" }; //and so on

int cntr = 0;
while(start != end) //start and end are the beginning and the end iterator of a container
{
    cout << Index[cntr] << *start <<")"<<endl;
    start++;
}

I have these to lists that I want to print out.

list<graphNode> open; // List of open nodes to be explored
list<graphNode> closed; // List of closed nodes which have been explored

I have been trying to use an iterator like this:

void Level::printNodeList(list <graphNode> nodelist)
{
    cout << "<";
    list<grpahNode>::iterator listIter;
    for(listIter = nodelist.begin();listIter != nodelist.end();++listIter)
    {
        cout<<*listIter<<",\n";
    }
        cout<<">\n";
}

but I cannot get it to work any ideas?

When you say you can't get it to work...is the list returning empty?

If so, it may be because you're passing a copy of the list into the parameter. Try passing the list as a reference instead..

void Level::printNodeList(list <graphNode> &nodelist)

no operator found which takes a right-hand operand of type 'graphNode' (or there is no acceptable conversion)

This is the error that I get from my program. Do not understand what I am doing wrong.

Hmm ok, track back a bit and rather than passing of type graphnode, try using int.

This code compiles for me... maybe it'll help you to further diagnose the problem

#include <list>
#include <iostream>
using namespace std;

void printNodeList(list <int> &nodelist)
{
cout << "<";
list<int>::iterator listIter;
for(listIter = nodelist.begin();listIter != nodelist.end();++listIter)
{
cout<<*listIter<<",\n";
}
cout<<">\n";
}

void main()
{
	list<int> x;

	x.push_back(1);
	x.push_back(8);
	x.push_back(5);

	printNodeList(x);

}


list<grpahNode>::iterator listIter;

but I cannot get it to work any ideas?

Oh and I hope you've noticed there's a spelling mistake there..

grpahNode = graphNode?

The problem I have with doing that now though is how I call it in my a star algorithm. I dont know what function the argument takes if it is declared as
list<graphNode> open; // List of open nodes to be explored
list<graphNode> closed; // List of closed nodes which have been explored

You mean if passing by reference?

list<graphNode> &open; // List of open nodes to be explored
list<graphNode> &closed; // List of closed nodes which have been explored

I try to call it like this

print(&closed);

what is wrong with this because it doesn't work?

I try to call it like this

print(&closed);

what is wrong with this because it doesn't work?

When actually calling it you do

print(closed);

You only apply the & in the function signature/prototype.

When I change it to

print(closed);

it gives these errors do you know why?

1>f:\pacmancw\level.cpp(451) : error C2530: 'open' : references must be initialized
1>f:\pacmancw\level.cpp(452) : error C2530: 'closed' : references must be initialized
1>f:\pacmancw\level.cpp(607) : error C2664: 'Level::print' : cannot convert parameter 1 from 'std::list<_Ty>' to 'std::list<_Ty> &'

thanks for the help

When I change it to

print(closed);

it gives these errors do you know why?

1>f:\pacmancw\level.cpp(451) : error C2530: 'open' : references must be initialized
1>f:\pacmancw\level.cpp(452) : error C2530: 'closed' : references must be initialized
1>f:\pacmancw\level.cpp(607) : error C2664: 'Level::print' : cannot convert parameter 1 from 'std::list<_Ty>' to 'std::list<_Ty> &'

thanks for the help

Error 1 & 2: Kind of difficult to diagnose without more code.
Error 3:In your function declaration/prototype for 'Level::print' have you included the &? It sounds as though you've got one function which is still passing by value and another passing by reference

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.