Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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=========================================
C++
#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=========================================
C++
#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===============================================
C++
#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.
Posted
Updated 8-Jan-22 8:31am
v2
Comments
RedDk 4-Feb-18 20:33pm    
LNK2019 is a linker error. The linker can't find a library that is required for a function or method being used in your code and is referencing it. The names are being mangled (all those @@QAE ... (this IS C++, after all)) so in order to find out ... at what point the cascade of un-referenced stuff STARTS ... try linking with a command line addition called /VERBOSE. Then, when the linker lists what's being referenced, you'll have an exact spot to check (see the list of library references you ARE linking). The name of the missing library will have a .lib extention most likely.

You might also do a system-wide search for all .lib files. Likely where they are, the missing .lib should be/ought to be. Also google the app; I'm assuming you're looking for something with SnakeBite in the title, yes?
m_smith 5-Feb-18 5:17am    
Thank you. I might get you wrong... I checked the output, and I got many 'found' stuff in 'referenced' .libs, but the error message is the same. When it finishes checking/searching for libraries, the message is just 'Finished searching libraries'. And yes, it is a SnakeBite, and it works on console :)

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.
 
Share this answer
 
Comments
m_smith 5-Feb-18 5:48am    
Thanks, you are the one! I have been trying to solve this for a few days, and actually I just forgot to add the display.cpp file to the project. It's a SHAME! :)
Your are right that the definition cant be correct solve, but the solution is to use static or global functions and objects.
C++
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.
C++
display *gpDisplay = NULL;

BTW: Be sure that you implemented the constructor
C++
display::display() {
}
 
Share this answer
 
Comments
m_smith 5-Feb-18 5:46am    
Thank you, Karsten, I would like to use it as a member function of a class.
KarstenK 23-Feb-18 11:16am    
That isnt possible, because the input needs some static function. You can use tricks to do it, but they may hit back and crash your program someday.
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
 
Share this answer
 
Comments
m_smith 5-Feb-18 4:26am    
I also have a display.cpp, I provided the definition of the ctor in there. Sorry if I was not clear enough.

#include "display.h"

display::display() {
scoreRect.left = 10;
scoreRect.top = 0;
scoreRect.right = 440;
scoreRect.bottom = 10;
}
...
Although this post is real old it helped me, Even I was facing same problem where the linker kept giving same errors of LINK2019. I spent hours trying the fiddle around with header files static members but no success. Realized from @m_smith mistake, maybe my visual studio is not able to detect the header or cpp file even thought I created them from its interface only. So tried excluding the script folder from project and including again and voila! got fixed.
 
Share this answer
 
Comments
CHill60 10-Jan-22 7:29am    
This is a comment regarding Solution 3 and should have been posted there using the "Have a Question or Comment?" link next to that post. It is not a solution to the original post so should not have been posted using "Add your solution here"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900