【问题标题】:error: expected declaration before } token (when trying to compile)错误:} 标记之前的预期声明(尝试编译时)
【发布时间】:2014-03-03 07:41:40
【问题描述】:

我遇到了问题。我在文件 stack.hstack.hpp 中有堆栈的模板和实现。然后我有一个 in2out.cpp 文件,它将中缀表达式转换为后缀表达式并对其进行评估。在 in2out.cpp 的开头我是“#include stack.h”。在 stack.h(这是我的界面) 中,我“#include stack.hpp”(stack.hpp 是我的实现)。我知道这很奇怪,但这是我必须这样做的方式。

无论如何,我的问题是,当我试图将这 3 个文件编译在一起时。我只收到 stack.h 中的 1 个错误(我的界面,如下所列)。错误是:

In file included from in2post.cpp:7:
stack.h:49: error: expected declaration before â}â token

第 49 行接近底部。这是我的 stack.h 文件。

#ifndef STACK_H
#define STACK_H

#include <iostream>
#include <vector>


namespace cop4530 {

template<typename T>
class Stack {

 private:
 std::vector<T> v;

 public:
 Stack();
 ~Stack();
 Stack (const Stack<T>&);
 //Stack (Stack<T>&& );
 Stack<T>& operator= (const Stack <T>&);
 //Stack<T> & operator=(Stack<T> &&);
 bool empty() const;
 void clear();
 void push(const T& x);
 //void push(T && x);
 void pop();
 T& top();
 const T& top() const;
 int size() const;
 void print(std::ostream& os, char ofc = ' ') const;

};

template<typename T>
std::ostream& operator<< (std::ostream& os, const Stack<T>& a);

template<typename T>
bool operator== (const Stack<T>& a, const Stack <T>& b);

template<typename T>
bool operator!= (const Stack<T>& a, const Stack <T>& b);

template<typename T>
bool operator< (const Stack<T>& a, const Stack <T>& b);

#include "stack.hpp"

}    //this is line 49

#endif

这是我的生成文件:

CC = g++
FLAGS = -Wall -pedantic
DDD = -g

in2post.x: in2post.cpp stack.h stack.hpp
    $(CC) $(FLAGS) $(DDD) -o in2post.x in2post.cpp

编辑:这是 Stack.hpp 的开头:(告诉我是否应该更改或添加一些内容) EDIT2:我刚刚决定包含整个 Stack.hpp。一些功能被屏蔽了,因为我不需要它们或无法弄清楚它们

#include <iostream>
#include <vector>

template<typename T>
Stack<T>::Stack(){}

template<typename T>
Stack<T>::~Stack(){
   v.clear();
}

template<typename T>
Stack<T>::Stack(const Stack<T> & S){
    v = S.v;
}
/*
template<typename T>
Stack<T>::Stack(Stack<T> &&){

}
*/
template<typename T>
Stack<T> & Stack<T>::operator=(const Stack<T> & S){
    v = S.v;
    return *this;
}
/*
template<typename T>
Stack<T>::Stack<T> & Stack<T>::operator=(Stack<T> &&){

}
*/
template<typename T>
bool Stack<T>::empty() const {
    return v.empty();
}

template<typename T>
void Stack<T>::clear(){
    while (!v.empty())
        v.clear();
}

template<typename T>
void Stack<T>::push(const T& x){
    v.push_back(x);
}
/*
template<typename T>
void Stack<T>::push(T && x){

}
*/
template<typename T>
void Stack<T>::pop(){
    v.pop_back();
}

template<typename T>
T& Stack<T>::top(){
    return v[v.size()-1];
}

template<typename T>
const T & Stack<T>::top() const{
    return v[0];
}

template<typename T>
int Stack<T>::size() const {
    return v.size();
}

template<typename T>
void Stack<T>::print(std::ostream & os, char ofc) const {
    for(int i = 0; i < v.size(); i++)
    os << v[i] << ofc;
}

/*------------------NON-MEMBER GLOBAL FUNCTIONS-----------------*/

template<typename T>
std::ostream & operator<<(std::ostream & os, const Stack<T> & a) {
    a.print(os);
    return os;
}


template <typename T>
bool operator==(const Stack<T> & a, const Stack<T> & b) {
    if(a.size() != b.size())
            return false;
    else {
        Stack<T> c = a;
        Stack<T> d = b;
        bool retVal = true;

        while(!c.empty()) {
            if(c.top() == d.top()) {
             c.pop();
             d.pop();
            }
            else {
             retVal = false;
             break;
            }
        }
    return retVal;
    }
}


template <typename T>
bool operator!=(const Stack<T> & a, const Stack<T> & b) {
    return !(a == b);
}

template <typename T>
bool operator<(const Stack<T> & a, const Stack<T> & b) {
    Stack<T> c = a;
    Stack<T> d = b;
    bool retVal = true;

    while(!c.empty()) {
        if(!(c.top() < d.top())) {
          retVal = false;
          break;
        }
        else {
          c.pop();
          d.pop();
        }
    }
    return retVal;
}

【问题讨论】:

  • @ValekHalfHeart 这是命名空间。我怀疑“stack.hpp”包含头有一些语法问题。为什么还是放在最后?
  • 告诉我们stack.hpp里面的东西,里面的东西可能是导致问题的原因。
  • 您可以将 } 放在包含之前
  • 你忘记关闭stack.h中的命名空间了吗?
  • 放置“#include stack.hpp”时出现错误。在命名空间之外,甚至在顶部。我不知道为什么

标签: c++ stack


【解决方案1】:

stack.hpp 中的operator&lt; 有一个额外的右花括号,它会提前关闭命名空间。

【讨论】:

  • 奇怪...我认为当我将它复制并粘贴到网站时可能是一个意外,因为它在我的 stack.hpp 文件中不一样...但是,我不不认为 stack.hpp 中有命名空间。
  • 修正了错误。对于那个很抱歉。但由于某种原因仍然出现错误。我再看一遍看能不能弄明白
  • 没关系,我想通了。原来我在 stack.hpp 中有一个额外的大括号,但它在我的代码末尾以下几行,我看不到它。大声笑多么尴尬。谢谢!
猜你喜欢
  • 1970-01-01
  • 2023-01-19
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多