expected class name before '{' token. C++ inheritance issue - c++

I've been on this for ages and did a lot of research and couldn't find an answer.
I have 4 classes: BuildingBase, CustomBuildingBase, Attackable, Nexus.
CustomBuildingBase is inherited from BuildingBase and Attackable, Nexus is inherited from CustomBuildingBase
I get this error in my nexus.h file
expected class name before '{' token.
My code:
custombuildingbase.h
#ifndef CUSTOMBUILDINGBASE_H
#define CUSTOMBUILDINGBASE_H
#include "buildings/buildingbase.h"
#include "handlerandmanager/gameeventhandler.h"
#include "handlerandmanager/gamemanager.h"
#include "attackable.h"
namespace Whiskas {
class CustomBuildingBase : public Course::BuildingBase, public Attackable
{
public:
CustomBuildingBase() = delete;
explicit CustomBuildingBase(
const std::shared_ptr<gameEventHandler>& eventhandler,
const std::shared_ptr<gameManager>& objectmanager,
const std::shared_ptr<Course::PlayerBase>& owner,
const int& tilespaces = 1,
const Course::ResourceMap& buildcost = {},
const Course::ResourceMap& production = {},
int health=3,
int attack=0
);
};
}
#endif // BUILDINGBASE_H
nexus.h:
#ifndef NEXUS_H
#define NEXUS_H
#include "buildings/custombuildingbase.h"
#include "core/resourcemaps.h"
namespace Whiskas {
static const Course::ResourceMap OUTPOST_PRODUCTIONMAP = {
{Course::BasicResource::MONEY, -10}
};
class Nexus : public CustomBuildingBase
{ //<--------------this is where I get the error
public:
Nexus() = delete;
explicit Nexus(
const std::shared_ptr<gameEventHandler>& eventhandler,
const std::shared_ptr<gameManager>& objectmanager,
const std::shared_ptr<Course::PlayerBase>& owner,
const int& tilespaces = 1,
const Course::ResourceMap& buildcost = Course::ConstResourceMaps::OUTPOST_BUILD_COST,
const Course::ResourceMap& production = Course::ConstResourceMaps::OUTPOST_PRODUCTION,
int health = 3,
int attack = 0
);
virtual ~Nexus() = default;
virtual std::string getType() const override;
virtual void onBuildAction() override;
virtual Course::ResourceMap getProduction() override;
}; // class Nexus
} // namespace Whiskas
#endif // NEXUS_H

Related

Static member declaration in Parametrized polymorphism

I have gender class with Male and Female as my parametric types of class
I am using following hierarchy:
#ifndef __GENDER_H
#define __GENDER_H
#include <string>
using namespace std;
// Forward declaration of templatized class
template<typename T>
class GenderTypes; // Generic Gender type to generate specific genders
// Generic gender type
class Gender { // Abstract Base Class
const string& name_; // Name of the Gender
struct MaleType {};
struct FemaleType {};
protected:
Gender(const string& name) : name_(name) {}
virtual ~Gender() { }
public:
const string& GetName() const { return name_; }
bool IsMale(const Gender&); // Checking and matching gender
// Enumerated types - the target sub-types
typedef GenderTypes<MaleType> Male;
typedef GenderTypes<FemaleType> Female;
};
// Specific gender types
template<typename T>
class GenderTypes : public Gender {
static const string sName;
GenderTypes(const string& name = GenderTypes<T>::sName) : Gender(name) { }
~GenderTypes() { }
public:
// Singleton object - placeholder for the respective type
static const GenderTypes<T>& Type() {
static const GenderTypes<T> theObject; // May be non-const for changeable behavior
return theObject;
}
};
inline bool Gender::IsMale(const Gender& g) { return &g == &Gender::Male::Type(); }
#endif
And declaring the static member name_ as follows:
#include <string>
using namespace std;
#include "../inc/gender.h"
// Names defined as static constants
const string Gender::Male::sName = "Male";
const string Gender::Female::sName = "Female";
This kind of hierarchy is fine . then why compiler gives this error:
gender.cpp:5:14: error: specializing member ‘GenderTypes<Gender::MaleType>::sName’ requires ‘template<>’ syntax
5 | const string Gender::Male::sName = "Male";
how should i initialize this static datas?
I am using VS CODE editor and Ubuntu 20.04
With this small change in your .cpp it compiles (and works) fine on my machine :
template<>
const string Gender::Male::sName = "Male";
template<>
const string Gender::Female::sName = "Female";
And indeed in your header you should use
#ifndef GENDER_H
#define GENDER_H
[...]
#endif
OR
#pragma once
[...]

