Inheritance of abstract Class

closed account (D3R4216C)
Hello,

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};

public:

// virtual ~cElement() = 0;

// virtual void info() = 0;

void set_Element_Voltage(double dvoltage);
double get_dElement_Voltage();

void set_Element_Current(double dcurrent);
double get_dElement_Current();
};

The Resistor Class (Coil and Capacitor Class look the same)

class Resistor :
public cElement
{
public:
double dResistor_Value;

public:
Resistor();

void set_Resistor_Value(double dwert);
double get_Resistor_Value();
};

In my source.cpp I am just doing the calculations of the values but i don't get the inheritance right.
Show us the errors
closed account (D3R4216C)
Errors:

- 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;
}

The fix is to either do #pragma once or header safeguards to make sure the code is only defined once and not multiple times.
Some header file
1
2
#pragma once
// ... declarations and class interfaces 

OR
1
2
3
4
5
#ifndef HEADERFILENAME_H
// if you had CBase.h it would be CBASE_H
#define HEADERFILENAME_H
// ... declarations and class interfaces
#endif 

Undefined base class usually is a sign that the classes are declared or included out of order.

It would be easier to help more if you just post your code in the code brackets.
Last edited on
Topic archived. No new replies allowed.