user2351234
user2351234

Reputation: 975

C++ Compilation Error. "Stack does not name a type."

I am new to C++ and I am trying to understand stacks, and how classes work, but I can't seem to get my program to compile at all. I keep getting stupid errors, and I tried searching around the web, but couldn't find anything helpful. I apologize in advance if this question is very dumb. I am new to C++ and I didn't know anywhere else to turn to.

Thank you.

Whenever I try compiling(make), I get this error:

stacks.cpp:4:1: error: ‘Stack’ does not name a type stacks.cpp:7:6: error: ‘Stack’ has not been declared stacks.cpp:7:18: error: ‘string’ was not declared in this scope stacks.cpp:7:18: note: suggested alternative: /usr/include/c++/4.6/bits/stringfwd.h:65:33: note:
‘std::string’ stacks.cpp:7:27: error: expected ‘,’ or ‘;’ before ‘{’ token make: * [stacks.o] Error 1

Stack.h

#ifndef _STACK
#define _STACK
// template <class ItemType>;
#include <string>
using namespace std;
class Stack{
    static const int MAX_STACK = 10;
    private:
        string data[MAX_STACK];
        int top; 
    public:
        Stack();
        bool pop(); 
        bool push(string item);
        string peek();
        bool isEmpty();
};
#endif

Stack.cpp

  #include <cassert>

    Stack::Stack(){
        top = -1;
    }
    bool Stack::Push(string s){
        bool result = false;
        if(top > MAX_STACK - 1){
        ++top;
        data[top] = s;
        result = true;
        }
        return result;
    }
    bool Stack::Pop(){
        bool result = false;
        if(!isEmpty()){
            --top;
            result = true;
        }
        return result;
    }
    string Stack::peek() const{
        assert(!isEmpty());
        return data[top];
    }

Tester.cpp

#include <iostream>
#include <string>
#include <cstdlib>

#include "stacks.h"
int main(){
    Stack test; 
    test.push("Hello");
    test.push("Yes!");
    while(!test.isEmpty()){
        cout << test.peek();
        test.pop();
    }
}

MakeFile:

CXX = g++

CXXFLAGS = -Wall -ansi -std=c++0x

TARGET = execute

OBJS = Tester.o stacks.o

$(TARGET) : $(OBJS)
    $(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJS)

Tester.o : Tester.cpp
    $(CXX) $(CXXFLAGS) -c -o Tester.o Tester.cpp

stacks.o : stacks.cpp stacks.h
    $(CXX) $(CXXFLAGS) -c -o stacks.o stacks.cpp

.PHONY : clean
clean:
    rm $(OBJS)  

Upvotes: 0

Views: 4239

Answers (2)

HadeS
HadeS

Reputation: 2038

You forgot to include Stack.h in Stack.cpp .... also you are including stacks.h which is not present include Stack.h in Tester.cpp...hope this will help..

Upvotes: 2

Damien Black
Damien Black

Reputation: 5647

You need to include stack.h in your file stack.cpp.

Also:

#include "stacks.h"

Should be stack.h if that is the name of the file.

Upvotes: 1

Related Questions