Possible Duplicate:
Undefined reference error for template methodC++模板錯誤
你好,我有這樣的代碼是給我這個錯誤:
未定義的參考`MyStack ::推(INT)」的main.cpp
爲什麼?
MyStack.h:
#ifndef STACK_H
#define STACK_H
template <typename T>
class MyStack
{
private:
T *stack_array;
int count;
public:
void push(T x);
void pop(T x);
void xd(){}
};
#endif /* STACK_H */
MyStack.cpp:
#include "mystack.h"
template <typename T>
void MyStack<T>::push(T x)
{
T *temp;
temp = new T[count];
for(int i=0; i<count; i++)
temp[i] = stack_array[i];
count++;
delete stack_array;
stack_array = new T[count];
for(int i=0; i<count-1; i++)
stack_array[i] = temp[i];
stack_array[count-1] = x;
}
template <typename T>
void MyStack<T>::pop(T x)
{
}
main.cpp中:
#include <iostream>
#include "mystack.h"
using namespace std;
int main(int argc, char *argv[])
{
MyStack<int> s;
s.push(1);
return 0;
}
順便說一句,你還需要在構造函數中可能使用它之前,你的初始化計數變量。 – 2011-04-27 09:16:46
謝謝!我確實注意到需要初始化,但是我想解決這個問題非常糟糕,因爲它讓我感到不安,哈哈。 – petermlm 2011-04-27 12:49:00