簡體   English   中英

C ++編譯器錯誤

[英]C++ Compiler errors

我正在編寫一個c ++堆棧和隊列實現程序,完成了堆棧部分,但是在編譯時遇到了這些錯誤

arrayListImp.cpp:18:19: error: expected unqualified-id
                arrayList[++top]= x;
                                ^
arrayListImp.cpp:28:13: error: 'arrayList' does not refer to a value
                itemPoped=arrayList[top];
                          ^
./arrayList.h:3:7: note: declared here
class arrayList{
      ^
arrayListImp.cpp:35:9: error: 'arrayList' does not refer to a value
        return arrayList[top];
               ^
./arrayList.h:3:7: note: declared here
class arrayList{
      ^
arrayListImp.cpp:46:9: error: 'arrayList' does not refer to a value
                cout<<arrayList[i]<<endl;
                      ^
./arrayList.h:3:7: note: declared here
class arrayList{
      ^
4 errors generated.

這是頭文件

#ifndef ARRAYLIST_H

class arrayList{

public:
    arrayList();
    static const int maxSize = 10;
    int array[10];
};

class stack : public arrayList{

public:
    stack();    
    void push(int x);
    void pop();
    int Top();
    int isEmpty();
    void print();
    int x;
    int top;
    int itemPoped;
    int i;
};

#define ARRAYLIST_H
#endif

arrayListImp.cpp

#include <iostream>
#include "arrayList.h"
using namespace std;

//Stack implementation

stack::stack(){

    top = -1;
}

void stack::push(int x){

    if (top == maxSize -1){
        cout<<"Stack overflow"<<endl;
    }
    else{
        arrayList[++top]= x;
        cout<<x<<", is pushed on to the stack"<<endl;
    }
}

void stack::pop(){
    if (top == -1){
        cout<<"Stack underflow"<<endl;
    }
    else{
        itemPoped=arrayList[top];
        top--;
        cout<<itemPoped<<", is poped from the stack"<<endl; 
    }
}

int stack::Top(){
    return arrayList[top];
}

int stack::isEmpty(){
    if (top == -1) return 1;
    return 0;
}

void stack::print(){
    cout<<"Stack: "<<endl;
    for (i = 0; i<=top; i++){
        cout<<arrayList[i]<<endl;

    }
}

arrayListUse.cpp

#include <iostream>
#include "arrayList.h"
using namespace std;


int main()
{
    //Stack testing
    stack S;
    S.push(1);S.print();
    S.push(2);S.print();
    S.push(3);S.print();
    S.pop();S.print();
    S.push(4);S.print();

    //Queue testing



    return 0;
}

您能指出我在這里做錯了嗎?

你總是寫

arrayList[...]

類的名稱是什么,但是閱讀代碼似乎就像是要寫

array[...]

它將訪問數據。

您應該只閱讀錯誤消息。

  1. 您應該使用array而不是arrayList ,它是類的名稱。 因此,只需引用變量即可。

    您收到的錯誤消息是類似

     test.cpp: In member function 'void stack::push(int)': test.cpp:44:18: error: expected unqualified-id before '[' token arrayList[++top]= x; ^ 

    當您檢查線路時,您立即看到那里出了什么問題。

  2. 您聲明一個構造函數arrayList::arrayList() ,但未定義它。 您可以刪除該聲明,或者應該在cpp文件中實現它。

     arrayList::arrayList() { // do some initialization } 

    您收到的錯誤消息是類似

     /tmp/cc4y06YN.o:test.cpp:function stack::stack(): error: undefined reference to 'arrayList::arrayList()' 

    該代碼可以編譯,但是沒有鏈接。 因此,所有聲明可能都是正確的,但是缺少符號。 通常,當您聲明要引用的內容但從未定義它時,就是這種情況。

暫無
暫無

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

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