简体   繁体   中英

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.

#include <iostream>
#include <string>
#include <fstream>
#include "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);

#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++?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM