John John - 1 year ago 77
C++ Question

no match for 'operator[]' error for some reason

I have this object with a vectors of pointers. I am trying to access a pointer but I just can't for some reason

myNode.cpp:83: error: no match for 'operator[]' in '((MyProjectGraph::MyNode*)this)->MyProjectGraph::MyNode::out[i]'

myNode.cpp:88: error: no match for 'operator[]' in '((MyProjectGraph::MyNode*)this)->MyProjectGraph::MyNode::in[i]'

/** Bannana
* bannana
void MyNode::toString( ){
cout<<"Name: "<<ltr<<"\nValue: "<<value<<"\nTime: "<<time<<endl;

cout<<"\n Outgoing To: "<<endl;
for(int i = 0; i < out.size(); i++){
cout<<(out[i]->in->ltr)<<endl; //L 85

cout<<" Incoming To: "<<endl;
for(int i = 0; i < in.size(); i++){
cout<<((in[i]->out)->ltr)<<endl; // L 88

Node class fields

std::list<MyEdge*> in;
std::list<MyEdge*> out;

Edge class fields

MyNode* in;
MyNode* out;

Answer Source

std::list doesn't support the array indexing operation. You can iterate over the items of a std::list using std::list::iterator or a range-for statement.

For pre-C++11, use:

std::list<MyEdge*>::iterator iter = out.begin();
std::list<MyEdge*>::iterator end = out.end();
for ( ; iter != end; ++iter )
   cout << (*iter)->in->ltr << endl;

For C++11 or later, use:

for ( autu& item : out )
   cout << item->in->ltr << endl;

Make similar changes to iterate over the items of in.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download