我剛剛開始使用C++,並遇到了這個問題。我在Fifo.h定義的類先進先出:C++未定義的構造函數
/* Fifo.h */
#ifndef FIFO_H_
#define FIFO_H_
#include <atomic>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
template <class T>
class Fifo
{
public:
Fifo<T>(int len);
~Fifo<T>();
int AddTokens(T* buffer, int len);
int RetrieveTokens(T* buffer, int len);
private:
//int len;
};
#endif /* FIFO_H_ */
而且在Fifo.cpp的定義:
/* Fifo.cpp*/
#include "Fifo.h"
template <class T>
Fifo<T>::Fifo(int len)
{
//_fifoptr = new FifoImpl_class((T)len);
printf ("From the constructor\n");
//thisbuffer = malloc(sizeof(T)*len);
}
template <class T>
Fifo<T>::~Fifo() { }
template <class T>
int Fifo<T>::AddTokens(T* buffer, int len)
{
printf("Added tokens\n");
return(1);
}
template <class T>
int Fifo<T>::RetrieveTokens(T* buffer, int len)
{
printf("Removed tokens\n");
return(2);
}
而且,我測試我的課像這樣(Fifotest.cpp):
#include "Fifo.h"
int main(int argc, char *argv[])
{
Fifo<int> MyFifo(20);
}
用gcc-4.5構建它給了我這個錯誤: 未定義參考Fifo<int>::~Fifo()' undefined reference to
先進先出:: FIFO(INT)」
看起來像我有相關的方法定義,但我無法弄清楚爲什麼我得到這個錯誤。我花時間搜索它,而選擇是跑上課並修改它。但是,我想知道我已經有什麼問題。將不勝感激任何幫助!
這必須有六萬億投資。 – Puppy 2011-12-26 13:00:32