如何修复LNK 2019错误 [英] How to fix LNK 2019 error

查看:73
本文介绍了如何修复LNK 2019错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我捕获到错误"LNK 2019".

我是C ++ OOP的新手,所以我没有足够的经验来管理此错误.

请帮我修复它.非常感谢. (对不起,我的英语不好).

我的项目:

I caught a error "LNK 2019".

I''m a newbie in C++ OOP so I have not enough experience to manage this error.

Please help me fix it. Thanks so much. (Sorry my English language''s not good).

My project:

#pragma once
// file Node.h
#pragma once


class CNode
{
	friend class CDanhSachDinh;
	
public:
	CNode(const int&);
	~CNode(void);	
	int& GetInfo();
	

private:
	int _info;
	CNode *_pNext;
};





// file DanhSachDinh.h (DanhSachDinh in English means Vertice_List)
#pragma once
#include "Node.h"
#include <iostream>
#include <string>
using namespace std;

class CDanhSachDinh
{	
public:	
	CDanhSachDinh();
	CDanhSachDinh(const CDanhSachDinh&);
	~CDanhSachDinh();
	bool IsEmpty() const;
	int Length() const;
	bool InsertAt(const int&, int);
	void Append(const int&);
	void Delete();
	CNode* GetHead();
	int& ElementAt(int);
	void Input();
	friend ostream & operator << (ostream &, CDanhSachDinh &);
	friend istream & operator >> (istream &, CDanhSachDinh &); 
	
private:
	CNode* _pHead;
	CNode* _pTail;
	int _length;
	std::string _result;
	void Print();
};





// file Node.cpp
#include "Node.h"

CNode::CNode(const int& value)
{
	this->_info = value;
	this->_pNext = NULL;
}

CNode::~CNode(void)
{
}

int& CNode::GetInfo()
{
	return this->_info;
}







// file DanhSachDinh.cpp (DanhSachDinh in English means Vertice_List)

#include "DanhSachDinh.h"
#include "Node.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

CDanhSachDinh::CDanhSachDinh(void)
{
	this->_pHead = NULL;
	this->_pTail = NULL;
	this->_length = 0;
	this->_result = "";
}

CDanhSachDinh::CDanhSachDinh(const CDanhSachDinh& list)
{
	if(list.IsEmpty())
	{
		this->_pHead = this->_pTail = NULL;
		this->_length = 0;
	}
	else
	{
		this->_pHead = new CNode(list._pHead->_info);
		this->_pTail = this->_pHead;
		CNode *p;
		for(p = list._pHead->_pNext; p; p = p->_pNext)
			this->Append(p->_info);
		this->_length = list._length;
	}
}

CDanhSachDinh::~CDanhSachDinh()
{

}

CNode* CDanhSachDinh::GetHead()
{
	return NULL;
}

void CDanhSachDinh::Append(const int& x)
{
	CNode *p = new CNode(x);
	if(this->IsEmpty())	// Danh sách rỗng
	{
		this->_pHead = this->_pTail = p;
	}
	else	// Danh sách có ít nhất 1 phần tử
	{
		this->_pTail->_pNext = p;
		this->_pTail = p;
	}
	this->_length++;
}

bool CDanhSachDinh::InsertAt(const int& data, int position)
{
	// Vị trí chèn x vào nằm cuối cùng
	if(position == this->_length + 1)
	{
		this->Append(data);
		return true;
	}

	// Vị trí không hợp lệ
	if(!(position >= 1 && position <= this->_length))
		return false;

	// Chèn x vào
	CNode *newp = new CNode(data);
	if(position == 1)
	{
		newp->_pNext = this->_pHead;
		this->_pHead = newp;
	}
	else
	{
		CNode *p = this->_pHead;
		while(position > 2)
		{
			p = p->_pNext;
			position--;
		}
		newp->_pNext = p->_pNext;
		p->_pNext = newp;
	}	
	return true;
}

bool CDanhSachDinh::IsEmpty() const
{
	return this->_length==0;
}

