CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2009
    Posts
    2

    Inheritance Problem

    I have not used C++ in about a year and I was trying to get my project to use Inheritance. I have already created my header file that looks something like:

    class Instructions // start class Instructions
    {
    public:
    Instructions();
    Instructions(const char*);
    Instructions(Instructions*);
    ~Instructions();
    const Instructions & operator = (const Instructions&);
    friend ostream & operator << (ostream&, const Instructions&);
    Instructions(vector<string> sendingVect);

    virtual int foobar();
    };

    class LDI: public Instructions
    {
    protected:
    double healthValue;

    public:
    LDI(LDI*);
    LDI(int value);
    };

    class LD: public Instructions
    {
    protected:
    double capacity;

    public:
    LD();
    LD(LD*);
    };

    I also have Instructions.cpp file that has the correct Constructors for each class:

    Instructions::Instructions()
    {
    return;
    }

    LDI::LDI(int valueTemp)
    : Instructions()
    {
    value = valueTemp;
    }


    LD::LD()
    : Instructions()
    {
    //capacity = sentCapacity;
    }

    So when I create an instance in main.cpp of Instruction nothing happens it works fine, but when I try to do:

    LDI *test = new LDI();

    inside main it gives me an error saying that LDI is not a type. I have also tried doing Instructions *test = new LDI(); I still get the same error. Does anyone know a good solution to this error?? Thanks for the help!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Inheritance Problem

    I notice that LDI does not have a default constructor.

    Please post your well indented code in [code][/code] bbcode tags. You should also post the exact error message.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jan 2003
    Posts
    615

    Re: Inheritance Problem

    The code you provided should not compile, the variable 'value' is not defined.

    Code:
    LDI::LDI(int valueTemp)
    : Instructions()
    {
      value = valueTemp;
    }
    Please provide a proper example that demonstrates your problem using code tags.
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  4. #4
    Join Date
    Nov 2009
    Location
    India
    Posts
    10

    Re: Inheritance Problem

    LDI *test = new LDI();
    This will give linker error because LDI is inheritted from Instruction which has a virtual function foobar() only declared. You must define this function either in Instruction or LDI. It will work then.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Inheritance Problem

    Quote Originally Posted by cush2187 View Post
    Code:
    class Instructions
    {
    public:
    Instructions();
    Instructions(const char*);
    Instructions(Instructions*);
    //...
    };
    That gives me shivers. A pointer to Instructions is implicitly castable to an instance of Instructions.

    Did you copy-paste this code snippet from your actual code or did you just type it in manually? Please don't do the latter, cause it's a waste of time to have to correct typos.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured