C++ undefined reference to vtable on compile [duplicate] - c++

This question already has answers here:
Undefined reference to vtable
(21 answers)
Closed 9 years ago.
Why do I keep getting an error on compile with this code?
#ifndef OPERATOR_H
#define OPERATOR_H
#include <cstdlib>
#include <iostream>
#include <string>
#include "Individual.h"
using namespace std;
class Operator
{
public:
Operator();
virtual void execute (Individual* parent);
private:
};
#endif
Then in the cpp file I have
#include <cstdlib>
#include <iostream>
#include <string>
#include "Operator.h"
using namespace std;
Operator::Operator()
{
}
void execute(Individual* parent)
{
}

Define a destructor. The vtable for a class with virtual methods is created in the translation unit that defines the constructor.
In your header, add:
virtual ~Operator();
And in your source file, add:
Operator::~Operator() {
}

Related

#Include causing undeclared identifier error [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 2 months ago.
My code works normally up until the point that I add #include "CharacterBase.h" to a file called ItemBase.h. I am using #pragma once in all of my files, and I cannot figure out why when I add CharacterBase.h it suddenly is filled with errors. "Itembase" undeclared identifier, is the error.
//Filename is :ItemBase
#pragma once
#include <string>
#include "CharacterBase.h"
class ItemBase
{
}
//Filename is ItemConsumable
#pragma once
#include "ItemBase.h"
class ItemConsumable : public ItemBase
{
}
//File name is CharacterBase.h
#pragma once
#include <string>
#include "ItemBase.h"
#include "ItemConsumable.h"
#include <vector>
class CharacterBase
{
public:
}
You just have a circular dependencie.
In C++, if your file “CharacterBase.h” includes “ItemBase.h” then “ItemBase.h” cannot include “CharacterBase.h”. The only way for ItemBase to use CharacterBase is to forward declare CharacterBase, use pointers or references on CharacterBase in the header and finally include “CharacterBase.h” in “ItemBase.cpp”.
File ItemBase.h
//ItemBase.h
#pragma once
#include <string>
//ItemBase.h
#include "CharacterBase.h"
class ItemBase
{
}
File CharacterBase.h
//CharacterBase.h
#pragma once
#include "ItemBase.h"
class ItemBase{} // Forward declaration
class CharacterBase
{
public:
CharacterBase(ItemBase *base);
ItemBase *itemBase;
}
File CharacterBase.cpp
// CharacterBase.cpp
#include ItemBase.h
CharacterBase:CharacterBase(ItemBase *itemBase){// your stuff}

Resolving a circular reference in c++ header files [duplicate]

This question already has an answer here:
C++ Circular Dependency in Header Files
(1 answer)
Closed 6 years ago.
I am encountering a circular include reference error where I wish to use an object of type Deck in CardFactory, and an object of type CardFactory in Deck. Any hints as to how to fix this problem?
//CardFactory.h
#ifndef CARDFACTORY_H
#define CARDFACTORY_H
#include "Deck.h"
#include <string>
using std::string;
class CardFactory {
public:
Deck getDeck();
static CardFactory* getFactory() {
static CardFactory singleton;
return &singleton;
}
};
#endif
//Deck.h
#ifndef DECK_H
#define DECK_H
#include <vector>
#include <iostream>
#include "CardFactory.h"
using std::ostream;
class Deck : public std::vector<Card*> {
friend ostream& operator<<(ostream& os, const Deck& dt);
Card* draw();
Deck(CardFactory* cf);
};
#endif
Forward reference (or forward declaration).
In Deck.h you don't need to #include "CardFactory.h", instead just declare the class.
class CardFactory;
This should work because in Deck class you are only using pointer to class CardFactory

Arduino: Inheritance and arrays of pointer subclasses

This is problem #2 from this previous question:
Inheritance in Arduino Code
Building off of Steven's answer, I do need the array that holds the pointers to persist outside of its scope, which is resulting in some weird behavior.
This is my "Board" class I have so far, that contains multiple child elements:
Board.h:
#ifndef Board_h
#define Board_h
#include <StandardCplusplus.h>
#include <serstream>
#include <string>
#include <vector>
#include <iterator>
#include "Arduino.h"
#include "Marble.h"
#include "Wall.h"
class Board
{
public:
Board();
void draw(double* matrix);
private:
Marble marble;
//std::vector<Actor> children;
Actor* children[2];
};
#endif
Board.cpp:
#include "Arduino.h"
#include "Board.h"
#include <math.h>
#include <iterator>
#include <vector>
Board::Board()
{
}
void Board::create(double* _matrix, int _cols, int _rows) {
Marble *marble = new Marble();
Wall wall;
children[0] = marble;
//children.push_back(marble);
//children.push_back(wall);
}
void Board::draw(double* matrix) {
Serial.println("board draw");
children[0]->speak();
}
In my "loop" function I am calling
board.draw(matrix);
which results in some nutty Serial code being written out.
Clearly I am not understanding the ins and outs of pointers in arrays in classes here.
You need to make Actor::speak virtual, the compiler uses dynamic binding for virtual methods.
class Actor
{
public:
Actor();
virtual void speak(); // virtual
private:
};

Undeclared Identifier vector of pointers to objects

Error: Line 12 of Cell.h: 'Actor' undeclared identifier.
If I try to forward declare above it, it says that there's a redefinition. What do I do?
Actor.h:
#ifndef ACTOR_H
#define ACTOR_H
#include <iostream>
#include <vector>
#include <string>
#include "Cell.h"
using namespace std;
class Actor //Simple class as a test dummy.
{
public:
Actor();
~Actor();
};
#endif
Cell.h:
#include <iostream>
#include <string>
#include <vector>
#include "Actor.h"
#ifndef CELL_H
#define CELL_H
using namespace std;
class Cell // Object to hold Actors.
{
private:
vector <Actor*> test;
public:
Cell();
~Cell();
vector <Actor*> getTest();
void setTest(Actor*);
};
#endif
Cell.cpp:
#include "Cell.h"
#include <vector>
vector<Actor*> Cell::getTest() //These functions also at one point stated that
{ // they were incompatible with the prototype, even
} // when they matched perfectly.
void Cell::setTest(Actor*)
{
}
What else can I do?
Remove the #include "Cell.h" from Actor.h and you're set to go.
In general, prefer forward declarations where you can, and includes where you must. I'd also replace the #include "Actor.h" from Cell.h with a forward declaration: class Actor;.
In the cpp files you can include the headers if you need them.
You have recursive #includes via your mutual references between cell.h and actor.h.
In Cell.h, delete #include <Actor.h>.
In Cell.h, add the line class Actor; just above the definition of class Cell.
In Cell.cpp, you might need to add #include "Actor.h".

unknown type error in C++

What is going on?
#include "MyClass.h"
class MyOtherClass {
public:
MyOtherClass();
~MyOtherClass();
MyClass myVar; //Unknown type Error
};
Suddenly when I include the .h and write that var Xcode gives me tons of errors... and also the unknown type error.
How can it be unknown when the .h is included right there?
Here is the NodeButton.h file which would correspond to the MyClass.h in the example
#pragma once
#include "cinder/Vector.h"
#include "cinder/gl/gl.h"
#include "cinder/gl/Texture.h"
#include "cinder/Color.h"
#include "cinder/ImageIo.h"
#include "cinder/Timeline.h"
#include "cinder/app/AppBasic.h"
#include "cinder/App/App.h"
#include "Node.h"
#include "CursorMano.h"
using namespace ci;
using namespace ci::app;
using namespace std;
using namespace is;
typedef boost::shared_ptr<class NodeButton> NodeButtonRef;
class NodeButton : public Node2D
{
public:
NodeButton (CursorMano *cursor, string imageUrl, bool fadeIn = false, float delay = 0.0f);
virtual ~NodeButton ();
//methods
void update( double elapsed );
void draw();
void setup();
//events
bool mouseMove( ci::app::MouseEvent event );
//vars
CursorMano *mCursor;
gl::Texture mImageTexture;
Anim<float> mAlpha = 1.0f;
bool mSelected = false;
private:
};
And here are the contents of CursorMano.h which would correspond to MyOtherClass.h in the example.
#pragma once
#include <list>
#include <vector>
#include "cinder/app/AppBasic.h"
#include "cinder/qtime/QuickTime.h"
#include "cinder/gl/Texture.h"
#include "cinder/Vector.h"
#include "NodeButton.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class CursorMano {
public:
CursorMano (AppBasic *app);
~CursorMano ();
void mueveMano(Vec2i);
void update();
void draw();
void play(int button);
void reset(int button);
Vec2i mMousePos;
NodeButton mButtonCaller; //this gives the unknow type error
private:
AppBasic *mApp;
gl::Texture mFrameTexture;
qtime::MovieGl mMovie;
int mIdButton;
};
You have a circular dependency of your header files.
NodeButton.h defines NodeButton class which CursorMano.h needs to include so that compiler can see definition for NodeButton but NodeButton.h itself includes CursorMano.h.
You will need to use forward declarations to break this circular dependency.
In NodeButton.h you just use an pointer to CursorMano so You do not need to include the CursorMano.h just forward declare the class after the using namespace declarations.
using namespace std;
using namespace is;
class CursorMano;
It's probably a result of the circular dependency between you two header files (NodeButton includes CursorMano and CursorMano includes NodeButton). Try removing the #include "CursorMano.h" in NodeButton.h and add class CursorMano; before your NodeButton declaration.