LNK2019 when instantiating an object c++ on Qtcreator

时间:2017-04-10 00:09:25

标签: c++ constructor qt-creator instantiation lnk2019

I am a beginner in OO so this issue might be very basic.

I have been getting the LNK2019 when I try to instantiate an object that I have defined in another header file:

main.obj:-1: error: LNK2019: unresolved external symbol "public: virtual __cdecl Ship::~Ship(void)" (??1Ship@@UEAA@XZ) referenced in function main

I am using Qtcreator on Windows with msvc2015 as my compiler.

Okay, first I have a GameObjects Class:

#ifndef GAMEOBJECTS_H
#define GAMEOBJECTS_H
#include "Vec4.h"


#include <cmath>
class GameObjects
{


public:
    bool m_isDead;
    bool m_isVisible;
    int m_velocity;
    Vec4 m_position;

    virtual ~GameObjects(){;}
    GameObjects():
        m_isDead(false),
        m_isVisible(true),
        m_velocity(1),
        m_position(1.0f,1.0f,1.0f,1.0f){;}


    GameObjects(bool _isDead, bool _isVisible):
                m_isDead(_isDead),
                m_isVisible(_isVisible),
                m_velocity(1),
                m_position(1.0f,1.0f,1.0f,1.0f)
                {;}

    GameObjects(int _v,float _x,float _y,float _z):
                m_isDead(false),
                m_isVisible(true),
                m_velocity(_v),
                m_position(_x,_y,_z,1.0f){;}

    void hideToggle();
    void killToggle();



};
#endif // GAMEOBJECTS_H

Then I have a derived class called Ship:

#ifndef SHIP_H
#define SHIP_H
#include "GameObjects.h"
#ifdef WIN32
  #include <Windows.h> // must be before gl.h include
#endif

#if defined (__linux__) || defined (WIN32)
    #include <GL/gl.h>
#endif
#ifdef __APPLE__
    #include <OpenGL/gl.h>
#endif

class Ship : public GameObjects
{
public:

    Ship():
        m_fireRate(1){;}

    ~Ship();
    void drawMe();
private:
    int m_fireRate;
};

#endif // SHIP_H

Ship.cpp:

#include "Ship.h"


void Ship::drawMe()
{
  //Drawing using OpenGL

    glEnd();
}

And my main: (I will only show the "#include" and the error line)

/// under mac __APPLE__ is defined by the compiler so we can check
/// this to get the correct includes for OpenGL
#ifdef WIN32
  #include <Windows.h>
#endif

#if defined (__linux__) || defined (WIN32)
  #include <GL/gl.h>
#endif
#ifdef __APPLE__
  #include <OpenGL/gl.h>
#endif

#include <iostream>
#include <cstdlib>
#include "GameObjects.h"
#include "Ship.h"
#include "Camera.h"
#include "SDLOpenGL.h"

#undef main

// function to init the basic OpenGL scene for this demo
void initOpenGL();
// function to render our scene.
void draw(Ship &_player);



int main(int argc, char *argv[])
{
   //...
    Ship player; //Here, whenever instantiate with a default ctor, LNK2019

//....
    return EXIT_SUCCESS;
}

Here is my .pro file:

# we are going to build an app
TEMPLATE=app
CONFIG+=c++11
# qt 5 wants this may cause errors with 4
isEqual(QT_MAJOR_VERSION, 5) {cache() }
QT += opengl
QT -= corex`
TARGET=Asteroids_PPP
OBJECTS_DIR=$$PWD/obj

HEADERS += \
    include/SDLOpenGL.h \
    include/GameObjects.h \
    include/Mat4.h \
    include/Vec4.h \
    include/Camera.h \
    include/DrawThings.h \
    include/CharaControls.h \
    include/Ship.h \
    include/Asteroids.h


SOURCES=$$PWD/src/main.cpp \
    src/SDLOpenGL.cpp \
    src/Mat4.cpp \
    src/Vec4.cpp \
    src/Camera.cpp \
    src/GameObjects.cpp \
    src/DrawThings.cpp \
    src/CharaControls.cpp \
    src/Ship.cpp \
    src/Asteroids.cpp


CONFIG-=app_bundle

INCLUDEPATH +=include
#INCLUDEPATH +=../GLFunctionsLib/include
#LIBS+=-L$$PWD/../GLFunctionsLib/lib -lGLFunctionsLib
macx:QMAKE_CXXFLAGS+= -arch x86_64

!win32 :{
QMAKE_CXXFLAGS += $$system(sdl2-config --cflags)
LIBS+=$$system(sdl2-config --libs)
}

!win32:LIBS+=-L/usr/local/lib
macx:LIBS+=-framework OpenGL


win32 : {
DEFINES+=_USE_MATH_DEFINES
INCLUDEPATH +=c:/SDL2/include
LIBS +="-LC:\SDL2\lib\x64" -lSDL2
LIBS+=-lopengl32
CONFIG+=console

}

I have tried cleaning->run qmake->rebuild, deleting build files and such...

I believe that I have implemented the constructors necessary for the instantiation or the correct addresses for the linkers...but clearly not

So I would like to ask you guys since I'm still a newbie...(first question on here)

Thanks in advance!

2 个答案:

答案 0 :(得分:0)

You didn't define the destructor of Ship.

答案 1 :(得分:0)

You haven't implemented the destructor.

add:

Ship::~Ship()
{
}
相关问题