错误LNK2001:未解决的外部符号错误 [英] Error LNK2001: unresolved External symbol errors

查看:86
本文介绍了错误LNK2001:未解决的外部符号错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中遇到错误LNK2001:未解析的外部符号错误。



以下内容来自Cthread.h文件:



Hi, I have encountered "Error LNK2001: unresolved External symbol" errors in my code.

The following is from the Cthread.h file:

#ifndef Cthread_H_
#define Cthread_H_


#include <windows.h>
#include <process.h>
#include <time.h>
#include <algorithm>

using namespace WAVE_VNS;

class Cthread
{
	bool Running;
	//This variable is the signal for our thread to stop execution.
	//If it's True then thread will gently stop.
	bool End;
	//This is the handle to our thread system give us when
	//Thread is created.
	HANDLE MySendThread;
	DWORD MySendThreadId;

	HANDLE MyReceiveThread;

	static VNS VNSelement;



	/*stores the socket number value after init socket*/
	static int vnsSocket;
	/*object of class ethernet*/
	static Ethernet Eth;

public:
	/***local Thread***/
	//automatically set the default values of running, end, tid and handle variables
	/*the local port will be automatically being initialised and binded to when calling this constructor*/
	Cthread(unsigned int port);
	
	/*Receive Thread:*/
	/*Starts the VNS decoding process*/
	static void ReceiveThread(void* data);

	/*Send Thread:*/
	static DWORD WINAPI SendThread(LPVOID lpParam); 

};

#endif/*Cthread_H*/







以下内容来自Cthread.cpp文件:






The following is from the Cthread.cpp file:

#include "Cthread.h"

/******************Threads***************************/

Cthread::Cthread(unsigned int port)
{
	//This means thread is not running
	Running = false;
	//Our signal for stopping thread, in this case not.
	End = false;

	//Set default values for identifier and handle
	MySendThread = 0;
	MyReceiveThread = 0;

	vnsSocket = Eth.initSock(port); 

	//Pass static_run function and whole object as parameter.
	//Get handle to our thread and identifier (tid)
	MySendThread = CreateThread(NULL,0, SendThread, 0, 0, &MySendThreadId );
	MyReceiveThread = (HANDLE)_beginthread( &Cthread::ReceiveThread, 0, 0);

	while(1)
	{
		Sleep(500);
	}

	Eth.endSock(vnsSocket);
}

/*Receive Thread initiator*/
/*Starts the VNS decoding process*/
void Cthread::ReceiveThread(void* data)
{
	/*stores the receive buffer*/
	char receive[512];
	/*aim: to convert the receive buffer into unsigned variable for hexadecimal*/
	unsigned char *received;

	received = new unsigned char[512];

	unsigned long desireSenderIP;
	unsigned long senderIP = 0;
	SOCKADDR_IN peerAdr;
	char *strSenderIp;
	desireSenderIP = inet_addr( VNS_UDP_IP_NO );
	int readbuffersize = sizeof(receive);

	int result;
	
	while (1) //true
	{
		result=  Eth.rec(vnsSocket, static_cast<void*>( &receive), &readbuffersize, &senderIP);
		
		//if result is OK
		if ((result > 0) && (desireSenderIP == senderIP))
		{

			received = (unsigned char *)receive; 
			//Decode the message received. Where the classification of the respective messages begins.
			VNSelement.decode_Message(received);
			VNSelement.SetFlagFault(false);
			printf("\n ****No Fault.***** ");

		
		}

		else
		{
			VNSelement.SetFlagFault(true);
			printf("\n ****Fault!!!***** ");
		}
	}
}

/*Send Thread initiator*/
DWORD WINAPI Cthread::SendThread(LPVOID lpParam)
{
	int result;
    //char * mytxdata = "Hello";
    int dataSize;
    char * mytxdata;
   
	char sendArray[1500] = { 0x01, 0x02, 0x03 }; 

    //mytxdata = reinterpret_cast<char*>(&test);
    //dataSize = sizeof(test);

    while (1)
    {

        //result = UdpServer::send ( socketno, mytxdata, dataSize, VNS_UDP_IP_NO, EDIP_UDP_PORT_NO );

        if ( result == 0 )
        {
            //cout << "Send!\n";
        }
        
    }
}







错误的详细信息如下:



错误LNK2001:未解析的外部符号private:static int Cthread :: vnsSocket(?vnsSocket @ Cthread @@ 0HA)



错误LNK2001:未解析的外部符号private:static class WAVE_VNS :: VNS Cthread :: VNSelement (?VNSelement @ Cthread @@ 0VVNS @ WAVE_VNS @@ A)



错误LNK1120:2个未解决的外部因素





关于我的程序的一些深入细节:

基本上,我已经创建了上面的两个文件并用它们制作了一个。 Lib文件供使用。 VNSelement是来自我的其他VNS类的对象的实例,而WAVE_VNS是VNS类的命名空间。



非常感谢任何帮助。谢谢!




the details of the errors are as follows:

error LNK2001: unresolved external symbol "private: static int Cthread::vnsSocket" (?vnsSocket@Cthread@@0HA)

error LNK2001: unresolved external symbol "private: static class WAVE_VNS::VNS Cthread::VNSelement" (?VNSelement@Cthread@@0VVNS@WAVE_VNS@@A)

error LNK1120: 2 unresolved externals


A little in-depth details on my program:
Basically, I've created the two files above and used them to make a .Lib File for usage. VNSelement is a instance of the object from my other VNS class, while WAVE_VNS is the namespace for the VNS class.

Any help is greatly appreciated. thanks!

推荐答案

你已宣布静态变量 - 你需要在某处定义它们



you've declared static vars - you need to define them somewhere

<br />
Cthread::vnsSocket=0;<br />





类似于其他的var



另外,我不会这么做那样......传递这个您的类作为 beginthread cookie,有一个静态函数用于线程输入,将cookie转换回您的类,然后调用该实例的接收处理程序



这样你不需要任何静态变量,并且你可以拥有该类的多个实例



similarly with the other var

Additionally, i wouldn't do it quite that way ... pass the this of your class in as the beginthread cookie, have ONE static function for thread entry, that casts the cookie back to your class, then call that instance's receive handler

that way you don't need to have any static vars, and you can have multiple instances of the class


这篇关于错误LNK2001:未解决的外部符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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