LNK2019 在 Qtcreator 上实例化对象 c++ 时
LNK2019 when instantiating an object c++ on Qtcreator
我是 OO 的初学者,所以这个问题可能非常基础。
当我尝试实例化我在另一个头文件中定义的对象时,我得到了 LNK2019:
main.obj:-1: error: LNK2019: unresolved external symbol "public: virtual __cdecl Ship::~Ship(void)" (??1Ship@@UEAA@XZ) referenced in function main
我在 Windows 上使用 Qtcreator,我的编译器是 msvc2015。
好的,首先我有一个游戏对象 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
然后我有一个派生的 class 叫做 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();
}
我的主要内容:(我只会显示“#include”和错误行)
/// 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;
}
这是我的 .pro 文件:
# 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
}
我已经尝试清理->运行 qmake->重建,删除构建文件等...
我相信我已经实现了实例化所需的构造函数或链接器的正确地址...但显然没有
所以我想问问你们,因为我还是个新手...(这里是第一个问题)
提前致谢!
您没有定义 Ship
的析构函数。
你还没有实现析构函数。
添加:
Ship::~Ship()
{
}
我是 OO 的初学者,所以这个问题可能非常基础。
当我尝试实例化我在另一个头文件中定义的对象时,我得到了 LNK2019:
main.obj:-1: error: LNK2019: unresolved external symbol "public: virtual __cdecl Ship::~Ship(void)" (??1Ship@@UEAA@XZ) referenced in function main
我在 Windows 上使用 Qtcreator,我的编译器是 msvc2015。
好的,首先我有一个游戏对象 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
然后我有一个派生的 class 叫做 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();
}
我的主要内容:(我只会显示“#include”和错误行)
/// 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;
}
这是我的 .pro 文件:
# 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
}
我已经尝试清理->运行 qmake->重建,删除构建文件等...
我相信我已经实现了实例化所需的构造函数或链接器的正确地址...但显然没有
所以我想问问你们,因为我还是个新手...(这里是第一个问题)
提前致谢!
您没有定义 Ship
的析构函数。
你还没有实现析构函数。
添加:
Ship::~Ship()
{
}