2013-10-09 39 views
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 
+0

我把你的代碼,我只做了一個改變,我在LinkedNode.h中註釋掉'private:',然後它爲我編譯。我看不到爲什麼發佈的代碼會發布錯誤。我唯一可以提出的建議是向LinkedNode.h添加一個包含守護程序。但我認爲這裏還有一些尚未發佈的內容。 – john

+0

我在編譯器中看到的唯一問題是private:LinkedNode – MRB

+0

[Linked Stack及其模板類]的可能重複(http://stackoverflow.com/questions/19268188/linked-stack-and-its-template-類) – dyp

回答

2

錯誤在您的頭文件中。你在註釋掉main函數時看不到編譯器錯誤的原因,是由於模板的工作原理(它們基本上是創建對象的基礎,但直到嘗試實例化時才創建該定義)。

看起來你在你的LinkedNode.h文件中缺少一個頭文件。在其頂部放置一個#pragma once(或使用#ifndef/#define)。但我不認爲這是你的錯誤的原因。

您似乎宣佈LinkedStack非模板版本,或可能有以下語法的地方(你有沒有顯示):

template<class T> 
template<class N> 
class LinkedStack {... } 

對於模板類這個小,我建議將它們寫入內聯,因爲它通常會使您的錯誤更易於注意。