I am currently playing around with inheritance and polymorphism. I programmed a RLC-Circuit and i want one abstract class, and the three elements (Coil, Capacitor and Resistor) are supposed to inherit the Current I and Voltage V from it. The value of the element (L,F & R) have to be in the class again i think.
But my IDE complains about redefinitions and similar issues which I somehow don't find.. Would really appreciate help.
My abstract Class:
class cElement
{
public:
double dElement_u {0};
double dElement_i {0};
- cElement had class type redefinition
- base class undefined
- set_Element_Voltage is not a member of 'Resistor'
19 Errors in Total most of the because of set/get
What you posted compiles cleanly for me.
Obviously you have not posted what is causing your errors.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
It compiles fine for me too. The thing I'll point out is you only need to declare private, protected or public once.
1 2 3 4 5 6 7 8
class Foo{
// by default classes are private
// everything here is private
public: // private ends and everything is public
// everything below here is public
protected: // protected from here on
};
Do you have headers where you are defining the class? That is the first thing that comes to mind for class type redefinition is including headers that don't have either #pragma once or header guards in them. Would probably be quicker if you just show the entire code then people can help with the code and give advice on coding style.
For example, let's say you have a header, CBase.h, which you then include in CExt.h, which you then include in main.cpp you get the above error.
CBase.h
1 2
class CBase{
};
CExt.h
1 2 3
#include "CBase.h"
class CExt : public CBase{
};
Main.cpp
1 2 3 4 5 6 7 8
#include "CBase.h"
#include "CExt.h" // just include both headers to be on the safe side
int main()
{
//...code....
return 0;
}
Now with #includes the preprocessor reads everything in that file into your project. So to the compiler the main.cpp file looks like so:
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12
class CBase{ // because of cpp's #include CBase.h
};
class CBase{ // because of the cpp #include CExt includes CBase
};
class CExt{ // because of CExt.h
};
int main()
{
// ... code...
return 0;
}