未解析的外部符号,c++

unresolved external symbol, C++

本文关键字:符号 c++ 外部      更新时间:2023-10-16

当试图建立一个关于未解决的外部符号的项目时,我得到了一个错误,但是我找不到我的问题在哪里,有人有任何想法吗?由于

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:worksecond yearc++ breakout completebreakout completetball.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:WorkSecond yearC++ Breakout CompleteDebugBreakout 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,并从构造函数中删除它的初始化。