Anthony
Anthony

Reputation: 11

C++ Redefinition of abstract base class

Implementing abstract base class with constructors that derived classes can build on. I have trouble compiling the codes, I apologise in advance for the use of namespace std; it is required in my assignment.

I tried headerguards and checking through my include codes. I separate the files to a main file(Assn2), abstract base class(S2D) .h and .cpp files.

Within main file Assn2

#include <iostream>
#include <string>
#include <fstream>
#include "S2D.h"

Within S2D.h

#ifndef _S2D_H_
#define _S2D_H_
#include <iostream>
#include <string>
using namespace std;

class ShapeTwoD {

    private:
        string name;
        bool containsWarpSpace;

    public: 
        ShapeTwoD();
        ShapeTwoD(string, bool);

Within S2D.cpp

#include <iostream>
#include <string>
#include "S2D.h"

using namespace std;

class ShapeTwoD {
    ShapeTwoD::ShapeTwoD() {
        name = " ";
        containsWarpSpace = false;
    }

    ShapeTwoD::ShapeTwoD(string Name, bool ContainsWarpSpace) {
        name = Name;
        containsWarpSpace = containsWarpSpace;
    }

};

This is the error I received.

S2D.cpp:7:7: error: redefinition of ‘class ShapeTwoD’
 class ShapeTwoD {
       ^~~~~~~~~

In file included from S2D.cpp:3:
S2D.h:7:7: note: previous definition of ‘class ShapeTwoD’
 class ShapeTwoD {
       ^~~~~~~~~
make: *** [S2D.o] Error 1

Just a follow up question, I am trying to implement derived classes based on this abstract base class. I am trying to create derived classes constructors that have more arguments based on these abstract constructors.

For eg.

Rectangle::Rectangle(string Name, bool ContainsWarpSpace, int YSize, int XSize, int(*ArrY), int (*ArrX) )

I wonder this concept which I was taught in Java will work in C++?

Upvotes: 0

Views: 291

Answers (1)

May
May

Reputation: 121

Within S2D.cpp

#include <iostream>
#include <string>
#include "S2D.h"

using namespace std;
class ShapeTwoD { // delete this line
   ShapeTwoD::ShapeTwoD() {
        name = " ";
        containsWarpSpace = false;
    }

    ShapeTwoD::ShapeTwoD(string Name, bool ContainsWarpSpace) {
        name = Name;
        containsWarpSpace = containsWarpSpace;
    }
}; // delete this line

Upvotes: 2

Related Questions