我目前正在做一個課程任務,它涉及創建一個模板類。我有模板類設置和一切,但我得到的錯誤,我似乎無法修復。我嘗試了各種解決方案,迄今都沒有工作,我也做了一些合理的研究,並且沒有找到與我的情況完全相關的答案,儘管其他許多人都問過這個問題。我得到有關我的模板類的錯誤
下面是代碼迄今...
#ifndef STACK_H
#define STACK_H
#include "Queue.h"
#include "MyException.h"
#include <iostream>
using namespace std;
template<class T>
class Stack;
template<class T>
ostream& operator<<(ostream&,Stack<T>&);
template<class T>
class Stack
{
public:
Stack();
Stack(const Stack<T>& other);
void push(const T& el);
T pop();
T peek();
bool isEmpty();
friend ostream& operator<< <T>(ostream&,Stack<T>&);
Stack<T>& operator=(const Stack<T>& other);~Stack();
private:
class Node
{
public:
Node(const T& data, Node* n = 0)
{
element = data;
next = n;
}
T element;
Node* next;
};
Node* top;
};
#include "Stack.cpp"
#endif
那就是 「Stack.h」 再有就是
template<class T>
Stack<T>::Stack()
{
}
template <class T>
Stack<T>::Stack(const Stack<T>& other)
{
}
template <class T>
void Stack<T>::push(const T& el)
{
}
template <class T>
T Stack<T>::pop()
{
}
template <class T>
T Stack<T>::peek()
{
}
template <class T>
bool Stack<T>::isEmpty()
{
return false;
}
那就是 「Stack.cpp」。 我明白,在不同的文件中的實現不是普遍接受的方法,但不幸的是,這是代碼需要的地方。現在,當我甚至沒有任何真正的代碼運行時,可以這麼說,我得到以下錯誤。
3: expected constructor, destructor, or type conversion before '<' token
3: expected ';' before '<' token
9: expected constructor, destructor, or type conversion before '<' token
9: expected ';' before '<' token
17: expected init-declarator before '<' token
17: expected ';' before '<' token
23: expected init-declatator before '<' token
23: expected ';' before '<' token
,並繼續這樣,對每個功能上到第35行
現在,它可能是我的編譯器?難道我沒有完成所有的功能並返回了一些東西?
任何幫助將大大受惠,謝謝。
你的.cpp文件中的* required *#include指令在哪裏? –
您應該在您的cpp文件中包含Stack.h,而不是其他方式。 – IllusiveBrian
我非常確定,使用模板類可以用其他方式工作。在.cpp中包含.h使得它基本上循環編譯函數。 .cpp包含在.h –