Failed to compile c++ code undefined reference to `vtable - c++

I am trying to create a stomp client that process messages from the server.
in order to do so im using the Visitor-Visited client.
i keep getting "undefined reference to `vtable for StompConnected'
(and every class that extends StompServerFrame)
this is the code:
///////////// StompFrame.h ///////////////
#ifndef STOMPFRAME_H_
#define STOMPFRAME_H_
#include <string>
#include <sstream>
#include <map>
using namespace std;
class StompFrame{
private:
const string _command;
protected:
map<string, string> _headers;
string _content;
StompFrame(string command);
public:
static const char END_OF_MESSAGE = 0;
string toString() const;
void addHeader(string header, string value);
bool getHeader(string header, string& value);
void setContent(string content);
~StompFrame();
};
#endif
//////////// StompServerFrame.h //////////////////
#ifndef STOMPSERVERFRAME_H_
#define STOMPSERVERFRAME_H_
#include "StompFrame.h"
#include "MessageProcessor.h"
class MessageProcessor;
class StompServerFrame: public StompFrame{
protected:
StompServerFrame(string command);
public:
virtual ~StompServerFrame();
virtual void accept(MessageProcessor& processor) = 0;
};
#endif
////////////// StompConnected.h //////////////////
#ifndef STOMPCONNECTED_H_
#define STOMPCONNECTED_H_
#include "StompServerFrame.h"
class StompConnected: public StompServerFrame{
private:
string _version;
public:
static const string COMMAND_NAME;
StompConnected(string version);
virtual ~StompConnected();
virtual void accept(MessageProcessor& processor);
};
#endif
#ifndef MESSAGEPROCESSOR_H_
#define MESSAGEPROCESSOR_H_
class StompServerFrame;
class StompConnected;
class StompError;
class StompReceipt;
class StompMessage;
class MessageProcessor{
public:
void processMessage(StompServerFrame* frame);
void processMessage(StompConnected* frame);
void processMessage(StompError* frame);
void processMessage(StompReceipt* frame);
void processMessage(StompMessage* frame);
};
#endif
////////////////////////// StompFrame.cpp ///////////////////////////////
#include "../include/StompFrame.h"
#include <iostream>
#include <string>
using namespace std;
StompFrame::StompFrame(string command):_command(command), _headers(), _content(){}
StompFrame::~StompFrame(){}
string StompFrame::toString() const{
stringstream stream;
stream<<_command<<endl;
for(map<string, string>::const_iterator iterator = _headers.begin(); iterator != _headers.end(); iterator++){
string header = (*iterator).first;
string value = (*iterator).second;
stream<<header<<":"<<value<<endl;
}
stream<<endl;
stream<<_content;
if (_content != ""){
stream<<endl;
}
stream<<END_OF_MESSAGE;
return stream.str();
}
void StompFrame::addHeader(string header, string value){
_headers[header] = value;
}
bool StompFrame::getHeader(string header, string& value){
map<string, string>::iterator iterator = _headers.find(header);
if (iterator == _headers.end()){
return false;
}
value = (*iterator).second;
return true;
}
void StompFrame::setContent(string content){
_content = content;
}
///////////// StompServerFrame.cpp ////////////////
#include "../include/StompServerFrame.h"
StompServerFrame::StompServerFrame(string command):StompFrame(command){}
StompServerFrame::~StompServerFrame(){}
///////////////// StompConnected.cpp ////////////////////
#include "../include/StompConnected.h"
StompConnected::StompConnected(string version): StompServerFrame(StompConnected::COMMAND_NAME),
_version("version"){
_headers[_version] = version;
}
void StompConnected::accept(MessageProcessor& processor){
processor.processMessage(this);
}
const string StompConnected::COMMAND_NAME = "CONNECTED";
////////////////////// MessageProcessor.cpp //////////////////
#include "../include/MessageProcessor.h"
#include "../include/StompServerFrame.h"
#include "../include/StompConnected.h"
#include "../include/StompError.h"
#include "../include/StompReceipt.h"
#include "../include/StompMessage.h"
void MessageProcessor::processMessage(StompServerFrame* frame){
frame->accept(*this);
}
/////////////////////////////////////////////////////////////////
please advice me
thanks!

You're missing the implementation for
virtual ~StompConnected();

Related

C++ Class inheritance in different files

