c++ - 尝试在 C++ 中为堆栈实现模板(但惨遭失败)

标签 c++ visual-studio templates stack 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".

这里是代码: //堆栈.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;
}
}

...还有 main.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();
}

非常感谢帮助,现在已经尝试了一个多小时来找出错误,谢谢

最佳答案

类模板成员函数的定义必须驻留在您的头文件中,以便它们在引用它们的每个翻译单元中都可用。

你的书应该提到这一点。

关于c++ - 尝试在 C++ 中为堆栈实现模板(但惨遭失败),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24614103/

相关文章:

c++ - 为什么 getchar_unlocked() 比其他方法更快?

c++ - OpenScenegraph 示例代码问题

c++ - 模板容器类被认为是 "scalar"?

php - 解析 TWIG 中的变量内容

c++ - 来自元组的构造函数参数

c++ - 为必须可平凡复制的 volatile 结构分配一个值

c++ - vector -首先出现k值

c++ - 使用 Stroustrup 示例的 condition_vairable::wait_for() 问题

c# - 像在 Visual Studio 中一样标记滚动条

c# - 是否可以告诉 Visual Studio 不要将源文件视为 "component"?