C++ 链接堆栈模板和一系列错误

C++ 链接堆栈模板和一系列错误,c++,stack,C++,Stack,谁能告诉我我做错了什么?如果我注释掉主函数中的所有语句,编译器不会抱怨。因此,我认为它与主函数有关,就在我创建LinkedStack的新实例时 LinkedNode.h #include<memory> template<class T> class LinkedNode { friend std::ostream& operator<<(std::ostream& os, const LinkedNode<T>&

谁能告诉我我做错了什么?如果我注释掉主函数中的所有语句,编译器不会抱怨。因此,我认为它与主函数有关,就在我创建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;
}
#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();
}
#包括
模板
类链接节点
{

朋友STD::OsFrase&运算符

错误在你的头文件中。当你注释< <代码>主< /代码>函数时,你不会看到编译器错误的原因是模板是如何工作的(它们基本上是创建一个对象的基础,但是直到你试图实例化一个对象之前才真正创建这个定义)。 您的LinkedNode.h文件中似乎缺少一个头保护。请在其顶部放置一个

#pragma
(或使用#ifndef/#define)。但我不认为这是导致错误的原因

您似乎已经声明了非模板版本的
LinkedStack
,或者可能在某个地方有以下语法(您尚未显示):

模板
模板
类LinkedStack{…}

对于这么小的模板类,我建议将它们内联编写,因为这通常会使您的错误更容易被注意到。

我接受了您的代码,我只做了一个更改,我在LinkedNode.h中注释了
private:
,然后为我编译了它。我看不出发布的代码会给出错误的原因。我唯一能做的建议是ke打算在LinkedNode.h中添加一个include guard。但我认为这里发生了一些尚未发布的事情。我在编译器中看到的唯一问题是私有的:在LinkedNode中,可能的重复可能是最好的建议是重新开始。我从前面的问题中看到,您已经对这段代码进行了相当多的黑客攻击。显然有些问题当你这么做的时候,g出错了。这里的代码并不完美,但是这里没有任何东西会导致你看到的错误。所以从零开始,使用上面发布的代码,而不是其他任何东西。然后看看你是怎么做的。
#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
template<class T>
template<class N>
class LinkedStack {... }