I'm trying to learn Inheritance mechanism in C++, I have made a Bancnote(Bills) class, and I want to make a class Card inheriting all the functions and variables from Class Bancnote.
And I get this type of error :
include\Card.h|6|error: expected class-name before '{' token|
BANCNOTE.H
#ifndef BANCNOTE_H
#define BANCNOTE_H
#include <iostream>
#include "Card.h"
using namespace std;
class Bancnote
{
public:
Bancnote();
Bancnote(string, int ,int ,int );
~Bancnote( );
int getsumacash( );
void setsumacash( int );
int getsumaplata( );
void setsumaplata( int );
int getrest( );
void setrest( int );
string getnume( );
void setnume( string );
void ToString();
protected:
private:
string nume;
int sumacash;
int rest;
static int sumaplata;
};
#endif // BANCNOTE_H
BANCNOTE.CPP
#include <iostream>
#include "Bancnote.h"
#include "Card.h"
using namespace std;
int Bancnote::sumaplata=0;
Bancnote::Bancnote(string _nume,int _sumacash,int _rest, int _sumaplata )
{
this->nume=_nume;
this->sumacash=_sumacash;
this->rest=_rest;
this->sumaplata=_sumaplata;
}
Bancnote::Bancnote()
{
this->nume="";
this->sumacash=0;
this->rest=0;
this->sumaplata=0;
}
Bancnote::~Bancnote()
{
cout<<"Obiectul"<<"->" <<this->nume<<"<-"<<"a fost sters cu succes";
}
string Bancnote::getnume()
{
return nume;
}
void Bancnote::setnume(string _nume)
{
this->nume=_nume;
}
int Bancnote::getsumacash()
{
return sumacash;
}
void Bancnote::setsumacash(int _sumacash)
{
this->sumacash=_sumacash;
}
int Bancnote::getsumaplata()
{
return sumaplata;
}
void Bancnote::setsumaplata(int _sumaplata)
{
this->sumaplata=_sumaplata;
}
int Bancnote::getrest()
{
return rest;
}
void Bancnote::setrest(int _rest)
{
this->rest=_rest;
}
void Bancnote::ToString()
{
cout<< "-----"<<getnume()<< "-----"<<endl;
cout<<"Suma Cash: "<<this->getsumacash()<<endl;
cout<<"Suma spre plata: "<<this->getsumaplata()<<endl;
cout<<"Restul:"<<this->getrest()<<endl;
}
CARD.H
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
class Card: public Bancnote
{
public:
Card();
virtual ~Card();
protected:
private:
};
#endif // CARD_H
You have messed up the includes. What you have is more or less this:
Bancnote.h:
#ifndef BANCNOTE_H
#define BANCNOTE_H
#include "Card.h" // remove this
struct Bancnote {};
#endif
Card.h
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
struct Card : Bancnote {}; // Bancnote is not yet declared
// when compiler reaches here
#endif
When in main you include Bancnote.h then this header includes Card.h so you try to declare Card before Bancnote is declared. Actually Bancnote does not need the definition of Card, so simply removing the include should fix it.
PS: there are other issues (see comments below your question). Most importantly it is not clear why a Card is a Bancnote. Second, never put a using namespace std; inside a header! (see here why)

error: No matching function for call to class object

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)
{
// ...
}

How to pass vector as reference in C++?

