Undefined reference to class constructor - c++

Here's my code:
menuState.hpp
#ifndef MENU_STATE_HPP
#define MENU_STATE_HPP
#include "state.hpp"
#include <SFML/Graphics.hpp>
class MenuState : public State
{
public:
MenuState();
static void create(StateListener* Parent, const std::string name)
{
MenuState* myState = new MenuState();
myState->parent = Parent;
Parent->manageState(name, myState);
}
void enter();
void exit();
void resume();
private:
};
#endif // MENU_STATE_HPP
I'm getting an undefined reference to the constructor when I do MenuState* myState = new MenuState(); and I'm not sure why because MenuState::MenuState() comes before the create function in the class declaration.
EDIT: I'm also getting the same error to all my sfml functions.
Here's the exact build messages: http://pastebin.com/e819FhPj
I do have the sfml libraries linked and the header path set in my compilers search directories.

It is always best to show the exact text of the error, but my educated guess is, you are getting a linker error, not a compiler error. Have you actually implemented the constructor anywhere? I bet you haven't.

I'm not an experience C++ developer but it seems like MenuState constructor is declared but not defined.
Replace MenuState(); with MenuState(){} should fix the error.

Related

C++ template class with static member - Undefined reference, whereas my static member seems declared and initialized

I read a lot of topics about templates classes, on stackoverflow and other websites, but all I understood and tried did not work, so please let me expose you my code, if you could tell me what I am doing wrong it could be very nice.
NB: I know about bad practice of using Singleton, but please it is not the talk, please assume it is just an academic exercice to work with template classes.
Library project :
// fsingleton.h
#include <QObject>
#if defined(LIB_LIBRARY)
# define FCORE_EXPORT Q_DECL_EXPORT
#else
# define FCORE_EXPORT Q_DECL_IMPORT
#endif
template<typename T>
class FCORE_EXPORT FSingleton
{
public:
static T *instance();
static void kill();
protected:
static T *m_instance;
FSingleton();
virtual ~FSingleton();
private:
Q_DISABLE_COPY(FSingleton)
};
template<typename T>
T *FSingleton<T>::m_instance = Q_NULLPTR;
template<typename T>
T *FSingleton<T>::instance()
{
if (m_instance == Q_NULLPTR) {
m_instance = new T();
}
return m_instance;
}
template<typename T>
void FSingleton<T>::kill()
{
delete m_instance;
m_instance = Q_NULLPTR;
}
template<typename T>
FSingleton<T>::FSingleton() {}
template<typename T>
FSingleton<T>::~FSingleton() {}
Here I am in a library project, so LIB_LIBRARY is defined when building, so we have Q_DECL_EXPORT, and the library is built sucessfully.
Executable project :
// testsingleton.h
#include "fsingleton.h"
class TestSingleton : public FSingleton<TestSingleton>
{
public:
TestSingleton();
virtual ~TestSingleton();
};
// testsingleton.cpp
#include "testsingleton.h"
TestSingleton::TestSingleton() {}
TestSingleton::~TestSingleton() {}
// tst_utfsingleton.cpp - it is a unit test source file following Qt Test template, I write it as a simple main.cpp function to simplify the example
#include "testsingleton.h"
void main()
{
TestSingleton* ptr1 = TestSingleton::instance();
}
This executable fails to compile on linking, and gives the following output :
../debug/obj/tst_utfsingleton.o: In function `FSingleton<TestSingleton>::instance()':
../lib/fsingleton.h:63: undefined reference to `__imp__ZN10FSingletonI13TestSingletonE10m_instanceE'
../lib/fsingleton.h:64: undefined reference to `__imp__ZN10FSingletonI13TestSingletonE10m_instanceE'
../lib/fsingleton.h:68: undefined reference to `__imp__ZN10FSingletonI13TestSingletonE10m_instanceE'
../debug/obj/testsingleton.o: In function `FSingleton<TestSingleton>::FSingleton()':
../lib/fsingleton.h:79: undefined reference to `__imp__ZTV10FSingletonI13TestSingletonE'
../debug/obj/testsingleton.o: In function `FSingleton<TestSingleton>::~FSingleton()':
../lib/fsingleton.h:88: undefined reference to `__imp__ZTV10FSingletonI13TestSingletonE'
../debug/obj/testsingleton.o: In function `FSingleton<TestSingleton>::~FSingleton()':
../lib/fsingleton.h:88: undefined reference to `__imp__ZTV10FSingletonI13TestSingletonE'
collect2.exe: error: ld returned 1 exit status
I dot not realy understand why all this undefined references.
The m_instance member is declared into the FSingleton class and initialized just after the class definition, in the fsingleton.h. I do not see what I am doing wrong, could you help me please ?
Regards
Ok so due to the answer of #drescherjm the issue is solved.
What I understand it is that template classes are generated during compile time, depending on the needed instantiations.
So, in my case I mark the template class FSingleton as __declspec(dllexport) when building library, but there is no instantiation in my library, so nothing is exported.
Then, compiling the executable, the class is marked as __declspec(dllimport) but there is consequently nothing to import, because nothing was built during library build, because the instantiation FSingleton<TestSIngleton> is done into the executable source code.
Do not hesitate to fix my words if I said something wrong ^^
Thanks again #drescherjm