Variable or field declared void C++

I am making a school assignment, but I am getting a strange error. I have tried to google it, but nothing helped.
So I have a file called main.cpp. Within this file I have some includes and code.
This:
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
#include "RentalAdministration.h"
#include "Limousine.h"
#include "Sedan.h"
void addTestDataToAdministration(RentalAdministration* administration)
{
string licencePlates[] = {"SD-001", "SD-002", "SD-003", "SD-004", "LM-001", "LM-002"};
for (int i = 0; i < 4; i++)
{
Car* sedan = new Sedan("BMW", "535d", 2012 + i, licencePlates[i], false);
administration->Add(sedan);
}
for (int i = 4; i < 6; i++)
{
Car* limousine = new Limousine("Rolls Roys", "Phantom Extended Wheelbase", 2015, licencePlates[i], true);
administration->Add(limousine);
}
}
int main( void )
{
RentalAdministration administration;
addTestDataToAdministration(&administration);
}
So the compiler tells me that the variable: "RentalAdministration administration" does not exist.
So if we have look in my rentaladministration header. We see this:
#ifndef RENTALADMINISTRATION_H
#define RENTALADMINISTRATION_H
#include <vector>
#include "car.h"
class RentalAdministration
{
private:
std::vector<Car*> Cars;
Car* FindCar(std::string licencePlate);
Car* FindCarWithException(std::string licencePlate);
public:
std::vector<Car*> GetCars() const {return Cars;}
bool Add(Car* car);
bool RentCar(std::string licencePlate);
double ReturnCar(std::string licencePlate, int kilometers);
void CleanCar(std::string licencePlate);
RentalAdministration();
~RentalAdministration();
};
#endif
This is the exact error:
src/main.cpp:18:34: error: variable or field ‘addTestDataToAdministration’ declared void
void addTestDataToAdministration(RentalAdministration* administration)
^
src/main.cpp:18:34: error: ‘RentalAdministration’ was not declared in this scope
src/main.cpp:18:56: error: ‘administration’ was not declared in this scope
void addTestDataToAdministration(RentalAdministration* administration)
Help will be appreciated!
Edit:
I am getting warnings in sublime for the Sedan and Limousine headers. Something that has to do with some static constants. I think it was called a GNU extension. Maybe it has something to do with it.
Even when I comment the call of that function out. I get the same error.
I am calling that function nowhere else.
Some people say that the cause might be in these headers:
#ifndef LIMOUSINE_H
#define LIMOUSINE_H
#include "Car.h"
//c
class Limousine : public Car
{
private:
bool needsCleaning;
bool hasMiniBar;
static const double priceperkm = 2.5;
public:
double Return(int kilometers);
void Clean();
bool GetHasMiniBar() const { return hasMiniBar;}
void SetHasMiniBar(bool value) {hasMiniBar = value;}
Limousine(std::string manufacturer, std::string model, int buildYear, std::string licencePlate, bool hasminiBar);
~Limousine();
};
#endif
2:
#ifndef SEDAN_H
#define SEDAN_H
#include "Car.h"
//c
class Sedan : public Car
{
private:
int lastCleanedAtKm;
bool hasTowBar;
bool needsCleaning;
static const double priceperKm = 0.29;
public:
void Clean();
int GetLastCleanedAtKm() const {return lastCleanedAtKm;}
void SetLastCleanedAtKm(bool value){ lastCleanedAtKm = value;}
bool GetHasTowBar() const {return hasTowBar;}
void SetHasTowBar(bool value) {hasTowBar = value;}
bool GetNeedsCleaning() const {return needsCleaning;}
void SetNeedsCleaning(bool value){needsCleaning = value;}
Sedan(std::string manufacturer, std::string model, int buildYear, std::string licencePlate, bool hastowBar);
~Sedan();
};
#endif
class Limousine : public Car
{
private:
static const double priceperkm = 2.5;
...
}
Remove the static and declare the member simply as const double, example:
class Limousine : public Car
{
private:
const double priceperkm = 2.5;
...
}
The error message ‘RentalAdministration’ was not declared in this scope indicates that the right header file for RentalAdministration was not included. Check the file names to make sure class declaration for RentalAdministration is in the right file.
Restarting the terminal has somehow solved this error. I got another error this time, which I solved already. I missed the destructor. It stood in the header file, but not in the cpp file.
Buggy terminals...

