0

Below are by 4 classes, I'm learning about basic c++ syntax and boy is it much harder and less forgiving than other languages I have used. I have a main class, base class "BaseArray" and two sub classes "OrderedArray" and "UnorderedArray".

Main.cpp

#include <iostream>
#include "OrderedArray.cpp"
#include "UnorderedArray.cpp"

using namespace std;

int main() {


    system("PAUSE");
    return 0;
}

BaseArray.cpp

#include <iostream>

using namespace std;

class BaseArray {
    public:
        BaseArray::BaseArray() {

        }
};

OrderedArray.cpp

#include <iostream>
#include "BaseArray.cpp"

using namespace std;

class OrderedArray : public BaseArray {
    OrderedArray::OrderedArray() {

    }
};

UnorderedArray.cpp

#include <iostream>
#include "BaseArray.cpp"

using namespace std;

class UnorderedArray : public BaseArray {
    UnorderedArray::UnorderedArray() {

    }
};

The errors I receive are as followed, from scouting other threads online. I think it might have to do with duplicate calling of classes. To be honest, I have no clue. If someone could point me in the right direction that would be nice, thanks in advance!

error C2011: 'BaseArray':'class' type redefinition
error C2504: 'BaseArray':base class undefined

To fix this error I can remove one of the includes at the top of main.cpp, but I need those to create objects and call functions from the subclasses later on.

  • 2
    You need to use include guards. – drescherjm Oct 12 '16 at 19:02
  • 6
    One should almost never include a cpp file. You need to move your code into header files or split the interface from the implementation. – NathanOliver Oct 12 '16 at 19:02
  • [This](http://stackoverflow.com/a/36983180/4342498) basically answers the question but I would not call it a dupe – NathanOliver Oct 12 '16 at 19:08
  • Interesting, I quickly read up on header files. Now my question is how do I show inheritance syntax in header files? Rather, what is the proper way to have this same setup but include header files? – Jon Perron Oct 12 '16 at 19:10
  • The key is to: ***split the interface from the implementation*** The interface goes in the header files with the implementation in the source files. – drescherjm Oct 12 '16 at 21:06

1 Answers1

1

You should put your base array in a header:

BaseArray.h

#ifndef BASEARRAY_H_GUARD       // include guard
#define BASEARRAY_H_GUARD       // include guard  

                                // no using namespace !! 
                                // only includes needed for what's in the header
class BaseArray {
    public:
        BaseArray(); 
};

#endif                      // include guard

And then leave in the cpp only the implementation part of your class:

BaseArray.cpp

#include <iostream>
#include "BaseArray.h"

using namespace std;

BaseArray::BaseArray() {     // no need class enclosing: the BaseArray:: prefix is sufficient
}

The you can apply the same principle to the derived classes:

OrderedArray.h

#ifndef BASEARRAY_H_GUARD       // include guard
#define BASEARRAY_H_GUARD       // include guard  

#include "BaseArray.h"         // include only those that you need but all those that you need 

class OrderedArray : public BaseArray {   // requires BaseArray 
    OrderedArray(); 
};

#endif

OrderedArray.cpp

#include <iostream>         // include headers that are needed for class implementation   
#include "OrderedArray.h"   // this should be self contained and provide
                            // evertyhing that is needed for the class itself

using namespace std;

OrderedArray::OrderedArray() {
}

You then have to do the same for UnorderedArray and finally, you have to adapt your main.cpp to include .h instead of .cpp. And you're done.

A final remark: your cpp source code files are now ready for separate compilation. This means that you can no longer compile only main.cpp, hoping that it includes all the code: you have now to compile the 4 cpp files and link them together.

Christophe
  • 68,716
  • 7
  • 72
  • 138