Extra qualification on member, bezier curve OPENGL

I got error say [Error] extra qualification 'bezierCurve::' on member 'calCurve' [-fpermissive]. Could anyone explain to me why this happen? I've been looking for answer, but the I cannot solve the problem.
#ifndef _BEZIERCURVE_H_
#define _BEZIERCURVE_H_
#include "bezier.h"
class bezierCurve : public bezier{
private:
int numPoints;
float **controlPoints;
float **curvePoints;
void bezierCurve::calCurve(); //and error here
public:
bezierCurve(int numPoints, float *points[3]);
void bezierCurve::setShowPoints(bool showControlPoints); // I got the error here
virtual void draw();
~bezierCurve();
};
#endif
This is an error because it is not valid C++ syntax. The elephant in the room is that VisualC++ has historically not considered this an error. But GCC has since around version 4.
Simply removing the extra qualifications fixes the code.
For example:
#ifndef __ANIMAL_H__
#define __ANIMAL_H__
class Animal
{
...
int Animal::getLegCount();
bool Animal::hasFur();
};
#endif
Is not correct, member must be defined without the Classname:: prefix:
#ifndef __ANIMAL_H__
#define __ANIMAL_H__
class Animal
{
...
int getLegCount();
bool hasFur();
};
#endif
You are confusing declarations and definitions. When you declare a member function, it's in the context of the class already so classname:: is redundant. When you define the body of a function outside of the class, you need the classname:: so that the compiler knows which class it belongs to.
class bezierCurve : public bezier{
void setShowPoints(bool showControlPoints);
};
void bezierCurve::setShowPoints(bool showControlPoints) {
}
this is what output i get. suppose it's not like this.
#kingsley, this shown the output when i'm running the codes after I remove _s from sscanf_s().

send reference qplaintextedit to C++ constructor class

