2012-02-10 47 views
1

所以我一直tryind创建一个简单的数组列表类,并得到了在一开始媒体链接卡住...林坚持与C++中的模板类

我的头文件(我删除了我的cpp文件,并仍然得到了同样的信息)

#ifndef ARRAYLIST_H 
#define ARRAYLIST_H 

#include <iostream> 
using namespace std; 

template <typename T> 

class ArrayList { 
private: 
    T *arr; 
    int length; 

public: 
    ArrayList(); 

    void Insert(T item); 

    void Print(); 

    //friend &ostream operator<< (ostream &out, ArrayList &al); 
}; 

#endif 

和我的错误

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall ArrayList<int>::ArrayList<int>(void)" ([email protected]@@[email protected]) referenced in function _main 
1>C:\Users\Gannash\Desktop\Programming\C++WS\XMLReader\Debug\XMLReader.exe : fatal error LNK1120: 1 unresolved externals 
+1

嗯,你已经声明了三个成员函数,在哪里定义? – ildjarn 2012-02-10 23:08:47

回答

1

这就像你只是声明了类的方法,而不是将它们定义

将所有定义放在标题中

+0

我actualy没有声明.cpp文件中的构造函数,但我没有发布它在这里...仍然相同的错误信息 – 2012-02-10 23:20:41

0

您还没有在.cpp文件中定义ArrayList(),Insert()和Print()。 您需要为这些函数编写代码或将它们转换为纯虚函数,或者virtual Print()=0;

1

如果您正在定义模板类,您应该提供所有的成员函数implmeentations内联。缺少的ctor肯定是一个问题,但是当你真正使用你的课程时,会出现其他问题。另外,还有标准的容器提供你想要达到的东西,最值得注意的是std :: vector。

此外,在头文件中使用名称空间是一个过时的问题(或者至少极不鼓励)。 您应该使用std :: qualification来代替,特别是您只需要一次。

0

,让我们对错误信息:

unresolved external symbol "public: __thiscall ArrayList<int>::ArrayList<int>(void)" ([email protected]@@[email protected]) referenced in function _main 

unresolved external symbol =>有一些符号(这里函数)声明,但没有定义

ArrayList<int>::ArrayList<int>(void) =>构造或以T实例化ArrayList类模板=诠释

referenced in function _main =>有可能是像下面的代码主():

ArrayList<int> IntList; 

的解决方案是提供构造的实施方案,可能如下:

ArrayList() : arr(0), length(0) {} 

类体内部。


顺便说一句,请随时查看您参考:

std::array 

    std::vector