简体   繁体   中英

C++ Compiler errors

I'm writing a c++ stack and queue implementation program, I finished the stack part, but when compiling I'm getting these errors

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.

Here is the header file

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

Can you please point out to what I'm doing wrong here?

You always have written

arrayList[...]

what is the name of your class but reading the code it seems like you wanted to write

array[...]

which would access the data.

You should just read your error messages.

  1. You should use array instead of arrayList , which is the name of the class. So just refer to the variable instead.

    The error message you got is something like

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

    When you check the line, you immediately see what is wrong there.

  2. You declare a constructor arrayList::arrayList() , but you do not define it. Either you can drop the declaration, or you should implement it in the cpp-file.

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

    The error message you got is something like

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

    The code could compile, but it did not link. So all declarations may be correct, but a symbol was missing. This is usually the case when you declared something you referred to, but you never defined it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM