Big Mike
Big Mike

Reputation: 1

What is the correct way to implement inheritance when dealing with classes in separate .cpp and .h files?

I'm new to C++ and to practice I decided to write a simple console game. I originally wrote it all in the main.cpp file, but now I've decided to split all the classes up to make it simpler to read. I looked up a few tutorials on how to correctly do this but I seem to have run into an error that I haven't been able to find a solution to.

Here is a base class for a "block". Blocks of different textures inherit from this base block.

This is the code for the header file:

#pragma once
#include <iostream>

class BaseBlock { //Base block class
private:
    int PosX, PosY;
protected:
    std::string Appearance;
    bool        IsActive;

public:
    BaseBlock(int x, int y);

    virtual inline int          getX();
    virtual inline int          getY();
    virtual inline bool         getIsActive();
    virtual inline std::string  getAppearance();
    virtual inline void         setActive(bool isactive);

};

Here is its .cpp file:

 #include "clsBaseBlock.h"



    BaseBlock::BaseBlock(int x, int y) {
        PosX        = x;
        PosY        = y;
        Appearance  = ' ';
        IsActive    = false;
    }

    inline int          BaseBlock::getX()                   { return PosX; }
    inline int          BaseBlock::getY()                   { return PosY; }
    inline bool         BaseBlock::getIsActive()            { return IsActive; }
    inline std::string  BaseBlock::getAppearance()          { return Appearance; }
    inline void         BaseBlock::setActive(bool isactive) { IsActive = isactive; }

Here is the code for the header of one of the derived classes:

 #pragma once
#include "clsBaseBlock.h"

class BlockWeak : public BaseBlock { // Third derived block class
public:
    BlockWeak(int x, int y) : BaseBlock(x, y) {} //Here is the inheritance
};

Here is its .cpp file:

  #include "clsBlockWeak.h"

BlockWeak::BlockWeak(int x, int y): BaseBlock(x,y){
        Appearance = "\xB1";
        IsActive = true;
    }

From this code I get all sorts of errors in the header file of the derived classes like:

-syntax error : 'symbol' : expected member function definition to end with '}' -trailing 'char' illegal in base/member initializer list

Then in the .cpp file I get:

-type "int" unexpected -unexpected end of file found

Any information on how to correct my code would be much appreciated.

Upvotes: 0

Views: 125

Answers (1)

Armen Tsirunyan
Armen Tsirunyan

Reputation: 133082

BlockWeak(int x, int y) : BaseBlock(x, y) {} //Here is the inheritance

No, it's not. Here is the inheritance:

class BlockWeak : public BaseBlock

The following is a full definition of a constructor, which you have also provided in the .cpp file thus potentially breaking the One Definition Rule

BlockWeak(int x, int y) : BaseBlock(x, y) {} 

You should probably remove the part starting from :, including the {} and move those into the definition

Upvotes: 1

Related Questions