I want to pass my vector "myStaffs" from Team Class by reference to Manage Class because I want to manipulate the data of staffs and sort them. How do I pass it by reference?
Header Staff
#ifndef STAFF_H
#define STAFF_H
#include <vector>
#include <cstdlib>
#include <ctime>
#include <string>
class Staff
{
public:
Staff();
Staff(int, int, int, int);
~Staff();
static int genRanNum(int);
static Staff* createStaff(int);
const int getSize();
private:
int staSkills1,staSkills2,staSkills3;
int staId;
//int staDeptAsigned;
//std::string staName;
};
#endif
CPP Staff
#include "Staff.h"
#include <iostream>
using namespace std;
Staff::Staff():
staId(0),
staSkills1(0),
staSkills2(0),
staSkills3(0)
{
}
Staff::Staff(int id, int s1, int s2, int s3):
staId(id),
staSkills1(s1),
staSkills2(s2),
staSkills3(s3)
{
}
Staff *Staff::createStaff(int s)
{
Staff *staff = new Staff();
staff->staId = s;
staff->staSkills1 = genRanNum(10);
staff->staSkills2 = genRanNum(10);
staff->staSkills3 = genRanNum(10);
return staff;
}
int Staff::genRanNum(int num)
{
return 1+(rand()%num);
}
Staff::~Staff()
{
}
Header Team
#ifndef TEAM_H
#define TEAM_H
#include "Staff.h"
#include <vector>
#include <iostream>
using std::vector;
class Team: public Staff
{
public:
Team();
~Team();
private:
vector<Staff *> myStaffs;
};
#endif // TEAM_H
CPP Team
#include "Team.h"
const int SIZE = 30;
Team::Team():
myStaffs(SIZE)
{
for(int iStaff = 0; iStaff <= SIZE; iStaff++)
{
myStaffs[iStaff] = createStaff(iStaff);
}
}
Team::~Team()
{
}
Header Manage
#ifndef OPTIONS_H
#define OPTIONS_H
#include "Team.h"
#include <vector>
#include <iostream>
using std::vector;
class Manage
{
public:
Manage();
~Manage();
private:
// vector
};
CPP Manage
#include "Manage.h"
Manage::Manage()
{
}
Manage::~Manage()
{
}
#endif
Its simple you pass it as you would pass any other object by reference
int sortEmployee(std::vect<Staff *> &staffList> {
// ... code to sort Employee
}
and you can call it like below
vector<Staff *> myStaffs
result = sortEmployee(myStaffs);
It's as simple as
#include <vector>
void myVectorManglingFun(std::vector<Staff *> &myStaff) //notice the &
{
//do something here
}
If you don't need to modify the vector, then always use a const reference.
void myVectorReadingFun(const std::vector<Staff *> &myStaff)

undefined reference to vtable when using interface

I've looked around, and I can't quite figure out where I'm going wrong, as I seem to be following the correct convention when using interfaces, but perhaps I'm overlooking something. The exact error I'm getting is:
undefined reference to `vtable for Icommand'
I've only just begun to seperate my classes and class declarations into separate header files, so perhaps I'm missing a preprocessor directive somewhere.
main.cpp:
#include <iostream>
#include <string>
#include <cstdlib>
#include "Icommand.h"
#include "Command.h"
using namespace std;
void pause();
int main(){
Icommand *run = new Command("TEST");
cout << run->getCommand() << endl;
delete run;
pause();
}
void pause(){
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());
cin.get();
}
Icommand.h:
#ifndef ICOMMAND_H
#define ICOMMAND_H
#include <string>
#include <vector>
class Icommand
{
private:
public:
Icommand(){}
virtual ~Icommand(){}
virtual bool run(std::string object1) = 0;
virtual bool run(std::string object1, std::string object2) = 0;
virtual std::string getCommand() const;
};
#endif // ICOMMAND_H
Command.h:
#ifndef COMMAND_H
#define COMMAND_H
#include <string>
#include <vector>
#include "Icommand.h"
class Command : public Icommand {
private:
std::string command;
std::vector<std::string> synonymns;
Command(); // private so class much be instantiated with a command
public:
Command(std::string command) : command(command){}
~Command(){}
bool run(std::string object1);
bool run(std::string object1, std::string object2);
std::string getCommand() const;
};
#endif // COMMAND_H
Command.cpp:
#include <string>
#include <vector>
#include "Command.h"
bool Command::run(std::string object1){
return false;
}
bool Command::run(std::string object1, std::string object2){
return false;
}
std::string Command::getCommand() const {return command;}
In Icommand.h, replace
virtual std::string getCommand() const;
with
virtual std::string getCommand() const = 0;
to make it pure virtual. Then the compiler can generate a vtable for Icommand. Alternatively, implement Icommand::getCommand.

How exactly serialization works in C++?

Here are my code files:
main.cpp
#include <iostream>
#include <fstream>
#include "Car.h"
#include "Engine.h"
using namespace std;
int main() {
Car* car = new Car(1984);
/* do something here */
delete car;
return 0;
}
Car.h
#ifndef CAR_H
#define CAR_H
#include <iostream>
using namespace std;
#include "Engine.h"
class Car {
public:
Car(int);
virtual ~Car();
void serialize(ostream& s) {
engine.serialize(s);
s << ' ' << yearModel;
}
void unserialize(istream& s) {
engine.unserialize(s);
s >> yearModel;
}
private:
Engine engine;
int yearModel;
};
#endif /* CAR_H */
Car.cpp
#include "Car.h"
Car::Car(int year) {
yearModel = year;
}
Car::~Car() {
}
Engine.h
#ifndef ENGINE_H
#define ENGINE_H
#include <iostream>
using namespace std;
class Engine {
public:
Engine();
virtual ~Engine();
void serialize(ostream& s) {
s << ' ' << engineType;
}
void unserialize(istream& s) {
s >> engineType;
}
private:
int engineType;
};
#endif /* ENGINE_H */
Engine.cpp
#include "Engine.h"
Engine::Engine() {
engineType = 1;
}
Engine::~Engine() {
}
What I want to do in the main.cpp is to save the created Car object to file.txt and later read it from there. How does that exactly work? For example: how do I call the serialization function in Car class?
I'm sorry if I sound like a noob, but this whole serialization thing is pretty new to me.
Edit: Code compiles now when I added 'void' in front of all serialize- and unserialize-functions.
This has nothing to do with serialization: a function needs a return type, even if it is void. So this is wrong:
serialize(ostream& s) // look, no return type.
You probably need to either return void,
void serialize(ostream& s) { /* code as before */ }
or return the stream by reference to allow for chaining:
ostream& serialize(ostream& s) {
return s << ' ' << engineType;
}