문제

I'm having a problem. I have the template and implementation for a stack in files stack.h and stack.hpp. Then I have an in2out.cpp file that converts infix expressions to outfix expressions and evaluates them. At the beginning of in2out.cpp I "#include stack.h". And inside stack.h (which is my interface) I "#include stack.hpp" (stack.hpp is my implementation). I know this is weird but it's the way I'm required to do it.

Anyways, my problem is, when I'm trying to compile these 3 files together. I get only 1 error which is in stack.h (my interface, listed below). The error is:

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

Line 49 is near the bottom. here is my stack.h file.

#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

Here's my makefile:

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

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

EDIT: Here's the beginning of Stack.hpp: (tell me if I should change or add something) EDIT2: I just decided to include the whole Stack.hpp. Some functions are blanked out cause I didn't need them or couldn't figure them out

#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;
}
도움이 되었습니까?

해결책

Your operator< in stack.hpp has an extra closing curly brace which is closing the namespace early.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top