470,815 Members | 1,133 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Help for me:LNK2019

Hi,I have met a trouble,has anyone can help me?

error detail:
Linking...
ListTest.obj : error LNK2019: unresolved external symbol "public: __thiscall
com::LinearList<int>::LinearList<int>(int)" (??0?$LinearList@H@com@@QAE@H@Z)
referenced in function _main
D:\Project\CPPTest\Debug\CPPTest.exe : fatal error LNK1120: 1 unresolved
externals

<ListTest.cpp>
-------------------------------------------------------------------------------------------
#include <stdafx.h>
#include "List.h"
#include <iostream>

using namespace std;
using namespace com;

void main()
{
try
{
LinearList<int> L(10);
//cout << "Length = " << L.Length() << endl;
//cout << "IsEmpty = " << L.IsEmpty() <<endl;
////L.Insert( 0 , 2 ).Insert( 1 ,6 ) ;
////cout << "List is " << L << endl;
//cout << "IsEmpty = " << L.IsEmpty() << endl;
//int z = 0;
////L.Find( 1 , z ) ;
//cout << "First element is " << z << endl;
//cout << "Length = " << L.Length() << endl;
////L.Delete( 1 , z ) ;
//cout << "Deleted element is " << z << endl;
////cout << "List is " << L << endl;

}
catch (...)
{
cerr << "An exception has occurred" << endl;
}
}
<List.h>
-------------------------------------------------------------------------------------------
#include "stdafx.h"
#include <iostream>
using namespace std;

namespace com {
// 内存不足
class NoMem {
public:
NoMem () {}
};

template<class T> class LinearList {
public:
//构造函数
LinearList(int MaxListSize);
//析构函数
~LinearList() {delete [] element;}
bool IsEmpty() const {return length == 0;}
int Length() const {return length;}
//返回第k个元素至x中
bool Find(int k, T& x) const;
// 返回x所在位置
int Search(const T& x) const;
// 删除第k个元素并将它返回至x中
LinearList<T>& Delete(int k, T& x);
// 在第k个元素之后插入x
LinearList<T>& Insert(int k, const T& x);
void Output(ostream& out) const;
private:
int length;
int MaxSize;
// 一维动态数组
T *element;
} ;

};

<List.cpp>
-----------------------------------------------------------------------------
#include <stdafx.h>
#include "List.h"
#include <iostream>
using namespace std;

namespace com {

// 使n e w引发N o M e m异常而不是x a l l o c异常
void my_new_handler()
{
throw NoMem();
}

//set new handler for new operation
new_handler Old_Handler_=set_new_handler(my_new_handler);

template<class T> LinearList<T>::LinearList(int MaxListSize)
{
// 基于公式的线性表的构造函数
MaxSize = MaxListSize;
element = new T[MaxSize];
length = 0;
}
......
}
Mar 3 '06 #1
2 1542

"雪云鹰" <xu********@hotmail.com> wrote in message
news:Ob**************@TK2MSFTNGP15.phx.gbl...
Hi,I have met a trouble,has anyone can help me?

error detail:
Linking...
ListTest.obj : error LNK2019: unresolved external symbol "public:
__thiscall com::LinearList<int>::LinearList<int>(int)"
(??0?$LinearList@H@com@@QAE@H@Z) referenced in function _main
D:\Project\CPPTest\Debug\CPPTest.exe : fatal error LNK1120: 1 unresolved
externals

<ListTest.cpp>
-------------------------------------------------------------------------------------------
#include <stdafx.h>
#include "List.h"
#include <iostream>

using namespace std;
using namespace com;

void main()
{
try
{
LinearList<int> L(10);
//cout << "Length = " << L.Length() << endl;
//cout << "IsEmpty = " << L.IsEmpty() <<endl;
////L.Insert( 0 , 2 ).Insert( 1 ,6 ) ;
////cout << "List is " << L << endl;
//cout << "IsEmpty = " << L.IsEmpty() << endl;
//int z = 0;
////L.Find( 1 , z ) ;
//cout << "First element is " << z << endl;
//cout << "Length = " << L.Length() << endl;
////L.Delete( 1 , z ) ;
//cout << "Deleted element is " << z << endl;
////cout << "List is " << L << endl;

}
catch (...)
{
cerr << "An exception has occurred" << endl;
}
}
<List.h>
-------------------------------------------------------------------------------------------
template<class T> class LinearList {
public:
//构造函数
LinearList(int MaxListSize);
//析构函数
~LinearList() {delete [] element;}
<snip>
} ;

};

<List.cpp>
-----------------------------------------------------------------------------
#include <stdafx.h>
#include "List.h"
#include <iostream>
using namespace std;

namespace com {

// 使n e w引发N o M e m异常而不是x a l l o c异常
void my_new_handler()
{
throw NoMem();
}

//set new handler for new operation
new_handler Old_Handler_=set_new_handler(my_new_handler);

template<class T> LinearList<T>::LinearList(int MaxListSize)
{
// 基于公式的线性表的构造函数
MaxSize = MaxListSize;
element = new T[MaxSize];
length = 0;
}


I am pretty sure it will work if you do not split declaration and
implementation into 2 separate files.
i.e. you have to put the full implementation in the header file.

if you want to split the declaration and implementation of a template class
you would have to use the export keyword.

quote from stroustroub, programming C++ language 3d edition, page 351:
"To be accessible from other compilation units, a template definition must
be explicitly declared export ...
otherwise, the definition must be in scope whenever the template is used"

sadly, the export keyword is not supported in VC, so you have to put the
implementation in your header file.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 3 '06 #2
雪云鹰 wrote:
Hi,I have met a trouble,has anyone can help me?

template<class T> class LinearList {
public:
//构造函数
LinearList(int MaxListSize);
...
} ;


Where's definition of this constructor?
Mar 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Hadi | last post: by
2 posts views Thread by hazizpour | last post: by
1 post views Thread by eraserwars | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.