943,563 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 353
  • C++ RSS
Dec 4th, 2008
0

Inheritance, and code not running

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. void main()
  2. {
  3. sLink* pChainLink; //sLink stays for single list link
  4. sLink ChainLinkNo1 (pChainLink, 1);
  5. sLink ChainLinkNo2 (ChainLinkNo1, 2);
  6. sLink ChainLinkNo3 (ChainLinkNo2, 3);
  7.  
  8. sLink * tmp = ChainLinkNo3;
  9. while(tmp != 0){
  10. cout << tmp->Id();
  11. tmp = ChainLinkNo3.Next();
  12. }
  13.  
  14. }

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)
  1. link.h //the basic node
  2. class Link
  3. {
  4. public:
  5. Link (int id): _id (id) {}
  6. int Id () const { return _id; }
  7. private:
  8. int _id;
  9. };

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.
Similar Threads
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008
Dec 4th, 2008
0

Re: Inheritance, and code not running

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.
C++ Syntax (Toggle Plain Text)
  1. void main() // main should return int, not void
  2. {
  3. sLink* pChainLink; // uninitialised pointer
  4. sLink ChainLinkNo1 (pChainLink, 1); // undefined behaviour: accessing value of uninitialised pointer
  5. sLink ChainLinkNo2 (ChainLinkNo1, 2); // compiler error: ChainLinkNo1 not a pointer, sLink's constructor expects one
  6. sLink ChainLinkNo3 (ChainLinkNo2, 3); // compiler error: ChainLinkNo2 not a pointer, sLink's constructor expects one
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.
Last edited by grumpier; Dec 4th, 2008 at 11:36 pm.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Dec 5th, 2008
0

Re: Inheritance, and code not running

pretty good answer GOT IT ! thx
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help solving errors: C2533, C2511, C2144.
Next Thread in C++ Forum Timeline: A Puzzling Address Book...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC