-
November 24th, 2009, 11:43 PM
#1
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!
-
November 25th, 2009, 12:21 AM
#2
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.
-
November 25th, 2009, 01:29 AM
#3
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
-
November 25th, 2009, 03:38 AM
#4
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.
-
November 25th, 2009, 08:34 AM
#5
Re: Inheritance Problem
Originally Posted by cush2187
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|