I need to send reference a QPlainTextEdit to my C++ class Analizador for add lines to QPlainTextEdit from my class. I add the include <QPlainTextEdit> to the class, create the QPlainTextEdit from the graphic interface and call the constructor function just like that
Analizador *anal=new Analizador(ui->textProgres);
the constructor function is:
Analizador(QPlainTextEdit* text);
the compiler throw the error :
mainwindow.cpp:23: error: undefined reference to
`Analizador::Analizador(QPlainTextEdit*)'
so I guess the error is because I'm not sending a pointer to the constructor function but I don't know how to access the pointers of QPlainTextEdit
PS. I'm new in Qt and C++
In this case, compiler complains that it cannot find the definition of Analizador constructor when it tries to link your application.
Make sure you have written the definition of Analizador::Analizador(QPlainTextEdit*) constructor.
If you have written the constructor but still you gets this issue, The cpp file where your constructor exists may not have got compiled. If you are using QtCreator, try Build -> Run QMake and then Build -> Rebuild All
You can try this workaround.
#ifndef ANALIZADOR_H
#define ANALIZADOR_H
#include <QPlainTextEdit>
class Analizador
{
public:
Analizador(QPlainTextEdit *text)
{
plainTextEdit = text;
}
void addLines(QString line)
{
plainTextEdit->appendPlainText(line);
}
private:
QPlainTextEdit *plainTextEdit;
};
#endif // ANALIZADOR_H
And use this class like this.
analizador = new Analizador(ui->plainTextEdit);
analizador->addLines("Hello");
analizador->addLines("World");

c++ code blocks error

First: My English is not that good yours is. Excuse me.
I'm using Ubuntu (I don't know if this is important) and I had issues with Code::Blocks since I started to use it. But I fixed them by re-opening the program. But now, I get a really crazy error when compiling the code. I included a file just like usual:
#include "GameObjectUtility.h"
and I used the class "GameObjectUtility" to declare a member object, just like this:
class GameObject
{
std::vector<GameObjectUtility> uts;
// Error here:
// GameObjectUtility was not declared in this scope
}
So, is this my fault or is there something buggy with Code::Blocks?
And, additionally, is there a way of saying to the Linker: First execute this file and then the other?
Thank you for your answers!
EDIT: .h and .ccp file GameObjectUtility:
So this is GameObjectUtility.h:
#ifndef GAMEOBJECTUTILITY_H
#define GAMEOBJECTUTILITY_H
#include <string>
#include "Collision.h"
class GameObjectUtility
{
public:
GameObjectUtility();
virtual ~GameObjectUtility();
virtual void Update() = 0;
virtual void LateUpdate() = 0;
virtual void FixedUpdate() = 0;
static void SendMsg(std::string msg);
protected:
private:
virtual void GetMsg(std::string msg) = 0;
};
#endif // GAMEOBJECTUTILITY_H
And in GameObjectUtility.cpp are just two empty definitions of constructor and destructor
Since class GameObjectUtility is pure virtual, you cannot instantiate it.
You can only store std::vector<GameObjectUtility*> in class GameObject.

error C2504: 'BASECLASS' : base class undefined

I checked out a post similar to this but the linkage was different the issue was never resolved. The problem with mine is that for some reason the linker is expecting there to be a definition for the base class, but the base class is just a interface. Below is the error in it's entirety
c:\users\numerical25\desktop\intro todirectx\godfiles\gxrendermanager\gxrendermanager\gxrendermanager\gxdx.h(2) : error C2504: 'GXRenderer' : base class undefined
Below is the code that shows how the headers link with one another
GXRenderManager.h
#ifndef GXRM
#define GXRM
#include <windows.h>
#include "GXRenderer.h"
#include "GXDX.h"
#include "GXGL.h"
enum GXDEVICE {
DIRECTX,
OPENGL
};
class GXRenderManager {
public:
static int Ignite(GXDEVICE);
private:
static GXRenderer *renderDevice;
};
#endif
at the top of GxRenderManager, there is GXRenderer , windows, GXDX, GXGL headers. I am assuming by including them all in this document. they all link to one another as if they were all in the same document. correct me if I am wrong cause that's how a view headers. Moving on...
GXRenderer.h
class GXRenderer {
public:
virtual void Render() = 0;
virtual void StartUp() = 0;
};
GXGL.h
class GXGL: public GXRenderer {
public:
void Render();
void StartUp();
};
GXDX.h
class GXDX: public GXRenderer {
public:
void Render();
void StartUp();
};
GXGL.cpp and GXDX.cpp respectively
#include "GXGL.h"
void GXGL::Render()
{
}
void GXGL::StartUp()
{
}
//...Next document
#include "GXDX.h"
void GXDX::Render()
{
}
void GXDX::StartUp()
{
}
Not sure whats going on. I think its how I am linking the documents, I am not sure.
The problem is You need to have #include "GXRenderer.h" at the top of both: GXGL.h and also GXDX.h.
The base type must be defined not just declared before defining a derived type.
By the way, the error is a compiling error not linking error.
Edit: About your class type redefinition:
at the top of every header file you should have #pragma once.
The #pragma once directive specifies that the file will be included at most once by the compiler in a build.
You included them all into GXRenderManager.h, meaning that GXRenderManager.h is OK.
But you forgot to include them all into GXGL.cpp and GXDX.cpp. In these .cpp files GXRenderer class is completely unknown.
There are at least two "schools" of #include strategies. One says that header file must include everything that is needed for its own compilation. That would mean that GXGL.h and GXDX.h must include GXRenderer.h. If you followed that strategy, your GXGL.cpp and GXDX.cpp would be OK as they are now.
Another "school" says that header files must not include each other at all, i.e. all inclusions must be done through .cpp files. At first sight one could guess that your GXGL.h and GXDX.h follow that strategy (since you are not including anything into them), but then your GXRenderManager.h looks completely different.
You need to decide which strategy you are trying to follow and follow it. I'd recommend the first one.
I got an error C2504: 'CView' : base class undefined
where CView is not directly my base class from which I am inheriting.
I am inherting mYClass from MScrollView, "for this matter any class which is not actual Base Class is what the point is to be noted down here"
but the error is the C2504. When I have included it in the header where this problem is arising, this problem is resolved.
#include "stdafx.h"
where stdafx.h has #include which contains all the basic class defined...hope this answer resolves everyone who are facing this issue.