Connecting Tech Pros Worldwide Forums | Help | Site Map

Passing Reference to Base through Constructor

MarkoKlacar's Avatar
Expert
 
Join Date: Aug 2007
Location: Stockholm, Sweden
Posts: 294
#1: Jan 14 '08
Hi,

I've got the following problem.

I have a class, Action_base, that is the base class of a template called Action.
Now in another class called Performer I have a constructor that looks like this:
Expand|Select|Wrap|Line Numbers
  1. Performer::Performer(int weight,int age,Action_base* action);
When creating a new Performer I want to pass the Action class (since its base class is Action_base) to the constructor, but the problem is that is says I have an undefined reference and does not seem to treat Action as a sub-class of Action_base.

Something like this:
Expand|Select|Wrap|Line Numbers
  1. Performer* p = new Performer(90,190,new Action<Whatever>("jump"));
Any help is appreciated.
Any ideas?

Thanks in advance...

Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#2: Jan 14 '08

re: Passing Reference to Base through Constructor


What's the order of your includes(I believe you are using different header and implementation files for base template class,performer and Action)?

Are you sure compiler directs error to that line,perhaps error is somewhere else?
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,370
#3: Jan 14 '08

re: Passing Reference to Base through Constructor


It has to be as Savage says becuse this compiles and links:
Expand|Select|Wrap|Line Numbers
  1. class Whatever
  2. {
  3.  
  4. };
  5. class Action_base
  6. {
  7.  
  8. };
  9. template <class T> 
  10. class Action : public Action_base
  11. {
  12.    public:
  13.        Action(char*);
  14.  
  15. };
  16. template <class T>
  17. Action<T>::Action(char* arg)
  18. {
  19.  
  20. }
  21. class Performer
  22. {
  23.     public:
  24.         Performer(int arg1, int arg2, Action_base* arg3);
  25.  
  26. };
  27. Performer::Performer(int arg1, int arg2, Action_base* arg3)
  28. {
  29.  
  30. }
  31. int main()
  32. {
  33.    Performer* p =  new Performer(90,190,new Action<Whatever>("jump"));
  34. }
  35.  
MarkoKlacar's Avatar
Expert
 
Join Date: Aug 2007
Location: Stockholm, Sweden
Posts: 294
#4: Jan 14 '08

re: Passing Reference to Base through Constructor


Hi guys,

I really appreciate your help.
I'll give it a shot and let you know how it went.

Thanks!


Quote:

Originally Posted by weaknessforcats

It has to be as Savage says becuse this compiles and links:

Expand|Select|Wrap|Line Numbers
  1. class Whatever
  2. {
  3.  
  4. };
  5. class Action_base
  6. {
  7.  
  8. };
  9. template <class T> 
  10. class Action : public Action_base
  11. {
  12.    public:
  13.        Action(char*);
  14.  
  15. };
  16. template <class T>
  17. Action<T>::Action(char* arg)
  18. {
  19.  
  20. }
  21. class Performer
  22. {
  23.     public:
  24.         Performer(int arg1, int arg2, Action_base* arg3);
  25.  
  26. };
  27. Performer::Performer(int arg1, int arg2, Action_base* arg3)
  28. {
  29.  
  30. }
  31. int main()
  32. {
  33.    Performer* p =  new Performer(90,190,new Action<Whatever>("jump"));
  34. }
  35.  

Reply