void CDanhSachDinh::Print()
{	
	char buffer[30];
	std::string result = "";	
	result += "{";
	CNode* p = this->_pHead;
	while(p)
	{
		_itoa_s(p->_info, buffer, 10);
		result.append(buffer);
		result.append("; ");
		p = p->_pNext;
		
	}
	result += "}";
	//cout<<result<<endl;
	this->_result = result;
	
}

void CDanhSachDinh::Input()
{
	int n;
	cout<<" - Nhap bao nhieu dinh: ";
	cin>>n;
	for(int i = 1; i <= n; i++)
	{
		int x;
		cout<<" - Nhap phan tu thu "<<i<<" = ";
		cin>>x;
		this->Append(x);
	}
}

int CDanhSachDinh::Length() const
{
	return this->_length;
}

ostream& operator << (ostream & os, CDanhSachDinh & list)
{
	list.Print();
	os << list._result << endl;
	return os; 
}

推荐答案

这是无法解析的外部符号"错误.太糟糕了,您没有报告确切和完整的错误消息;你应该总是这样做.原因是:您使用了由编译器创建的一些目标代码,而没有源代码.可以以目标文件(* .obj)或更可能以对象库(* .lib)的形式提供此代码.链接器的任务是将所有目标代码(从源代码和库中编译的所有目标代码)放在一起,将它们放在一起,并通过方法和变量的名称解析它们之间的所有依赖关系.

您通过头文件使用此代码的接口,但未提供库本身.这是通过链接器的命令行选项或通过您的项目的属性来完成的.您需要1)阅读完整的错误消息并找出无法解析的符号,2)通过例如头文件进行标识,该符号也属于哪个库并标识该库; 3)找出库* .lib文件(或* .obj文件)所在的位置,并将其添加到项目中.

请注意:链接器符号可能由于名称修饰而与它的源代码名称不同:
http://en.wikipedia.org/wiki/Name_mangling [
This is "unresolved external symbol" error. Too bad you did not report exact and complete error message; you should always do it. The reason is: you use some object code created by a compiler, without the source code. This code can be available in the form of either object file (*.obj) or, more likely, object library (*.lib). The task of the linker is to take all object codes together (those were just compiled from your source code and those from the libraries), put them together, and resolve all dependencies between them by the names of methods and variables.

You use the interface of this code through a header file, but did not supply the library itself. It''s done by the command line options of the linker or through the propertied of your project. You need to 1) read complete error message and find out what symbol is not resolved, 2) identify by, say, header files, what library this symbol belongs too and identify this library; 3) find out where the library *.lib file (or *.obj files) is located and add it to the project.

Please be careful: the linker symbol could differ from the source-code name of it due to the name mangling:
http://en.wikipedia.org/wiki/Name_mangling[^].

—SA


您转储了整个代码,但我们需要的是错误消息.

无论如何,您遇到了链接错误.向我们显示缺少的函数名称.那么我们也许可以为您提供帮助.

通常,库文件位于默认的lib目录中,如果某个文件不存在,则需要引用它.为此,请转到项目属性. ALT + F7是快捷方式,

在配置属性"树菜单上,链接器树菜单>输入树菜单>

在右侧面板上,将您的库添加到其他依赖项".

我的说明基于Visual Studio 2008
You dumped your entire code, but what we need is the error message.

Anyway, you are having linking error. Show us the missing function name. then we might be able to help you.

In general, library files are resides in default lib directory, if the certain file is not there, we need to refer it. To do so, go to project property. ALT+F7 is the short cut,

on "Configuration Properties" tree menu> Linker Tree Menu> Input Tree Menu>

on the right panel add your library to Additional Dependencies.

My instruction is based on Visual Studio 2008


此处的代码太多.但我记得在较早的编译器上也有同样的问题.我有一个解决方案,其中模板类的函数定义和声明必须在同一文件中才能消除问题.

因此,也许您应该尝试将模板类的定义和声明放在同一文件中,或者使该类内联.
There is too much code here. but I remember having the same problem on older compilers. I had a solution where the template classes'' function definition and declaration has to be in same file to remove the problem.

so perhaps you should try to put the definition and declaration of your template class in same file or make the class inline.


这篇关于如何修复LNK 2019错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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