Difficulties with Pointers in C++ / Linking error

When compiling my code I get the following error:
LNK2019 unresolved external symbol "public: class QuantLib::Matrix
const & __cdecl SachLib::AWDCalculator::RSB(void)const "
(?RSB#AWDCalculator#SachLib##QEBAAEBVMatrix#QuantLib##XZ) referenced
in function "protected: void __cdecl
SachLib::CalculationEngine_Sach::set_results(class
boost::shared_ptr &)const "
(?set_results#CalculationEngine_Sach#SachLib##IEBAXAEAV?$shared_ptr#VAWDCalculator#SachLib###boost###Z)
It is found in CalculationEngine_Sach.obj in line 1.
Here is my code:
Abwicklungsdreieck and Gewichtungsfaktoren basically consists out of a Matrix and are obtained by its respective Reader. I think the names of their methods are unambiguous.
main.cpp:
#include "AbwicklungsdreieckReader.h"
#include "Abwicklungsdreieck.h"
#include "GewichtungsfaktorenReader.h"
#include "Gewichtungsfaktoren.h"
#include "Tarif_Sach.h"
#include "CalculationEngine_Sach.h"
#include "CalculationEngine_Sach_Typ1.h"
#if defined(QL_ENABLE_SESSIONS)
namespace QuantLib {
Integer sessionId() { return 0; }
}
#endif
using namespace QuantLib;
using namespace SachLib;
int main()
{
std::cout << "\n\n\nTest Chain-Ladder-Verfahren\n\n";
std::string input1 = "C:/Users/D91476/Desktop/LifeLib_Sol/awd.csv";
std::string input2 = "C:/Users/D91476/Desktop/LifeLib_Sol/gwf.csv";
boost::shared_ptr<PricingEngine> EngineTyp1(new CalculationEngine_Sach_Typ1());
SachLib::AbwicklungsdreieckReader input1Reader(input1);
SachLib::GewichtungsfaktorenReader input2Reader(input2);
SachLib::Abwicklungsdreieck AWD = input1Reader.get_awd();
std::cout << "\nInput 1: Abwicklungsdreieck\n\n";
std::cout << AWD.get_Matrix() << "\n";
SachLib::Gewichtungsfaktoren GWF = input2Reader.get_gwf();
std::cout << "\nInput 2: Gewichtungsfaktoren\n\n";
std::cout << GWF.get_Matrix();
Tarif_Sach *tarifsach1;
tarifsach1 = new Tarif_Sach_Typ1(AWD, GWF);
tarifsach1->setPricingEngine(EngineTyp1);
EngineTyp1->calculate();
}
By testing I found out that the error appears because of the following line:
boost::shared_ptr<PricingEngine> EngineTyp1(new CalculationEngine_Sach_Typ1());
AWDCalculator.h
#pragma once
#include "Tarif_Sach.h"
#include "Abwicklungsdreieck.h"
#include "Gewichtungsfaktoren.h"
#ifndef _AWDCalculator_H
#define _AWDCalculator_H
namespace SachLib {
class AWDCalculator {
public:
AWDCalculator();
AWDCalculator(Tarif_Sach::arguments *arguments);
AWDCalculator(Abwicklungsdreieck awd, Gewichtungsfaktoren gwf);
AWDCalculator(const AWDCalculator &obj);
AWDCalculator& operator=(AWDCalculator const& rhs);
//! Fills vectors.
void update(Tarif_Sach::arguments *arguments);
// Logic
void calculate(Abwicklungsdreieck awd, Gewichtungsfaktoren gwf);
//! \name Getter
//#{
const Abwicklungsdreieck& awd() const;
const Gewichtungsfaktoren& gwf() const;
const Matrix& estimated_costs_matrix() const;
const Matrix& RSB_all() const;
const Matrix& RSB() const;
//#}
Tarif_Sach::arguments *arguments_;
protected:
AWDCalculator *results_;
Abwicklungsdreieck awd_;
Gewichtungsfaktoren gwf_;
//! \name Outputs
//#{
Matrix estimated_costs_matrix_;
Matrix RSB_all_;
Matrix RSB_;
//#}
};
}
#endif
AWDCalculator.cpp
#include "AWDCalculator.h"
SachLib::AWDCalculator::AWDCalculator()
{
}
SachLib::AWDCalculator::AWDCalculator(Tarif_Sach::arguments *arguments)
: AWDCalculator(arguments->awd, arguments->gwf)
{
arguments_ = arguments;
}
SachLib::AWDCalculator::AWDCalculator(Abwicklungsdreieck awd, Gewichtungsfaktoren gwf)
: awd_(awd),
gwf_(gwf)
{
results_ = new AWDCalculator(awd, gwf);
calculate(awd, gwf);
}
SachLib::AWDCalculator::AWDCalculator(const AWDCalculator& obj)
{
arguments_ = obj.arguments_;
awd_ = obj.awd_;
gwf_ = obj.gwf_;
}
SachLib::AWDCalculator& SachLib::AWDCalculator::operator=(AWDCalculator const& rhs)
{
arguments_ = rhs.arguments_;
awd_ = rhs.awd_;
gwf_ = rhs.gwf_;
return *this;
}
void SachLib::AWDCalculator::update(Tarif_Sach::arguments *arguments)
{
results_ = new AWDCalculator(arguments);
arguments_ = arguments;
awd_ = arguments->awd;
gwf_ = arguments->gwf;
}
void SachLib::AWDCalculator::calculate(Abwicklungsdreieck awd, Gewichtungsfaktoren gwf)
{
// RSB_, RSB_all_ and estimated_costs_matrix_ get calculated here with the input awd and gwf
}
inline const Matrix& SachLib::AWDCalculator::estimated_costs_matrix() const
{
return estimated_costs_matrix_;
}
inline const Matrix& SachLib::AWDCalculator::RSB_all() const
{
return RSB_all_;
}
inline const Matrix& SachLib::AWDCalculator::RSB() const
{
return RSB_;
}
inline const SachLib::Abwicklungsdreieck& SachLib::AWDCalculator::awd() const
{
return awd_;
}
inline const SachLib::Gewichtungsfaktoren& SachLib::AWDCalculator::gwf() const
{
return gwf_;
}
CalculationEngine_Sach.h
#pragma once
#ifndef SachLib_calculationengine_sach_hpp
#define SachLib_calculationengine_sach_hpp
#include "Tarif_Sach.h"
#include "AWDCalculator.h"
using namespace QuantLib;
namespace SachLib {
class CalculationEngine_Sach : public Tarif_Sach::engine {
public:
CalculationEngine_Sach();
void calculate() const;
protected:
mutable boost::shared_ptr<AWDCalculator> AWDCalculator_;
void set_results(boost::shared_ptr<AWDCalculator> &awdCalculator) const;
};
}
#endif
CalculationEngine_Sach.cpp
#include "CalculationEngine_Sach.h"
#include "Tarif_Sach.h"
#include "AWDCalculator.h"
using namespace QuantLib;
namespace SachLib {
CalculationEngine_Sach::CalculationEngine_Sach()
{
}
void CalculationEngine_Sach::calculate() const
{
arguments_.AWDCalculator->update(&arguments_);
CalculationEngine_Sach::set_results(arguments_.AWDCalculator);
}
void CalculationEngine_Sach::set_results(boost::shared_ptr<AWDCalculator> &awdCalculator) const
{
results_.estimated_costs_matrix = awdCalculator->estimated_costs_matrix();
results_.RSB_all = awdCalculator->RSB_all();
results_.RSB = awdCalculator->RSB();
// because of the above three lines the error appears
}
}
CalculationEngine_Sach_Typ1.h
#pragma once
#include "CalculationEngine_Sach.h"
namespace SachLib {
class CalculationEngine_Sach_Typ1 : public CalculationEngine_Sach
{
public:
CalculationEngine_Sach_Typ1();
};
}
CalculationEngine_Sach_Typ1.cpp
#include "CalculationEngine_Sach_Typ1.h"
SachLib::CalculationEngine_Sach_Typ1::CalculationEngine_Sach_Typ1()
{}
If anyone could help I'd be much grateful.
You marked that function inline in the .cpp:
inline const Matrix& SachLib::AWDCalculator::RSB() const
In this case the compiler does not have to generate an out-of-line definition, unless it is needed in that translation unit (e.g. function address is taken). And that causes the linker error.
Remove inline from the function definition to make the compiler generate an out-of-line definition to fix the linker error.
you'll need to find the include file that has the definition for "Matrix" - that's what the error is showing.

Invalid conversion c++

I get this error
invalid conversion from 'GameObject*' to 'std::vector::value_type {aka SDLGameObject*}' [-fpermissive]
This is my code I don't know what is wrong because in my class MainMenu it works this is my class Playstate.cpp
bool PlayState::onEnter()
{
SDL_ShowCursor(1);
//parse the state
TheTextureManager::Instance()->load("assets/button.png", "test", TheGame::Instance()->getRenderer());
GameObject* pGameObject1 = TheGameObjectFactory::Instance()->create("MenuButton");
pGameObject1->load(new LoaderParams(120, 300, 400, 100, "test", 4, 4, 4));
m_gameObjects.push_back(pGameObject1);
}
GameObject.h
#include "LoaderParams.h"
#include <string>
using namespace std;
class GameObject {
public:
virtual void draw() = 0;
virtual void update() = 0;
virtual void clean() = 0;
virtual void load(const LoaderParams* pParams)=0;
protected:
GameObject() {}
virtual ~GameObject() {}
};
#endif /* GAMEOBJECT_H_ */
SDLGameObject.h
#ifndef SDLGAMEOBJECT_H_
#define SDLGAMEOBJECT_H_
#pragma once
#include "GameObject.h"
#include "LoaderParams.h"
#include "TextureManager.h"
#include "Vector2D.h"
class SDLGameObject : public GameObject {
public:
SDLGameObject();
virtual void draw();
virtual void update();
virtual void clean(){}
virtual void load(const LoaderParams* pParams);
Vector2D& getPosition() { return m_position; }
virtual int getWidth() { return m_width; }
virtual int getHeight() { return m_height; }
protected:
Vector2D m_position;
Vector2D m_velocity;
Vector2D m_acceleration;
int m_width;
int m_height;
int m_currentRow;
int m_currentFrame;
std::string m_textureID;
};
#endif /* SDLGAMEOBJECT_H_ */
MainMenuState.cpp here it does work!!
bool MainMenuState::onEnter()
{
SDL_ShowCursor(1);
//parse the state
TheTextureManager::Instance()->load("assets/button.png", "playbutton" , TheGame::Instance()->getRenderer());
TheTextureManager::Instance()->load("assets/exit.png", "exitbutton" , TheGame::Instance()->getRenderer());
GameObject* pGameObject = TheGameObjectFactory::Instance()->create("MenuButton");
GameObject* pGameObject1 = TheGameObjectFactory::Instance()->create("MenuButton");
pGameObject->load(new LoaderParams(120, 150, 400, 100, "playbutton", 0, 1, 2));
pGameObject1->load(new LoaderParams(120, 300, 400, 100, "exitbutton", 1, 2, 2));
m_gameObjects.push_back(pGameObject);
m_gameObjects.push_back(pGameObject1);
m_callbacks.push_back(0);
m_callbacks.push_back(s_menuToPlay);
m_callbacks.push_back(s_exitFromMenu);
//set callbacks for menu items
setCallbacks(m_callbacks);
std::cout << "Entering MainMenuState\n";
return true;
}
SDLGameObject derives from the base class GameObject. Your vector m_gameObjects stores pointers to SDLGameObject, but you are giving it a pointer to a GameObject.
This conversion is not possible as a GameObject is not necessarily an SDLGameObject.
If you are sure that this is the case you can do this:
SDLGameObject* p = dynamic_cast<SDLGameObject>(pGameObject1);
if(!p) {
// pGameObject1 is actually not an SDLGameObject
}
m_gameObjects.push_back(p);
Or change the definition of m_gameObjects to store pointers to GameObject.

C++ Event Handling - issue with const

I already asked about my Problem, now I'm on the next Step. In the code below I have the Problem, that I always have to make the EventHandler (Server::HandleMessage) static. But I need to have it non static to access other Variables in the Server class from within the Handler.
How can I achieve this?
Here my Code:
#include <iostream>
#include <functional>
using namespace std;
class Client{
public:
struct MessageReceiveArgs {
MessageReceiveArgs(int ID, const std::string& Text) : ID(ID), Text(Text) {}
int ID;
std::string Text;
};
std::function<void(MessageReceiveArgs)> onMessageReceive;
Client(){}
void FireEvent(){
this->onMessageReceive(MessageReceiveArgs(16, "SomeText"));
}
};
class Server{
public:
int i;
Server(){
this->client.onMessageReceive = &Server::HandleMessage;
this->i = 5;
}
void FireEvent(){
this->client.FireEvent();
}
Client client;
static void HandleMessage(Client::MessageReceiveArgs args) {
std::cout<<"ID "<<args.ID<<": "<<" "<<args.Text<<std::endl;
//need it non static
//std::cout<<"I: "<<this->i<<std::endl;
}
};
int main() {
Server sv = Server();
sv.FireEvent();
}
As mentioned in my earlier Post, i'm new to Standard C++ (Unix).
I'm fairly sure this is what you're after. You need to bind the implicit this explicitly when invoking a pointer-to-member through std::function in the fashion you seem to desire.
#include <iostream>
#include <functional>
using namespace std;
class Client{
public:
struct MessageReceiveArgs
{
MessageReceiveArgs(int ID, const std::string& Text)
: ID(ID), Text(Text) {}
int ID;
std::string Text;
};
Client(){}
void FireEvent()
{
this->onMessageReceive(MessageReceiveArgs(16, "SomeText"));
}
std::function<void(MessageReceiveArgs)> onMessageReceive;
};
class Server
{
public:
int i;
Server()
{
this->client.onMessageReceive
= std::bind(&Server::HandleMessage, this, std::placeholders::_1);
this->i = 5;
}
void FireEvent()
{
this->client.FireEvent();
}
Client client;
void HandleMessage(Client::MessageReceiveArgs args)
{
std::cout<<"ID "<<args.ID<<": "<<" "<<args.Text<<std::endl;
}
};
int main()
{
Server sv = Server();
sv.FireEvent();
}
Output
ID 16: SomeText