Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 months ago.
Improve this question
I'm trying to build a custom regular mesh, so I'm having one template class for the cell, and one template class for the mesh.
But it's giving me an error when trying to make both of them in the same header and both being templates.
Here's the code:
#include <list>
using namespace std;
template<typename T>
class Cell{
list<T> points;
public:
friend class Mesh<T>; // Error here "Explicit specialization of undeclared template class"
Cell(): points() {}
void insert(const T &data) { points.push_back(data); }
T *search(const T &data);
bool delete(const T &data);
};
template<typename T>
class Mesh { // Error here "Redefinition of 'Mesh' as different kind of symbol"
float xMin, yMin, xMax, yMax;
float tamaCellX, tamaCellY;
vector<vector<Cell<T>>> mr;
Cell<T> *getCell(float x, float y);
public:
Mesh(int aXMin, int aYMin, int aXMax, int aYMax, int aNDiv);
void insert(float x, float y, const T &data);
T *search(float x, float y, const T &data);
bool delete(float x, float y, const T &data);
};
What am I doing wrong?
You need to pre-declare the class template you're using:
template<typename T>
class Mesh;
This is needed before the Cell definition. However, you also need to change delete members as that's a reserved keyword. You'll need to include <vector> as you're using it. Finally, it's very much non-recommended to do using namespace std; (because, when you'll have large projects, you might encounter multiple libs defining their own list, vector, etc.) - just write std:: before standard lib classes.
bool delete(float x, float y, const T &data);
delete is a operator in c++ try to rename in both classes and your class Mesh is defined after class Cell make a header for class Mesh
Related
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.
I'm working on a simple game C++ game using the SFML library. This is one of my first endeavors with C++ and I'm running into some problems with defining structs in headers.
Here is the bullet.h:
#pragma once
#include <SFML\Graphics.hpp>
struct BulletTransform {
sf::RectangleShape shape;
//details
BulletTransform(float, float);
};
class Bullet {
//class definition stuff, no problems here
then I try to create an implementation in the bullet.cpp file:
#include "Bullet.h"
struct BulletTransform {
sf::RectangleShape shape;
BulletTransform::BulletTransform(float mX, float mY)
{
//constructor for shape stuff
}
};
Now when I try to compile it throws an error saying struct in the bullet.cpp being a type redefinition. I understand that I cannot define a struct with the same name twice, but I am also not sure how I can fix this issue. Do I somehow need to get a reference to the definition in the header? Or is my implementation simply wrong? Thanks in advance!
In the header file you can make the declaration. In the source file the definition - thats the rule of thumb in general. In your case for example:
in bullet.h:
struct BulletTransform {
sf::RectangleShape shape;
// cntr
BulletTransform(float mX, float mY) ;
// other methods
void Function1(float x, float y, float z);
};
in bullet.cpp:
BulletTransform::BulletTransform(float mX, float mY) {
// here goes the constructor stuff
}
void BulletTransform::Function1(float x, float y, float z) {
// ... implementation details
}
Normally you don't do some heavy stuff in the constructor - just the initialization of data members for example to some default values.
Hope this helps.
You've repeated your struct definition in your implemenation file. Don't do that. Instead, provide definitions for the individual members, like this:
#include "Bullet.h"
BulletTransform::BulletTransform(float mX, float mY)
{
//constructor for shape stuff
}
Could someone ease my struggling, please. I am trying to organize two classes (Points) to return opposite class in their methods. Cartesian point class has method that returns Polar point and vice versa.
Point2D.h
#pragma once
#include "PointPolar2D.h"
class Point2D
{
private:
double x;
double y;
public:
Point2D(double x, double y);
PointPolar2D toPolar();
~Point2D();
};
Point2D.cpp
#include "stdafx.h"
#include "Point2D.h"
#include "PointPolar2D.h"
Point2D::Point2D(double x, double y) : x(x), y(y)
{
}
PointPolar2D Point2D::toPolar()
{
return PointPolar2D(1, 4);
}
Point2D::~Point2D()
{
}
PointPolar2D.h
#pragma once
#include "Point2D.h"
class PointPolar2D
{
private:
double radius;
double angle;
public:
PointPolar2D(double radius, double angle);
Point2D toCartesian();
~PointPolar2D();
};
PointPolar2D.cpp
#pragma once
#include "Point2D.h"
class PointPolar2D
{
private:
double radius;
double angle;
public:
PointPolar2D(double radius, double angle);
Point2D toCartesian();
~PointPolar2D();
};
It does not compile. The error says: toPolar:unknown override specifier and also unexpected token(s)preceding ;
Please, help me figure out the reason. It must be something obvious or not.
I will provide any clarifications if needed.
Thanks.
EDITED
Created MCVE, as #Amit proposed. Thanks.
From the names of the classes, I'm guessing that PointPolar2D is a sub-class of Point2D.
Hence, PointPolar2D.h needs to #include Point2D.h. You also have:
#include "PointPolar2D.h"
in Point2D.h. That is circular inclusion. It leads to all kinds of problems.
Remove that line from Point2D.h and add a forward declaration.
class PointPolar2D;
You don't need the complete class definition to declare the function.
PointPolar2D toPolar();
A forward declaration would suffice.
Make sure to #include PointPolar2D.h in Point2D.cpp. You need the definition of PointPolar2D to implement Point2D::toPolar.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm trying to create a class that inherits a class, which inherits from another class. In this base class is a template.
Two classes inherit from this base class, and then two more classes each inherit from one or the other class. I am getting a 'class template has already been defined' compile error when I am trying to incorporate the buttons I have created into a menu. It will compile if I only include one of the button types, but if I compile with both files included I get the error.
The base class of all the buttons
#pragma once
#include <SFML/Graphics.hpp>
namespace eng
{
namespace UI
{
template<class EventTemplate>
class ButtonBase
{
protected:
bool hasBeenClicked;
typename EventTemplate myEvent;
sf::Sprite mySprite;
public:
ButtonBase();
virtual bool CheckIfUsed(int mouseX, int mouseY, bool mouseClicked, bool mouseReleased) = 0;
EventTemplate GetButtonEvent();
sf::Sprite *GetSprite();
};
};
};
The base class for radio buttons
#pragma once
#include "Button Base.h"
namespace eng
{
namespace UI
{
template<class EventTemplate>
class RadioButton : public ButtonBase<EventTemplate>
{
protected:
enum ButtonState {MouseOutside, MouseOver, On, Off} myState;
public:
RadioButton();
virtual bool CheckIfUsed(int mouseX, int mouseY, bool mouseClicked, bool mouseReleased) = 0;
void TurnOff();
};
};
};
The two classes intended to be used as instances.
#pragma once
#include "Radio Button.h"
namespace eng
{
namespace UI
{
template<class EventType>
class RoundRadioButton : public RadioButton<EventType>
{
private:
sf::Rect myArea;
public:
RoundRadioButton(double setx, double sety, double setRadius, sf::Image &setImage, EventType setEvent);
bool CheckIfUsed(int mouseX, int mouseY, bool mouseClicked, bool mouseReleased);
};
};
};
#pragma once
#include "Radio Button.h"
namespace eng
{
namespace UI
{
template<class EventType>
class RoundRadioButton : public RadioButton<EventType>
{
private:
double myCenterX;
double myCenterY;
double myRadius;
public:
RoundRadioButton(double setx, double sety, double setRadius, sf::Image &setImage, EventType setEvent);
bool CheckIfUsed(int mouseX, int mouseY, bool mouseClicked, bool mouseReleased);
};
};
};
The class that includes the two radio button types.
#pragma once
#include <vector>
#include "Round Radio Button.h"
#include "Rectangular Radio Button.h"
namespace eng
{
namespace UI
{
template<class EventType>
class RadioButtonMenu
{
private:
RadioButton<EventType> *myCurrentChoice;
std::vector< RadioButton<EventType>* > myButtons;
public:
RadioButtonMenu();
~RadioButtonMenu();
void SetParameters(std::vector< RadioButton<EventType> > *setButtons, RadioButton<EventType> *defaultSelection);
void CheckMouseAction(double mousex, double mousey, bool mouseClicked, bool mouseReleased);
EventType GetSelection();
};
};
};
If I leave out one of the radio button includes, the error goes away. I looked this up in a search and it has been solved using #pragma once or #ifndef but I'm already doing that. I've been reading about templates more as this is my first real attempt at using them but I'm having difficulty understanding it. Any help solving this problem or with templates in general would be greatly appreciated.
You are violating One Definition Rule (ODR) as you create two templates of the same name (RadioButtonMenu) with different definitions. Give them different names or merge them into one template as they only seem to differ in private (implementation) part and have the same interface.
I'm trying to use inheritance among classes defined inside a class template (inner classes). However, the compiler (GCC) is refusing to give me access to public members in the base class.
Example code:
template <int D>
struct Space {
struct Plane {
Plane(Space& b);
virtual int& at(int y, int z) = 0;
Space& space; /* <= this member is public */
};
struct PlaneX: public Plane {
/* using Plane::space; */
PlaneX(Space& b, int x);
int& at(int y, int z);
const int cx;
};
int& at(int x, int y, int z);
};
template <int D>
int& Space<D>::PlaneX::at(int y, int z) {
return space.at(cx, y, z); /* <= but it fails here */
};
Space<4> sp4;
The compiler says:
file.cpp: In member function ‘int& Space::PlaneX::at(int, int)’:
file.cpp:21: error: ‘space’ was not declared in this scope
If using Plane::space; is added to the definition of class PlaneX, or if the base class member is accessed through the this pointer, or if class Space is changed to a non-template class, then the compiler is fine with it.
I don't know if this is either some obscure restriction of C++, or a bug in GCC (GCC versions 4.4.1 and 4.4.3 tested). Does anyone have an idea?
It should be a problem related to c++'s two-phase name lookup:
http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Name-lookup.html#Name-lookup