Class Type Redefinition C++ - c++

I have seen other people asking this question before, but the answers they received were unique to their programs and unfortunately do not help me.
Firstly, I have a shape class - split into .h and .cpp files
//Shape.h
#include <string>
using namespace std;
class Shape
{
private:
string mColor;
public:
Shape(const string& color); // constructor that sets the color instance value
string getColor() const; // a const member function that returns the obj's color val
virtual double area() const = 0;
virtual string toString() const = 0;
};
//Shape.cpp
#include "Shape.h"
using namespace std;
Shape::Shape(const string& color) : mColor(NULL) {
mColor = color;
}
string Shape::getColor() const
{
return mColor;
}
I keep getting an error in my Shape.h class that says 'Shape' : 'class' type redefinition.
Any idea why I might be getting this error?

add include guard to your header file
#ifndef SHAPE_H
#define SHAPE_H
// put your class declaration here
#endif
And the way you initialize member mColor is incorrect. You can't assign NULL to string type
Shape::Shape(const string& color) : mColor(color) {
}
Add virtual destructor to Shape class as it serves as a base with virtual functions.
Also, do NOT use using directive in header file.

It seems like you want to write an abstract base class here, but is there any other files you compiled but not showed here?
You must include “shape.h” twice more.
Just use macros to prevent this case.
PS:I guess Rectangle is base class of Square,and also inherited Shape.

Related

error C2504: 'Entity': base class undefined

After searching on nearly every page covering this error, I couldn't find a solution that matched my problem. When including the header file for the base class in the file of the derived class, I get the error: "error C2504: 'Entity': base class undefined". I have three classes, a parent class, derived class, and an inbetween class that both of the other classes need access to. I'm making an entity class for my enemies and other entities to inherit from, but even after including its header, I still get the error. I've tried forward declarations for the parent class and they don't seem to do anything to no avail. Before looking at my code, ignore common.h, player.h, skeleton.h, game.h, and resourcemanager.h. Without futher ado, here is my code:
(Entity.h)
#include "Common.h"
#include "Tile.h";
class Entity {
public:
Tile *isSideColliding(bool isSolid, string&& type);
Tile* isTopColliding(bool isSolid, string&& type);
Tile* isBottomColliding(bool isSolid, string&& type);
Sprite sprite;
RectangleShape topHitbox, bottomHitbox;
};
(Slime.h)
#pragma once
#include "Common.h"
#include "Game.h"
#include "Entity.h"
#include "Tile.h"
#include "ResourceManager.h"
class Slime: public Entity {
public:
static vector<Slime> slimeVector;
Slime(float& x, float& y);
static void draw();
static void update();
private:
static const Vector2f SPRITE_DIMENSIONS;
char dir;
//Sprite sprite;
//RectangleShape topHitbox, bottomHitbox;
/*Tile* isSideColliding(bool isSolid, string&& type);
Tile* isTopColliding(bool isSolid, string&& type);
Tile* isBottomColliding(bool isSolid, string&& type);*/
static const float GRAVITY;
static const Vector2f TERMINAL_VELOCITY;
Vector2f position, velocity;
};
(Tile.h)
#pragma once
#include "Common.h"
#include "Player.h"
#include "Skeleton.h"
#include "ResourceManager.h"
#include "Chest.h"
#include "Slime.h"
class Slime;
class Player;
class Skeleton;
class Tile {
public:
Sprite sprite;
static void draw();
static void createLevelPathing();
static void setupBackground();
static vector<Tile> tileVector;
Vector2f spriteDimensions;
bool isSolid;
string type;
private:
Tile(float& x, float& y, string&& type, bool isSolid);
Tile(int& x, int& y, bool isSolid);
static void initTiles(int& levelPosX, int& levelPosY, const Image& image);
static Image getRoomTemplate(int& templateType);
static const float POSITION_SCALAR, SCALE;
static const int START_TILE, DOWN_TILE, UP_TILE, UP_AND_DOWN_TILE, DOOR_TILE;
};
My goal is to inherit from entity to slime. I'm fairly sure my file setup is good though. Could anyone please help explain why I'm getting the error of no base class defined? Also, I would appreciate critisism on my file including and how I could better structure it. Thanks!
As Jerry pointed out in the comment, it is circular include.
This normally implies that something can be improved in the design.
For example, why does the Entity has to care about the colliding logic? Can it instead expose some functions for the Tile module to calculate colliding?
Come back to your question. I suggest to:
In Entity, forward declare the Tile class. Move the #include "tile.h" to the .cpp file. Do the same for Slime.
In Tile, remove the #include and forward declaration for Entity and Slime, it seems they're completely unused there.
Again, I still believe it is better to rethink the flow and the responsibility of classes, which one does what. From what I can remember, circular dependencies will likely bite us later.

