Passing Reference to Base through Constructor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    Passing Reference to Base through Constructor

    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:
    Code:
    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:
    Code:
    Performer* p = new Performer(90,190,new Action<Whatever>("jump"));
    Any help is appreciated.
    Any ideas?

    Thanks in advance...
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    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?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      It has to be as Savage says becuse this compiles and links:
      [code=cpp]
      class Whatever
      {

      };
      class Action_base
      {

      };
      template <class T>
      class Action : public Action_base
      {
      public:
      Action(char*);

      };
      template <class T>
      Action<T>::Acti on(char* arg)
      {

      }
      class Performer
      {
      public:
      Performer(int arg1, int arg2, Action_base* arg3);

      };
      Performer::Perf ormer(int arg1, int arg2, Action_base* arg3)
      {

      }
      int main()
      {
      Performer* p = new Performer(90,19 0,new Action<Whatever >("jump"));
      }
      [/code]

      Comment

      • MarkoKlacar
        Recognized Expert Contributor
        • Aug 2007
        • 296

        #4
        Hi guys,

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

        Thanks!


        Originally posted by weaknessforcats
        It has to be as Savage says becuse this compiles and links:
        [code=cpp]
        class Whatever
        {

        };
        class Action_base
        {

        };
        template <class T>
        class Action : public Action_base
        {
        public:
        Action(char*);

        };
        template <class T>
        Action<T>::Acti on(char* arg)
        {

        }
        class Performer
        {
        public:
        Performer(int arg1, int arg2, Action_base* arg3);

        };
        Performer::Perf ormer(int arg1, int arg2, Action_base* arg3)
        {

        }
        int main()
        {
        Performer* p = new Performer(90,19 0,new Action<Whatever >("jump"));
        }
        [/code]

        Comment

        Working...