Inheritance, and code not running

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

Inheritance, and code not running

 
0
  #1
Dec 4th, 2008
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

  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;
};

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Inheritance, and code not running

 
0
  #2
Dec 4th, 2008
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

Re: Inheritance, and code not running

 
0
  #3
Dec 5th, 2008
pretty good answer GOT IT ! thx
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC