c++ - Base operand has non-pointer type..but it is a pointer? -
i'm working on c++ project deals graphs. has lot of functionality related class "node" , class "edge."
at first, edge stored copies of starting node , ending node; like:
class edge{ ... private: node m_start, m_end; ... }
to make graph modification functions more efficient, decided make edge class store pointers start , end nodes instead:
class edge{ ... private: node* m_start, m_end; ... }
i made adjustments in cpp files/related functions ensure proper access (e.g., changing .
s ->
s). 1 example in printing function:
void edge::print(ostream& _os){ _os << "edgeid = " << m_id << endl << "connects nodes: " << m_start->getindex() << " , " << m_end->getindex() << endl; }
but when try compile, message (and similar ones same kinds of changes elsewhere):
models/edge.cpp: in member function ‘virtual void edge::print(std::ostream&) const’: models/edge.cpp:32: error: base operand of ‘->’ has non-pointer type ‘const node’
[where 32 line in example above starts << "connects nodes: "
]
what going on here? m_start pointer, far can tell! might there wrong in assignment somewhere? getindex()
function being called in example looks this, , same error occurs or without const
:
int getindex() const { return m_index; } //in node.h
you've shot in both feet. should be:
node* m_start, * m_end;
next time, pay screen line , write:
node * m_start; node * m_end;
you'll doing favour.
Comments
Post a Comment