0

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

Anthony
  • 11
  • 2
    Please go back to your books, tutorials or class-notes (or [get a couple of new good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282)). Because you *are* redefining the class itself in the source file. – Some programmer dude Feb 07 '19 at 09:53
  • In `S2D.cpp`, you should not have a `class ShapeTwoD { ... };`. You should just have the definition of the member functions, not enclosed in any class, otherwise the compiler interprets that you are trying to define `ShapeTwoD` for a second time. Have a look at some examples of header and definition files for classes in C++. About the follow up question, yes, you can do that in C++, again have a look at examples of subclassing and calling parent class constructor. – jdehesa Feb 07 '19 at 09:56
  • 5
    *"use of namespace std; it is required in my assignment."* Seriously, `using namespace std;` is required (in header) ? Is it not just usage of std classes as `std::string` instead of rewriting your own ? – Jarod42 Feb 07 '19 at 09:59
  • You have a bug here `containsWarpSpace = containsWarpSpace;` should be `containsWarpSpace = ContainsWarpSpace;` (case is different). Better still you should use *initializer lists* which would avoid this kind of bug. – john Feb 07 '19 at 10:06
  • Really thank you all for your constructive insight! I understand my mistakes now. And I'll improve! starting with namespace std :) – Anthony Feb 07 '19 at 10:06
  • *"... use of namespace std; it is required in my assignment."* - find the most constructive way you can to tell your instructors they are either misinformed or naive. And if they argue with you, you can probably add "in denial" to that list of afflictions. – WhozCraig Feb 07 '19 at 10:16
  • @Anthony you may take a loot at this too: [Why is using namespace std considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Moia Feb 07 '19 at 10:21

1 Answers1

2

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
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
May
  • 101
  • 7