0
有人會告訴我我做錯了什麼嗎?如果我註釋掉主函數中的所有語句,編譯器不會抱怨。所以,我認爲它與主要功能有關,正確的是當我創建一個LinkedStack的新實例時。鏈接堆棧模板和一堆錯誤
LinkedNode.h
#include<memory>
template<class T>
class LinkedNode
{
friend std::ostream& operator<<(std::ostream& os, const LinkedNode<T>& obj);
public:
LinkedNode(T newElement);
T GetElement() const {return element;}
void SetElement(T val) {element = val;}
LinkedNode<T>* GetNext() const {return next;}
void SetNext(LinkedNode<T>* val) {next = val;}
private:
T element;
LinkedNode<T>* next;
};
template<class T>
LinkedNode<T>::LinkedNode(T newElement)
{
element = newElement;
}
template<class T>
std::ostream& operator<<(std::ostream& os, const LinkedNode<T>& obj)
{
os << obj.element << endl;
return os;
}
LinkedStack.h
#pragma once
#include"LinkedNode.h"
#include<cassert>
template<class T>
class LinkedStack
{
public:
LinkedStack();
int GetSize() const {return size;}
bool IsEmpty() const {return size == 0;}
void Push(T val);
void Pop();
T Peek();
void Clear();
private:
LinkedNode<T>* head;
int size;
};
template<class T>
LinkedStack<T>::LinkedStack():size(0) {}
template<class T>
void LinkedStack<T>::Push(T val)
{
LinkedNode<T>* newOne = new LinkedNode<T>(val);
if(head == 0)
head = newOne;
else
{
newOne->next = head;
head = newOne;
}
size++;
}
template<class T>
void LinkedStack<T>::Pop()
{
assert(!IsEmpty());
LinkedNode<T>* tpHead = head;
head = head->next;
delete tpHead;
size--;
}
template<class T>
T LinkedStack<T>::Peek()
{
assert(!IsEmpty());
return head->element;
}
template<class T>
void LinkedStack<T>::Clear()
{
while(!IsEmpty())
Pop();
}
Source.cpp
#include"LinkedStack.h"
#include<iostream>
#include<string>
#include<memory>
int main()
{
LinkedStack<int> stack;
std::cout << "Add" << std::endl;
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.Push(4);
stack.Push(5);
std::cout << "Top element is: " << stack.Peek();
stack.Pop();
std::cout << "After pop one out, its top element is: " << stack.Peek() << std::endl;
return 0;
}
這是一個我得到的錯誤:
Warning 29 warning C4091: 'typedef ' : ignored on left of '_Collvec' when no variable is declared c:\program files (x86)\microsoft visual studio 11.0\vc\include\xlocinfo.h 58
Error 4 error C3857: 'LinkedStack': multiple template parameter lists are not allowed e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 5
Error 3 error C2989: 'LinkedStack' : class template has already been declared as a non-class template e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 20
Error 26 error C2989: 'lconv' : class template has already been declared as a non-class template c:\program files (x86)\microsoft visual studio 11.0\vc\include\locale.h 82
Error 27 error C2989: 'lconv' : class template has already been declared as a non-class template c:\program files (x86)\microsoft visual studio 11.0\vc\include\locale.h 109
Error 5 error C2988: unrecognizable template declaration/definition e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 23
Error 12 error C2988: unrecognizable template declaration/definition e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 40
Error 21 error C2988: unrecognizable template declaration/definition e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 57
Error 9 error C2447: '{' : missing function header (old-style formal list?) e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 27
Error 17 error C2447: '{' : missing function header (old-style formal list?) e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 51
Error 25 error C2447: '{' : missing function header (old-style formal list?) c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring 13
Error 2 error C2238: unexpected token(s) preceding ';' e:\fall 2013\cpsc 131\test\linkedstack\linkednode.h 6
Error 11 error C2182: 'LinkedStack' : illegal use of type 'void' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 40
Error 19 error C2182: 'LinkedStack' : illegal use of type 'void' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 57
Error 28 error C2143: syntax error : missing ';' before 'identifier' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xlocinfo.h 58
Error 10 error C2143: syntax error : missing ';' before '<' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 40
Error 18 error C2143: syntax error : missing ';' before '<' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 57
Error 8 error C2143: syntax error : missing ';' before '{' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 27
Error 16 error C2143: syntax error : missing ';' before '{' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 51
Error 24 error C2143: syntax error : missing ';' before '{' c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring 13
Error 20 error C2086: 'int LinkedStack' : redefinition e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 57
Error 1 error C2059: syntax error : '<' e:\fall 2013\cpsc 131\test\linkedstack\linkednode.h 6
Error 6 error C2059: syntax error : '<' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 23
Error 13 error C2059: syntax error : '<' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 40
Error 22 error C2059: syntax error : '<' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 57
Error 7 error C2039: 'Push' : is not a member of '`global namespace'' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 26
Error 14 error C2039: 'Pop' : is not a member of '`global namespace'' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 40
Error 15 error C2039: 'Peek' : is not a member of '`global namespace'' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 50
Error 23 error C2039: 'Clear' : is not a member of '`global namespace'' e:\fall 2013\cpsc 131\test\linkedstack\linkedstack.h 57
Error 30 error C1075: end of file found before the left brace '{' at 'c:\program files (x86)\microsoft visual studio 11.0\vc\include\xlocinfo.h(18)' was matched c:\program files (x86)\microsoft visual studio 11.0\vc\include\xlocinfo.h 58
我把你的代碼,我只做了一個改變,我在LinkedNode.h中註釋掉'private:',然後它爲我編譯。我看不到爲什麼發佈的代碼會發布錯誤。我唯一可以提出的建議是向LinkedNode.h添加一個包含守護程序。但我認爲這裏還有一些尚未發佈的內容。 – john
我在編譯器中看到的唯一問題是private:LinkedNode – MRB
[Linked Stack及其模板類]的可能重複(http://stackoverflow.com/questions/19268188/linked-stack-and-its-template-類) – dyp