簡體   English   中英

C ++編譯錯誤。 “堆棧未命名類型。”

[英]C++ Compilation Error. “Stack does not name a type.”

我是C ++的新手,我試圖了解堆棧以及類的工作原理,但似乎根本無法使我的程序進行編譯。 我不斷收到愚蠢的錯誤,我嘗試在網上搜索,但找不到任何有用的信息。 如果這個問題很愚蠢,我事先表示歉意。 我是C ++的新手,我不知道其他任何地方。

謝謝。

每當我嘗試編譯(make)時,都會出現此錯誤:

stacks.cpp:4:1:錯誤:'Stack'未命名類型stacks.cpp:7:6:錯誤:'Stack'未聲明stacks.cpp:7:18:錯誤:'string'不是在此范圍內聲明stacks.cpp:7:18:注意:建議的替代方法:/usr/include/c++/4.6/bits/stringfwd.h:65:33:注意:
'std :: string'stacks.cpp:7:27:錯誤:預期為','或';' 在'{'令牌之前進行: * [stacks.o]錯誤1

堆棧

#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];
    }

測試器

#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)  

你忘了包括Stack.hStack.cpp ....你也被包括stacks.h中不存在包括Stack.hTester.cpp ......希望這將有助於..

您需要包括stack.h在文件stack.cpp

也:

#include "stacks.h"

如果這是文件名, stack.hstack.h

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM