【发布时间】:2011-04-27 08:57:08
【问题描述】:
你好,我有这段代码给我这个错误:
对 `MyStack::push(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;
}
【问题讨论】:
-
顺便说一句,在构造函数中使用它之前,您还需要初始化计数变量。
-
谢谢!!!我确实注意到需要初始化,但我真的很想解决这个问题,因为这让我很紧张,哈哈。