Click here to Skip to main content
15,849,996 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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;
};


C++
// 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();
};


C++
// 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;
}



C++
// 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; 
}
Posted
Updated 13-Mar-12 20:04pm
v5

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
 
Share this answer
 
Comments
Rahul Rajat Singh 14-Mar-12 1:27am    
nice explanation.
Sergey Alexandrovich Kryukov 14-Mar-12 1:32am    
Thank you, Rahul. I'm not sure it's clear enough though, but at least OP could find out what to learn about.
--SA
Sergey Alexandrovich Kryukov 14-Mar-12 1:32am    
[Misplaced --SA]
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
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Mar-12 1:26am    
Correct and useful, but I feel that OP also needs some explanation on what the linker does. So, I voted 4 and added some explanations to complement your answer. Please see my answer.
--SA
Mohibur Rashid 14-Mar-12 2:58am    
Thank you
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Mar-12 1:24am    
Not a bid deal: this is "unresolved external symbol". Yes, more information would be needed, but I provided some instructions on what to do. Please see my answer.
--SA
You forgot to provide implementations of two functions that you declared in the header file DanhSachDinh.h
C++
void Delete();

and
C++
friend istream & operator >> (istream &, CDanhSachDinh &);

The Linker won't mind whether these are missing or not if you don't call them, but apparently somewhere you did call at least one of the two functions.

Solution: implement these functions and recompile.
 
Share this answer
 
Thanks all. This is the first time I post a question on codeproject.com.

Sorry for my inconvenient.
 
Share this answer
 
Comments
Nelek 14-Mar-12 14:02pm    
Don't worry, nobody here rant to newbies. Just to lazies ;) If you don't know, but you try, you will receive help. Keep going

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



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