i'm recently starting with c++ and I'm having troubles when I want to use a c++ class like Java's enums.
I would have the 'simulated enum' class attribute, but when I try to initialize the attribute in the constructor i received the following error:
no default constructor exists for model::suite
I now I have the constructor private, but the enum should have private constructors to prevent the construction of undefined objects of that class.
¿What should I do?
e.g.
suite.h
#include <iostream>
#include <string>
#include <vector>
namespace model
{
class Suite
{
public:
static const Suite CLUBS;
static const Suite DIAMONDS;
static const Suite SPADES;
static const Suite HEARTS;
static const int SIZE = 4;
private:
std::string name;
Suite(std::string name)
{
this->name = name;
}
public:
std::string toString()
{
return name;
}
std::vector<Suite> values()
{
return {Suite::CLUBS, Suite::DIAMONDS, Suite::SPADES, Suite::HEARTS};
}
};
const Suite Suite::CLUBS = Suite("CLUBS");
const Suite Suite::DIAMONDS = Suite("DIAMONDS");
const Suite Suite::SPADES = Suite("SPADES");
const Suite Suite::HEARTS = Suite("HEARTS");
}
card.h
#pragma once
#include <string>
#include "suite.h"
#include "face.h"
namespace model
{
class Card
{
public:
Card(int face, int suite);
Face getFace();
Suite getSuite();
bool isMergeable();
std::string toString();
private:
Face face;
Suite suite;
bool isRed();
bool isContigous(Card card);
};
}
card.cpp
#include <iostream>
#include "card.h"
using namespace std;
using namespace model;
Card::Card(int face, int suite)
{
this->face = Face::VALUES[face];
}
Face Card::getFace()
{
}
Suite Card::getSuite()
{
}
bool Card::isMergeable()
{
}
std::string Card::toString()
{
}
bool Card::isRed()
{
}
bool Card::isContigous(Card card)
{
}
Well finally I found a solution.
When we try to implement the 'simulated enum' on the cpp file, we have the this-> pointer and also the scope who envolves it (scope rules), so we need to define the object before the { } of the constructor.
#include <iostream>
#include "card.h"
using namespace std;
using namespace model;
Card::Card(Face face, Suite suite) : face(face), suite(suite)
{
}
That way we could implement a stronger and useful enums with attributes and methods.
Related
I've got some classes like class below written in c++ and i have to implement them into the Windows Forms. Is there any solution to create unmanaged objects in Windows Forms /clr classes?
#pragma once
#ifndef _HOTEL_H
#define _HOTEL_H
#include "Room.h"
#include "Adress.h"
#include "Employee.h"
#include "Apartament.h"
#include "TechnicalRoom.h"
#include "RecreationRoom.h"
#include <vector>
#include <string>
using namespace std;
class Hotel {
protected:
int HotelID, HotelStars, NumberOfEmployee, NumberOfClients, NumberofRooms;
string HotelName;
Adress HotelAdress;
vector <Room*> Rooms;
vector <Person*> People;
public:
//methods
Hotel(int = 3, string = "Hotel");
~Hotel();
string getName();
int getNumberOfClients();
int getNumberOfEmployee();
int getHotelStars();
void changeNumberOfStars(int);
void BookApartament(int, int);
void AddRoom(int);
void DeleteRoom(int);
void AddEmployee();
void DeleteEmployee(int);
friend ostream & operator<< (ostream &out, Hotel &h);
friend ref class MainWindow;
};
#endif
It sounds like you might want something along the lines of:
namespace SomeCompany
{
public ref class Hotel
{
::Hotel* pHotel;
public:
Hotel() : pHotel(new ::Hotel()) {}
~Hotel() {
delete pHotel;
pHotel = nullptr;
}
!Hotel() {
delete pHotel;
}
// ... etc. ...
};
}
See How to: Wrap Native Class for Use by C# for many more details.
I'm beginner with C++ and I have code like this
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
class Role
{
public:
Role(int r, bool x)
:role(r), active(x)
{}
private:
int role;
bool active;
};
class Test
{
public:
Test(vector<Role> xxxx = {})
:xxx(xxxx)
{}
private:
std::vector<Role> xxx;
};
int main()
{
vector<Role> role = { Role(1,true),Role(2,false),Role(3, true) };
Test test1(role);
fstream dataTransfer("data.txt");
Test test2;
dataTransfer.write(reinterpret_cast<char*>(&test1), sizeof(Test));
dataTransfer.read(reinterpret_cast<char*>(&test2), sizeof(Test)); // error
return 0;
}
This code cause "Read access violation" but i can't find any error.
Thank for any help
Each time I've attempted to build my project, I receive the same error:
>error: no matching function for call to 'Agent::Agent()'
>note: candidates are: Agent::Agent(std::string, room*)
>note: Agent::Agent(const Agent&)
Initially I assumed that I was feeding the wrong values, but even after seemingly correcting, I still get the same error.
main
#include <iostream>
#include <string>
#include <sstream>
#include "room.h"
#include "Thing.h"
//#include "Agent.h"
//#include "Grue.h"
//#include "Player.h"
using namespace std;
int main()
{
srand(time(NULL));
room *entrance = new room("Entrance","A wide open entrance...", 100);
room *hallway = new room("Hallway","A long hallway...", 50);
room *ballroom = new room("Ballroom","A huge ballroom...", 200);
room *garden = new room("Garden","A lush garden...", 150);
entrance->link("south", hallway);
hallway->link("north", entrance);
hallway->link("east", ballroom);
ballroom->link("west", hallway);
ballroom->link("east", garden);
hallway->printLinked();
while(true)
{
for(int i = 0; i < agents.size(); i++)
{
bool ok = agents[i]->act();
if(!ok)
{
cout << "Game quits." << endl;
return 0;
}
}
}
Player *josh = new Player("Josh", entrance);
Player *tracy = new Player("Tracy", entrance);
game.addAgent(josh);
game.addAgent(tracy);
cout << "Welcome!" << endl;
// the step() function in the Game class will eventually
// return false, when a player chooses to quit;
// this tiny "while" loop keeps asking the game.step()
// function if it is false or true; the effect is
// that the step() function is called repeatedly
// until it returns false
while(game.step());
return 0;
}
Thing header
#ifndef THING_H
#define THING_H
#include <iostream>
#include <string>
#include "room.h"
class room;
class Thing
{
private:
std::string name, desc;
protected:
room* cur_room;
public:
Thing(std::string _name, std::string _desc);
std::string getName();
std::string getDesc();
int getSize();
};
#endif // THING_H
Thing cpp
#include "Thing.h"
Thing::Thing(std::string _name, std::string _desc)
{
name = _name;
desc = _desc;
cur_room = NULL;
}
std::string Thing::getName()
{
return name;
}
std::string Thing::getDesc()
{
return desc;
}
int Thing::getSize()
{
return size;
}
Agent header
#ifndef AGENT_H
#define AGENT_H
#include "Thing.h"
#include <iostream>
#include <string>
#include "room.h"
class Agent : public Thing
{
protected:
//bool walk(std::string exit);
room *cur_room;
std::string name;
public:
Agent(std::string _name, room *_cur_room);
void get_curroom();
virtual bool act() = 0;
std::string getName() { return name; }
};
#endif // AGENT_H
Agent cpp
#include "Agent.h"
Agent::Agent(std::string _name, room *_cur_room)
{
name = _name;
room = _cur_room;
}
bool Agent::walk(std::string exit)
{
return 0;
}
bool Agent::act()
{
return 0;
}
void Agent::get_curroom()
{
return cur_room;
}
Player header
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <string>
#include "Agent.h"
#include "room.h"
class room;
class Player : public Agent
{
private:
std::string name;
protected:
public:
Player(std::string _name, room *starting_room);
bool Act();
};
#endif // PLAYER_H
Player cpp
#include "Player.h"
Player::Player(std::string _name, room *starting_room)
{
name = _name;
cur_room = starting_room;
}
bool Player::Act()
{
std::cout << "Where do you want to go? (or 'quit')" << std::endl;
}
I'm honestly stumped on where to go next. Any help is greatly appreciated!
In your inheritance chain of Player<-Agent<-Thing, you aren't passing the constructor parameters up to the parent. So, you need to change the Player constructor to:
Player::Player(std::string _name, room *starting_room)
:Agent(_name, starting_room)
{
name = _name;
cur_room = starting_room;
}
And you need to change the Agent constructor to:
Agent::Agent(std::string _name, room *_cur_room)
:Thing(_name, "")
{
name = _name;
room = _cur_room;
}
The parts I added after the colons are called initialization lists. One use of these is to call the constructor of the parent class. In C++, you have to call the parent class's constructor by name, since there is no keyword to reference the parent class generally.
The problem is that in the constructor of Player you don't call the constructor of the base class Agent and the latter doesn't have the default constructor. To fix it call the Agent's constructor (or add the default constructor):
Player::Player(std::string _name, room *starting_room)
: Agent(_name, starting_room)
{
// ...
}
I'm trying to create base of objects, which contains as attribute vector of object from different class. Here's my code:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
using namespace std;
class form_mesto
{
public:
string mesto;
int year;
int mounth;
int day;
bool visit = false;
form_mesto(string a_mesto)
{
mesto = a_mesto;
}
};
class Place
{
private:
friend class boost::serialization::access;
template<class Archieve>
void serialize(Archieve&ar, const unsigned int version)
{
ar& mestа;
ar& person;
}
public:
string person;
vector<form_mesto> mestа;
Place(string a_person)
{
person = a_person;
}
void add_place(form_mesto a_mesto)
{
mestа.push_back(a_mesto);
}
};
int main()
{
string input_in_form = "London";
string input_in_Place = "Eugene";
form_mesto z = form_mesto(input_in_form);
Place x = Place(input_in_Place);
x.add_place(z);
std::ofstream ofs("save.dat", std::ios::binary);
boost::archive::binary_oarchive oa(ofs);
oa<<x;
};
Error, which i get is:
c:\boost_1_57_0\boost\serialization\access.hpp(118): error C2039: serialize: is not a member of "std::vector>".
Can somebody share experience of how to serialize that type of objects?
To make the code compilable, one has to do the following:
to include the header responsible for vector serialization
#include <boost/serialization/vector.hpp>
to add the serialize method to the form_mesto class
class form_mesto
{
private:
friend class boost::serialization::access;
template<class Archieve>
void serialize(Archieve&ar, const unsigned int version)
{
// ...
}
// ...
};
Here is the compilable code.
EDIT:
I'm using namespace std
I'm using VS10
Room is a separate class
I've included the memory header in all necessary files
The original error was an Intellisense error I was getting before building. After building, I got a buttload more:
[The original Intellisense error before building] declaration is incompatible with "std::tr1::shared_ptr<< error-type >> Option::getRoom()
'std::tr1::shared_ptr<_Ty> Option::getRoom(void)' : overloaded function differs only by return type from 'std::tr1::shared_ptr Option::getRoom(void)'
'Option::getRoom' : redefinition; different basic types
'Option::getRoom' uses undefined class 'std::tr1::shared_ptr'
These are related to this piece of code in Option.cpp:
shared_ptr<Room> Option::getRoom(){
shared_ptr<Room> room(new Room);
return room;
}
The corresponding code in Option.hpp:
public:
virtual shared_ptr<Room> getRoom();
Error 'RoomOption::getRoom': overriding virtual function return type differs and is not covariant from 'Option::getRoom'
[IntelliSense] return type is not identical to nor covariant with return type "std::tr1::shared_ptr<< error-type >>" of overridden virtual function function "Option::getRoom"
This is related to this piece of code in RoomOption.hpp, a subclass of Option:
public:
shared_ptr<Room> getRoom();
Here's all the code from the two classes I'm having trouble with:
Option.h:
#pragma once
#include "Room.h"
#include <memory>
using namespace std;
class Option
{
protected:
int id;
char* text;
public:
Option(void);
Option(int, char*);
virtual ~Option(void);
char* getText();
int getID();
virtual shared_ptr<Room> getRoom();
};
Option.cpp:
#include "Option.h"
#include "Room.h"
#include <memory>
using namespace std;
Option::Option(void)
{
}
Option::Option(int newID, char* newText){
id = newID;
text = newText;
}
Option::~Option(void)
{
}
char* Option::getText(){
return text;
}
int Option::getID(){
return id;
}
shared_ptr<Room> Option::getRoom(){
shared_ptr<Room> room(new Room());
return room;
//note that this function will never be used. I'd prefer to
//pass back a null pointer but I couldn't do that either.
}
RoomOption.h:
#pragma once
#include "Option.h"
#include "Room.h"
#include <memory>
using namespace std;
class RoomOption :
public Option
{
private:
shared_ptr<Room> room;
public:
RoomOption(void);
RoomOption(int, char*, shared_ptr<Room>);
~RoomOption(void);
void setRoom(shared_ptr<Room>);
shared_ptr<Room> getRoom();
};
RoomOption.cpp:
#include "RoomOption.h"
#include "Room.h"
#include <memory>
using namespace std;
RoomOption::RoomOption(void)
{
}
RoomOption::RoomOption(int newID, char* newText, shared_ptr<Room> newRoom)
{
id = newID;
strcpy(text, newText);
room = newRoom;
}
RoomOption::~RoomOption(void)
{
}
void RoomOption::setRoom(shared_ptr<Room> newRoom){
room = newRoom;
}
shared_ptr<Room> RoomOption::getRoom(){
return room;
}
This code compiles without error at /W4 /WX with VS 2010:
#include <memory>
struct Room {};
class Option {
public:
std::shared_ptr<Room> getRoom();
};
std::shared_ptr<Room> Option::getRoom(){
std::shared_ptr<Room> room(new Room());
return room;
}
int main() {
Option opt;
std::shared_ptr<Room> room = opt.getRoom();
return 0;
}
What are you doing differently?
Is Room declared at the point where it's used in the getRoom() call in Option.hpp?
Have you tried removing the () from new Room() in case you're somehow getting hit by the most vexing parse, possibly in other code we can't see??