【发布时间】:2011-04-25 09:47:11
【问题描述】:
我第一次在 C++ 中使用模板,当我尝试编译时遇到了问题。基本上是在尝试创建我自己的各种基本 ArrayList:
.hpp:
#ifndef ARRAYLIST_HPP_
#define ARRAYLIST_HPP_
template <class T>
class ArrayList
{
private:
int current, top ;
T * al ;
public:
ArrayList() ; // default constructor is the only one
};
#endif /* ARRAYLIST_HPP_ */
.cpp:
#include "ArrayList.hpp"
using namespace std ;
//template <class T>
//void memoryAllocator( T * p, int * n ) ; // private helper functions headers
template <class T>
ArrayList<T>::ArrayList()
{
current = 0 ;
top = 10 ;
al = new T[top] ;
}
主要:
#include "ArrayList.hpp"
int main()
{
ArrayList<int> test ;
}
当我尝试在没有 main 的情况下编译时,它编译得很好,但是,一旦我尝试在 main 中使用它,我就会收到以下错误:
Undefined symbols for architecture x86_64:
"ArrayList<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::ArrayList()", referenced from:
_main in Website.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [APProject2] Error 1
任何关于可能是什么问题的想法将不胜感激!
干杯!
【问题讨论】:
-
这个问题很多。尝试在这个网站上搜索“[c++] [template] linker”。