| | |
Inheritance, and code not running
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 21
Reputation:
Solved Threads: 1
Ah right! Im on my first steps on c++, What Im trying to do is
have this "basic node" class wich just take an id and from there
usign inheritance make "linked list node" and "binary tree node" classes my code so far looks like this
and the other 2 headers
the compiler error says:
(red code position ) 'initializing' : cannot convert from 'Link *' to 'sLink *'
Some reason why's ... thanks
have this "basic node" class wich just take an id and from there
usign inheritance make "linked list node" and "binary tree node" classes my code so far looks like this
C++ Syntax (Toggle Plain Text)
void main() { sLink* pChainLink; //sLink stays for single list link sLink ChainLinkNo1 (pChainLink, 1); sLink ChainLinkNo2 (ChainLinkNo1, 2); sLink ChainLinkNo3 (ChainLinkNo2, 3); sLink * tmp = ChainLinkNo3; while(tmp != 0){ cout << tmp->Id(); tmp = ChainLinkNo3.Next(); } }
and the other 2 headers
//sLink.h
class sLink : public Link
{
public:
sLink (sLink* pNext, int id): Link(id), _pNext (pNext) {}
sLink * Next () const { return _pNext; }
private:
sLink * _pNext;
}; C++ Syntax (Toggle Plain Text)
link.h //the basic node class Link { public: Link (int id): _id (id) {} int Id () const { return _id; } private: int _id; };
the compiler error says:
(red code position ) 'initializing' : cannot convert from 'Link *' to 'sLink *'
Some reason why's ... thanks
Last edited by namehere05; Dec 4th, 2008 at 11:12 pm.
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
Your problem is actually the first four lines of the main() function. I've added comments to the code that are related to problems on each line.
The only reason the compiler is complaining about the line you have highlighted is that it is the only constructor that might be invoked by your code, but you've provided the wrong type of argument.
C++ Syntax (Toggle Plain Text)
void main() // main should return int, not void { sLink* pChainLink; // uninitialised pointer sLink ChainLinkNo1 (pChainLink, 1); // undefined behaviour: accessing value of uninitialised pointer sLink ChainLinkNo2 (ChainLinkNo1, 2); // compiler error: ChainLinkNo1 not a pointer, sLink's constructor expects one sLink ChainLinkNo3 (ChainLinkNo2, 3); // compiler error: ChainLinkNo2 not a pointer, sLink's constructor expects one
Last edited by grumpier; Dec 4th, 2008 at 11:36 pm.
![]() |
Similar Threads
- Need manpower to create game program (Game Development)
- Inheritance vs New Method (Java)
- Opinions? javascript/php/etc and programming standards (JavaScript / DHTML / AJAX)
- Another Java Noob :( (Java)
- overloading Operator << to accept endl (C)
Other Threads in the C++ Forum
- Previous Thread: Need help solving errors: C2533, C2511, C2144.
- Next Thread: A Puzzling Address Book...
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets





