[英]unresolved external symbol, C++
我在尝试构建一个关于未解析的外部符号的项目时遇到错误,但是我无法找到问题所在,任何人都有任何想法? 谢谢
Tball.cpp
#include "Tball.h"
#include <Windows.h>
using namespace std;
Tball::Tball(){
Position = TVector(70,0,70);
Verlocity = TVector(1,0,1);
}
Tball.h
#ifndef Tball_h
#define Tball_h
#include <iostream>
#include "mathex.h"
#include "tvector.h"
class Tball
{
public:
static TVector Position;
static TVector Verlocity;
Tball();
static void DrawBall(float x, float y, float z);
static TVector MoveBall();
static void init();
static int loadbitmap(char *filename);
static void SurfaceNormalVector();
static double Tball::collision();
static void Tball::pointz();
};
#endif
错误:
1>------ Build started: Project: Breakout Complete, Configuration: Debug Win32 ------
1> Tball.cpp
1> Generating Code...
1>g:\work\second year\c++ breakout complete\breakout complete\tball.cpp(59): warning C4715: 'Tball::MoveBall' : not all control paths return a value
1> Skipping... (no relevant changes detected)
1> Tvector.cpp
1> TdisplayImp.cpp
1> TBricks.cpp
1>Tball.obj : error LNK2001: unresolved external symbol "public: static class TVector Tball::Verlocity" (?Verlocity@Tball@@2VTVector@@A)
1>Tball.obj : error LNK2001: unresolved external symbol "public: static class TVector Tball::Position" (?Position@Tball@@2VTVector@@A)
1>G:\Work\Second year\C++ Breakout Complete\Debug\Breakout Complete.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我尝试过的任何东西似乎都对我有用。
把它放到你的cpp中:
TVector Tball::Position(/* contructor params */);
TVector Tball::Verlocity(/* contructor params */);
这使得这些成员静态变量成为“空间”。
我没看到的定义
static TVector Position;
static TVector Verlocity;
这只是声明。 您需要在一个.ccp文件中使用一些构造函数(可以使用默认的构造函数)来定义它。 静态成员不是每个对象的一部分,需要在对象构造函数以外的位置创建。
在你的情况下:
Tball.cpp
#include "Tball.h"
#include <Windows.h> // Why?
//using namespace std; Why??
TVector Tball::Position(70,0,70);
TVector Tball::Verlocity(1,0,1);
Tball::Tball(){}
最有可能(因为没有发布错误)你错过了定义
static TVector Position;
static TVector Verlocity;
要修复此添加
Tball::Position(70,0,70);
Tball::Verlocity(1,0,1);
到你的.cpp,并从构造函数中删除它的初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.