Xcode 8.3.1 - Compiler can no longer handle circular references?

I have been developing a C++ game engine for a long time. I have never had any issues with the compiler, or anything like that, until I update to Xcode 8.3.1!
Suddenly, it appears that a default setting was changed when I updated that made it so that the compiler simply cannot handle circular references.
Does anyone know how to set this back, (I tried downgrading Xcode, and it still doesn't work!)
My circular referencing looks something like this:
I have a class called "Object" defined in my code
"Object" includes another class called "Renderer2D"
"Renderer2D" includes another class called "Renderable2D"
"Renderable2D" extends "Object"
My "Object" class:
#pragma once
#include "Graphics/2D/Renderer2D.h"
namespace kineticengine {
class Object {
public:
Object();
virtual ~Object() {}
virtual void render(graphics::Renderer2D* renderer) const;
};
}
My "Renderer2D" class:
#pragma once
#include "Renderable2D.h"
namespace kineticengine {
namespace graphics {
class Renderer2D {
protected:
Renderer2D() {}
public:
virtual void submit(const Renderable2D* renderable) {}; // Error here, "Unknown type name 'Renderable2D', did you mean 'Renderer2D'?"
};
}
}
My "Renderable2D" class:
#pragma once
#include "Renderer2D.h"
#include "../../Object.h"
namespace kineticengine {
namespace graphics {
class Renderable2D : public Object {
public:
Renderable2D() : Object() {}
virtual ~Renderable2D() {}
void render(Renderer2D* renderer) const override {
renderer->submit(this); // Error here "Cannot initialize parameter of type 'const kineticengine::graphics::Renderer2D *' with an rvalue of type 'const kineticengine::graphics::Renderable2D *'"
}
};
}
}
All of my errors are basically variations of "Unknown class [x]" where x is one of the other classes.
Any help would be appreciated!
Renderable2D.h is including Renderer2D.h before defining class Renderable2D, so when Renderer2D.h refers to class Renderable2D, it is not yet defined. Clang is behaving correctly.
One way to break this cycle is to not include headers if you're only going to refer to a class by pointer or reference. You then put a forward declaration for the class in instead of the include directive. This has the added bonus of speeding up compile time as well.

chess game - error in abstract class

#pragma once
#include <string>
#include <iostream>
using namespace std;
class Chess_tool
{
public:
Chess_tool(string color, char name);
virtual bool legal_movement(int source[], int dest[]) const = 0;
private:
string _color;
char _name;
};
Im trying to create chess game, so I create abstract class for chess tool (queen, king, rook...)
I also created king tool to check my code:
#pragma once
#include "Chess_tool.h"
class King : Chess_tool
{
public:
King(string color, char name);
virtual bool legal_movement(int source[], int dest[]);
};
and I create game_board class:
#pragma once
#include "Game_board.h"
#include "Chess_tool.h"
#include <iostream>
#define BOARD_SIZE 8
using namespace std;
class Chess_tool;
class Game_board
{
public:
Game_board();
~Game_board();
void move(string panel);
protected:
Chess_tool* _board[BOARD_SIZE][BOARD_SIZE];
};
the problem is here, when i try to add object to the matrix its show me error :
1 IntelliSense: object of abstract class type "King" is not allowed:
pure virtual function "Chess_tool::legal_movement" has no overrider
#pragma once
#include "Chess_tool.h"
#include "Game_board.h"
#include "King.h"
using namespace std;
enum Turn { WIHTE, BLACK };
class Manager : Game_board
{
public:
Manager();
~Manager();
virtual bool legal_movement(int source[], int dest[]) const = 0;
};
....
#include "Manager.h"
Manager::Manager()
{
_board[0][0] = new King();
}
The member function in the base class is const-qualified, not in the derived class.
So these are not the same functions through inheritance. You've declared a new virtual function, not overriden the first one.
Add const to the second one so that it actually override the base class function.
Remember that for virtual function overriding to kick in, there are a few condition to actually satisfy. They must have:
the same name
the same return type
the same parameters count and type
the same const-qualification (our case here)
a few other minor things (for example, compatible exceptions specifications)
If any condition isn't satisfied, you create a very similar, but different, function for the compiler.
With C++11, you should use override for the functions you want to override, so the compiler knows your intention and tells you that you've made a mistake. E.g.:
virtual bool legal_movement(int source[], int dest[]) override;
// ^^^^^^^^

Issues calling base class function from derived class

I am currently very new to c++, i have started learning how to use pointers in a path finding algorithm.
I am having an issue with calling a function within a class that is derived from a base class.
The specific piece of code causing issue is:
FreeTile *tempPointer = new FreeTile();
cout<<tempPointer->getFree()<<endl;
mapp[i][j] = tempPointer;
when i call getFree (which returns a boolean value) i get the error:
undefined reference to Tile::getFree(). Tile being the base class.
The header for FreeTile is:
#ifndef FREETILE_H
#define FREETILE_H
#include "Tile.h"
class FreeTile:public Tile
{
public:
FreeTile();
virtual ~FreeTile();
void setParent(FreeTile* par);
int getF();
int getG();
int getH();
void setF(int in);
void setG(int in);
void setH(int in);
FreeTile* getParent();
protected:
private:
int F;
int G;
int H;
bool free;
};
Tile header is:
#ifndef TILE_H
#define TILE_H
class Tile
{
public:
Tile();
virtual ~Tile();
bool getFree();
void setFree(bool bo);
protected:
private:
bool free;
};
#endif // TILE_H
#endif // FREETILE_H
Finally the cpp file for Tile:
#include "Tile.h"
#include <iostream>
using namespace std;
bool free;
Tile::Tile()
{
cout<<"Constructor Called"<<endl;
}
Tile::~Tile()
{
//dtor
}
bool getFree(){
return free;
}
void setFree(bool bo){
free = bo;
}
If you need more code or if im missing something blatant feel free to shame me as much as you like :P
Thanks in advance.
On a side note, can you initiate a private variable in a constructor such as free = true as when doing this it states the variable is private.
In the Cpp file rename "bool getFree()" to
"bool Tile::getFree()"
In your implementation the function is just a regular c gloabl function.
In the fixed version it is the class function implementaion of the function you declare in the header file
Also
1st in your Tile you have a private variable "bool free"
in the cpp file you have a global variable "bool free"
this is confusing.
Probably want to delete the one you declared in the cpp file.
Want a deeper explanation?
Yeah! my 1st answer!
Deeper Explanation:
the function you declared in the Class Tile is not defined (just declared) because you didn't add "Tile::" before the function definition in the cpp file (i.e you didn't define a scope).
The function you wrote in the cpp file is both defined and declared in the cpp file, so only functions written after it in the cpp file can call it (works same a c).
Probably when you wrote the function it didn't know that "free" was, right? because it was not a class function. so you added the global "bool free" but that is a completely different variable.
Glad to help!
don't forget to mark this as answered!

