未解析的外部符号 - 函数看不到定义[已解决] [英] Unresolved external symbol - function doesn't see the definition [SOLVED]

查看:122
本文介绍了未解析的外部符号 - 函数看不到定义[已解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误LNK2019未解析的外部符号public:__thiscall display :: display(void)(?? 0display @@ QAE @ XZ)在函数public:__thiscall field :: field(void)中引用(?? 0field @ @ QAE @ XZ)SnakeByteWin C:\ Users\iptamas\source \repos\SnakeByteWin \ SnakeByteWin \ field.obj 1



错误LNK2019未解析的外部符号public:void __thiscall display :: displayFullGame(struct HWND __ *,class field *,int)(?displayFullGame @ display @@ QAEXPAUHWND __ @@ PAVfield @@ H @ Z)在函数long __stdcall WindowProci中引用HWND__ *,unsigned int,unsigned int,long)(?WindowProci @@ YGJPAUHWND __ @@ IIJ @ Z)SnakeByteWin C:\ Users\iptamas\source \repos\SnakeByteWin \SnakeByteWin \windowproc.obj 1



大家好,

这是我不断得到的错误。我有一个带有字段类的 field.h ,带有显示类的 display.h ,以及带有 windowproc.h和.cpp 的字段。 windowproc函数。我猜windowproc看不到显示类的定义,但我不知道为什么。

field.h ================== =======================

 # ifndef FIELD_H_INCLUDED 
#define FIELD_H_INCLUDED

#include defines.h
#include matrix.h
#include bites.h
#include snake.h
#include display.h

class field {
int 得分了;
int hiScore;
int 级别;
int nofBites;
int 速度;
int scoreCounter;
bool isGameOn;
bites * bitesPr;
snake * snakePr;
矩阵* matrixPr;
display * displayPr;
public
field();
~field();
void initLevel();
void initGame();
int getScore();



void setScore(int); ...... ..等等....

display.h ============================== ===========

  #pragma once 
#include windowproc .h
class 字段;
class display {
HBITMAP hBitmapWall;
HBITMAP hBitmapNoWall;
HBITMAP hBitmapBites;
HBITMAP hBitmapSnake;
HBITMAP hBitmapStartButton;
RECT scoreRect;
public
display();
BOOL CALLBACK displayChildWindows(HWND hwnd,LPARAM lParam);
void displayFullGame(HWND hwnd,field *, int );
RECT getScoreRect();
HBITMAP getBitmapWall();
HBITMAP getBitmapNoWall();
HBITMAP getBitmapBites();
HBITMAP getBitmapSnake();
HBITMAP getBitmapStartButton();
};



windowproc.cpp ========================== =====================

