试图在c++中实现一个堆栈模板(失败得很惨)
Trying to implement a template for a stack in c++ (and failing miserably)
我大约半年前开始学习信息学,我们正在学习c++作为编程语言,所以我对编码真的很陌生。今天尝试实现堆栈模板,但是visual studio在尝试编译时一直告诉我"lnk2019:未解析的外部符号",所以我的代码中一定有一个(可能非常愚蠢)错误。下面是错误信息(部分是德语,但单词应该很容易猜到):
1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall stack<int>::stack<int>(void)" (??0?$stack@H@@QAE@XZ)" in Funktion "_main".
1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall stack<int>::~stack<int>(void)" (??1?$stack@H@@QAE@XZ)" in Funktion "_main".
1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: void __thiscall stack<int>::pop(void)" (?pop@?$stack@H@@QAEXXZ)" in Funktion "_main".
1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: void __thiscall stack<int>::push(int)" (?push@?$stack@H@@QAEXH@Z)" in Funktion "_main".
这里的代码://stack.h
#pragma once
template <class obj>
class stack
{
int maxSize;
int currentSize;
obj * thisStack;
public:
stack(int size);
~stack();
bool isFull();
bool isEmpty();
obj top();
void pop();
void push(obj objekt);
};
//stack.cpp
#include "stdafx.h"
#include "stack.h"
#include <iostream>
using namespace std;
template <class obj>
stack<obj>::stack(int size)
{
currentSize = 0;
maxSize = size;
thisStack = new obj[maxSize];
}
template <class obj>
stack<obj>::~stack()
{
delete thisStack[];
}
template <class obj>
bool stack<obj>::isEmpty()
{
if (currentSize == 0)
return true;
else
return false;
}
template <class obj>
bool stack<obj>::isFull()
{
if (currentSize == maxSize)
return true;
else
return false;
}
template <class obj>
obj stack<obj>::top()
{
if (!isEmpty())
{
return thisStack[currentSize];
}
else
{
cout << "Stack is empty" << endl;
}
}
template <class obj>
void stack<obj>::push(obj objekt)
{
if (!isFull())
{
thisStack[currentSize] = objekt;
cout << "Object " << thisStack[currentSize] << "pushed on the stack" << endl;
currentSize++;
}
else
{
cout << "Der Stack is full" << endl;
}
}
template <class obj>
void stack<obj>::pop()
{
if (!isEmpty())
{
cout << "The Object " << thisStack[currentSize - 1] << endl;
currentSize--;
}
else
{
cout << "Stack is empty" << endl;
}
}
…还有主要的。CCP,我用几个整数值测试了它,发现它不起作用
#include "stdafx.h"
#include "stack.h"
void main()
{
stack<int> myStack(10);
myStack.push(1);
myStack.push(2);
myStack.push(3);
myStack.push(4);
myStack.push(5);
myStack.push(6);
myStack.push(7);
myStack.push(8);
myStack.push(9);
myStack.push(10);
myStack.push(11);
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
myStack.pop();
getchar();
}
帮助我将非常感激,试着找到一个多小时的错误,现在,谢谢
类模板成员函数的定义必须驻留在头文件中,以便在引用它们的每个翻译单元中可用。
你的书应该提到这一点
相关文章:
- 运行时检查失败 #2 变量"A"周围的堆栈已损坏
- 运行时检查失败 #2 - 变量"e"周围的堆栈已损坏。发生
- 运行时检查失败 #2 - 变量周围的堆栈'...'已损坏
- 运行时检查失败 #2 - 变量"l1"周围的堆栈已损坏
- C++:此代码可以编译,但引发运行时检查失败 #2 - 围绕变量周围的堆栈'num'已损坏。发生
- 运行时检查失败 - 变量周围的堆栈已损坏
- 调试:运行时检查失败 #2 - 变量"LoggerThread"周围的堆栈已损坏
- 运行时检查失败 #2 - 变量周围的堆栈'k'已损坏
- 运行时检查失败 #2 - 变量"数字选择"周围的堆栈已损坏
- 运行时检查失败#2 - 变量周围的堆栈'myArray'已损坏.- 似乎无法弄清楚如何删除此错误
- 运行时检查失败 #2 - 变量周围的堆栈'normalIndex'已损坏
- 错误:运行时检查失败 #2 - 变量周围的堆栈已损坏
- 堆栈:C 失败中的平衡括号
- 运行时检查失败 #2 - 变量周围的堆栈'sample'已损坏
- 运行时检查失败 #2 - 变量周围的堆栈'ap'已损坏
- 使用链接列表实现堆栈,调试断言失败
- 运行时检查失败 #2 - 变量周围的堆栈---已损坏
- 运行时检查失败 #2 - 变量周围的堆栈'projectAverage'已损坏
- 运行时检查失败 #2 - 变量周围的堆栈'length'已损坏
- 在调试生成C++中获取调用堆栈失败