c++ inheritence base and sub class

This is my base class Shape.h
#ifndef Shape_H
#define Shape_H
#include <iostream>
using namespace std;
class Shape
{
protected:
string name;
bool containsObj;
public:
Shape();
Shape(string, bool);
string getName();
bool getContainsObj();
double computeArea();
};
#endif
Shape.cpp
#include "Shape.h"
Shape::Shape(string name, bool containsObj)
{
this -> name = name;
this -> containsObj = containsObj;
}
string Shape:: getName()
{
return name;
}
bool Shape::getContainsObj()
{
return containsObj;
}
and this is my sub class. Cross.h
#ifndef Cross_H
#define Cross_H
#include <iostream>
#include "Shape.h"
using namespace std;
class Cross: public Shape
{
protected:
int x[12];
int y[12];
public:
Cross();
double computeArea();
};
#endif
Cross.cpp
#include "Cross.h"
Cross::Cross()
{
for (int i = 0; i < 12; i++)
{
this -> x[i] = 0;
this -> x[0] = 0;
}
}
Shape and Cross are in different files, but inside the same folder. The weird thing is when i compile this, errors that i have never seen before came up such as "In function 'ZN5CrossC1Ev', undefined reference to Shape::Shape(),'ZN5CrossC1Ev', undefined reference to Shape::Shape(), undefined reference to WinMain#16".
I tried to do some debugging myself. When i remove the Cross constructor, it works fine. But i definitely need it. Can anyone explain this to me?
You didn't define the default constructor but you declared it Shape();. The only constructor you defined is the one with string and bool parameters Shape(string, bool);.
adding
Shape::Shape()
{
}
or removing
Shape();
will fix it.
For future debugging read the error more carefully, it explains exactly whats wrong:
undefined reference to Shape::Shape()
You've declared a default constructor for Shape, but not defined it anywhere. The default constructor for Cross uses it implicitly to initialise its base class.
You options are:
Define the constructor, if you want Shape to be default-constructible;
Otherwise, remove the declaration and get Cross to initialise Shape with the other constructor.