  #include     field.h 

LRESULT CALLBACK WindowProci(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam){
LRESULT r = 0 ;
field * fg =(field *)GetWindowLongPtr(hwnd,GWLP_USERDATA);

switch (uMsg){
case WM_NCCREATE:{
SetWindowLongPtr(hwnd,GWLP_USERDATA,(LONG)(((CREATESTRUCT *)lParam) - > lpCreateParams));
r = DefWindowProc(hwnd,uMsg,wParam,lParam);
break ;

}
......其他案例 ' s ......


case WM_TIMER:{// =============== ========== WM_TIMER消息=====================
//显示d;
if(!fg-> getNofBites())PostMessage(hwnd,STARTNEWLEVEL,0,0); // fullGame事件+显示fullGame
fg-> isSnakeAlive();
fg-> moveSnake();
fg-> decreaseScoreCounter();
if(fg-> isBitesEaten()){
fg-> generatePositionBites();
fg-> nofBitesDown();
fg-> setScore(fg-> getScore()+ fg-> getScoreCounter());
fg-> setScoreCounter(SCORECOUNTER + fg-> getLevel()* LEVELER);
fg-> getSnakePr() - > setCounter(COUNTER);
fg-> getDisplayPr() - > displayFullGame(hwnd,fg,DISPLAYSCORE);
}
fg-> getDisplayPr() - > displayFullGame(hwnd,fg,DISPLAYBITES);
fg-> getDisplayPr() - > displayFullGame(hwnd,fg,DISPLAYSNAKE);
if(fg-> getSnakePr() - > getAlive())PostMessage(hwnd,GAMEOVER,0,0);
休息;
} //案例WM_TIMER)





我尝试了什么:



我的假设是'包含'不正确。我尝试了几乎所有的变化,但是当我设法避免所有'双重包含'时,我仍然得到相同的消息。

我也试图在windowproc函数中创建显示类的实例,但它也没有用。

有什么想法吗?谢谢。

解决方案

引用:

我想windowproc看不到定义显示类,但我不知道为什么。

确实是这个原因。编译器在 display.h 中看到 display 类的声明,但缺少定义(实现)。我希望你还有一个 display.cpp 文件。如果是这样,那也必须编译,并且必须链接生成的目标文件(例如,只需将文件添加到项目中)。



根据第一条错误消息你有一个 field.obj 文件,它可能是通过编译 field.cpp 创建的。你需要以类似的方式处理 display 类。


你的定义不能正确解决,但是解决方案是正确的是使用静态或全局函数和对象。

 静态  void  displayFullGame(HWND hwnd,field *, int ); 



这意味着它不是成员函数,而是全局函数。因此,您可能应该使用指向类对象的全局指针,并以正确的方式创建和销毁它。

 display * gpDisplay = NULL; 



BTW:确保你实现了构造函数

 display :: display(){
}


错误消息是明确的:代码中的某处显示 ctor是必需的,你没有提供实施。您必须:

  • 实施(源代码中) display :: display()




  • 删除头文件中的ctor声明(这样编译器将为您提供默认实现

Error LNK2019 unresolved external symbol "public: __thiscall display::display(void)" (??0display@@QAE@XZ) referenced in function "public: __thiscall field::field(void)" (??0field@@QAE@XZ) SnakeByteWin C:\Users\iptamas\source\repos\SnakeByteWin\SnakeByteWin\field.obj 1

Error LNK2019 unresolved external symbol "public: void __thiscall display::displayFullGame(struct HWND__ *,class field *,int)" (?displayFullGame@display@@QAEXPAUHWND__@@PAVfield@@H@Z) referenced in function "long __stdcall WindowProci(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowProci@@YGJPAUHWND__@@IIJ@Z) SnakeByteWin C:\Users\iptamas\source\repos\SnakeByteWin\SnakeByteWin\windowproc.obj 1

Hi All,
This is the error I keep getting. I have a field.h with the field class, a display.h with the display class, and a windowproc.h and .cpp with the windowproc function. I guess the windowproc cannot see the definition of the display class, but I don't know why.
field.h=========================================

#ifndef FIELD_H_INCLUDED
#define FIELD_H_INCLUDED

#include "defines.h"
#include "matrix.h"
#include "bites.h"
#include "snake.h"
#include "display.h"

class field {
      int score;
      int hiScore;
      int level;
      int nofBites;
      int speed;
      int scoreCounter;
      bool isGameOn;
      bites* bitesPr;
      snake* snakePr;
      matrix* matrixPr;
      display* displayPr;
   public:
      field();
      ~field();
      void initLevel();
      void initGame();
      int getScore();


void setScore(int);........ and so on....
display.h=========================================

#pragma once
#include"windowproc.h"
class field;
class display {
		HBITMAP hBitmapWall;
		HBITMAP hBitmapNoWall;
		HBITMAP hBitmapBites;
		HBITMAP hBitmapSnake;
		HBITMAP hBitmapStartButton;
		RECT scoreRect;
	public:
		display();
		BOOL CALLBACK displayChildWindows(HWND hwnd, LPARAM lParam);
		void displayFullGame(HWND hwnd, field*, int);
		RECT getScoreRect();
		HBITMAP getBitmapWall();
		HBITMAP getBitmapNoWall();
		HBITMAP getBitmapBites();
		HBITMAP getBitmapSnake();
		HBITMAP getBitmapStartButton();
};


windowproc.cpp===============================================

#include "field.h"

LRESULT CALLBACK WindowProci(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
	LRESULT r = 0; 
	field* fg=(field*)GetWindowLongPtr(hwnd, GWLP_USERDATA);

	switch(uMsg){
		case WM_NCCREATE: {	
		SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG)(((CREATESTRUCT*)lParam)->lpCreateParams));	
		r=DefWindowProc(hwnd, uMsg, wParam, lParam);
		break;

		}
	...... other case's ......


		case WM_TIMER: {//=========================   WM_TIMER message  =====================
			//display d;
			if (!fg->getNofBites()) PostMessage(hwnd, STARTNEWLEVEL, 0, 0);			// events of fullGame + display fullGame
			fg->isSnakeAlive();
			fg->moveSnake();
			fg->decreaseScoreCounter();
			if (fg->isBitesEaten()) {
				fg->generatePositionBites();
				fg->nofBitesDown();
				fg->setScore(fg->getScore() + fg->getScoreCounter());
				fg->setScoreCounter(SCORECOUNTER + fg->getLevel()*LEVELLER);
				fg->getSnakePr()->setCounter(COUNTER);
				fg->getDisplayPr()->displayFullGame(hwnd, fg, DISPLAYSCORE);
			}
			fg->getDisplayPr()->displayFullGame(hwnd, fg, DISPLAYBITES);
			fg->getDisplayPr()->displayFullGame(hwnd, fg, DISPLAYSNAKE);
			if (fg->getSnakePr()->getAlive()) PostMessage(hwnd, GAMEOVER, 0, 0);				
			break;			
}// case WM_TIMER)



What I have tried:

My assumption is that 'include's are not correct. I tried almost all the variations, but when I managed to avoid all the 'double includes', I still got the same message.
I also tried to create the instance of the display class in the windowproc function, but it did not work either.
Any idea? Thank you.

解决方案

Quote:

I guess the windowproc cannot see the definition of the display class, but I don't know why.

That is indeed the reason. The compiler sees the declaration of the display class in display.h but the definition (implementation) is missing. I would expect that you have also a display.cpp file. If so, that must be compiled too and the resulting object file must be linked (e.g. by just adding the file to your project).

According to the first error message you have a field.obj file which has been probably created by compiling field.cpp. You need to handle the display class in a similar manner.


Your are right that the definition cant be correct solve, but the solution is to use static or global functions and objects.

static void displayFullGame(HWND hwnd, field*, int);


This mean that it isnt a member function, but a global function. So it may be that you should use a global pointer to your class object and create and destroy it in a correct manner.

display *gpDisplay = NULL;


BTW: Be sure that you implemented the constructor

display::display() {
}


The error message is clear: somewhere in your code the display ctor is required and you did'nt provide the implementation. You have to either:
  • Implement (in source code) display::display()

or
  • Remove the ctor declaration in the header file (this way the compiler will provide the default implementation for you


这篇关于未解析的外部符号 - 函数看不到定义[已解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