| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1590 人关注过本帖
标题:为什么出现这个错误?
取消只看楼主 加入收藏
hffjhhh
Rank: 1
等 级:新手上路
帖 子:127
专家分:7
注 册:2019-4-10
结帖率:90.38%
收藏
已结贴  问题点数:14 回复次数:3 
为什么出现这个错误?
以下代码主要为实现在栈中添加和删除结构,但显示有很多错误。
比如这行有以下错误:
Item a[MAX];

[Error] field 'a' has incomplete type 'Item [10]'
代码如下:
程序代码:
#include<iostream>
using namespace std;
typedef struct Item;
class Stack{
private:
    double ui=0;
    int top;
    struct customer{
        char fullname[35];
        double payment;
    };
    enum{MAX=10};
    Item a[MAX];
public:
    Stack(){
        top=0;                                           
    };

    bool isemptey()const;
    bool isfull()const;
    bool push(const Item &st)const;
    bool pop(const Item &st)const;
};

bool Stack::isempty()const{
    return top==0;
}
bool Stack::isfull()const{
    return top==MAX;
}
bool Stack::push(const Item &st)const{
    if(top<MAX){
        a[top++]=st;
        return true;
    }
    else{
        return false;
    }
}
bool Stack::pop(const Item &st)const{
    if(top>0){
        st=a[--top];
        ui+=st.payment;
        return true;
    }
    else
        return false;
}
int Stack::show(){
    cout<<"ui的值为:"<<ui<<endl;
}

int main(){
    Stack asd;
    char ch;
    Item b={"qwer",44.3};
    while(cin>>ch&&ch!='Q'){
        while(cin.get()!="\n"){
            continue;
        }
    switch(ch){
        case 'A':
        case 'a':
            cin>>b.payment;
            if(asd.isfull())
                cout<<"Stack is full.";
            else
                asd.push(b);
            break;
        case 'p':
        case 'P':
            cin>>b.payment;
            if(asd.isempty())
                cout<<"Stack is empty.";
            else{
                asd.pop(b);
                cout<<"payment结构成员的值为:"<<b.payment<<endl;
                asd.show();
            }
            break;
        }
    }
    return 0;
}
搜索更多相关主题的帖子: Stack bool top Item const 
2021-01-24 12:25
hffjhhh
Rank: 1
等 级:新手上路
帖 子:127
专家分:7
注 册:2019-4-10
收藏
得分:0 
以下是引用rjsp在2021-1-24 20:22:49的发言:

你的 struct Item 定义在哪里?

改为这样后,又有新的错误,
显示这行出现以下错误,
a[top++]=st;

[Error] increment of member 'Stack::top' in read-only object
代码如下:
程序代码:
#include<iostream>
using namespace std;
typedef struct Item;
class Stack{
private:
    double ui=0;
    int top;
    struct Item{
        char fullname[35];
        double payment;
    };
    enum{MAX=10};
    Item a[MAX];
public:
    Stack(){
        top=0;                                           
    };

    bool isempty()const;
    bool isfull()const;
    bool push(const Item &st)const;
    bool pop(const Item &st)const;
};

bool Stack::isempty()const{
    return top==0;
}
bool Stack::isfull()const{
    return top==MAX;
}
bool Stack::push(const Item &st)const{
    if(top<MAX){
        a[top++]=st;
        return true;
    }
    else{
        return false;
    }
}
bool Stack::pop(const Item &st)const{
    if(top>0){
        st=a[--top];
        ui+=st.payment;
        return true;
    }
    else
        return false;
}
int Stack::show(){
    cout<<"ui的值为:"<<ui<<endl;
}

int main(){
    Stack asd;
    char ch;
    Item b={"qwer",44.3};
    while(cin>>ch&&ch!='Q'){
        while(cin.get()!="\n"){
            continue;
        }
    switch(ch){
        case 'A':
        case 'a':
            cin>>b.payment;
            if(asd.isfull())
                cout<<"Stack is full.";
            else
                asd.push(b);
            break;
        case 'p':
        case 'P':
            cin>>b.payment;
            if(asd.isempty())
                cout<<"Stack is empty.";
            else{
                asd.pop(b);
                cout<<"payment结构成员的值为:"<<b.payment<<endl;
                asd.show();
            }
            break;
        }
    }
    return 0;
}
2021-01-24 23:17
hffjhhh
Rank: 1
等 级:新手上路
帖 子:127
专家分:7
注 册:2019-4-10
收藏
得分:0 
回复 4楼 rjsp
考虑下面结构声明:
struct customer{
    char fullname[35];
    double payment;
};

编写一个程序,它从栈中添加和删除customer结构(栈用Stack类声明表示)。每次customer结构被删除时,起payment的值都被加入到总数中,并报告总数。注意:应该可以直接使用Stack类而不做修改,只需修改typedef声明,使Item的类型为customer,而不是unsigned long即可。
使用Stack类的程序如下:
程序代码:
#include<iostream>
#include<cctype>
typedef unsigned long Item;
class Stack{
private:
    int top;
    enum{MAX=10};
    Item items[MAX];
public:
    Stack();
    bool isempty()const;
    bool isfull()const;
    //push() returns false if stack already is full,true otherwise
    bool push(const Item &item);//add item to stack
    //push() returns false if stack already is empty,true otherwise
    bool pop(Item &item);//pop top into item
};

Stack::Stack(){
    top=0;
}
bool Stack::isempty()const{
    return top==0;
}
bool Stack::isfull()const{
    return top==MAX;
}
bool Stack::push(const Item &item){
    if(top<MAX){
        items[top++]=item;
        return true;
    }
    else{
        return false;
    }
}
bool Stack::pop(Item &item){
    if(top>0){
        item=items[--top];
        return true;
    }
    else
        return false;
}


int main(){
    using namespace std;
    Stack st;
    char ch;
    unsigned long po;
    cout<<"Please enter A to add a purchase order,\n"
        <<"P to process a PO,or Q to quit.\n";
    while(cin>>ch&&toupper(ch)!='Q'){
        while(cin.get()!='\n'){
            continue;
        }
        if(!isalpha(ch)){
            cout<<'\a';
            continue;
        }
    switch(ch){
        case 'A':
        case 'a':
            cout<<"Enter a PO number to add:";
            cin>>po;
            if(st.isfull())
                cout<<"Stack is full.";
            else
                st.push(po);
            break;
        case 'p':
        case 'P':
            if(st.isempty())
                cout<<"Stack is empty.";
            else{
                st.pop(po);
                cout<<"PO#"<<po<<"popped\n";
            }
            break;
        }
        cout<<"Please enter A to add a purchase order,\n"
            <<"P to process a PO,or Q to quit.\n";
    }
    cout<<"bye\n";
    return 0;
}



以上就是题目。如何按照题意进行相应的修改?
2021-01-25 20:25
hffjhhh
Rank: 1
等 级:新手上路
帖 子:127
专家分:7
注 册:2019-4-10
收藏
得分:0 
回复 6楼 rjsp
如果抛开题目而言,为什么我以下这行会出现这个错误?
a[top++]=st;

[Error] increment of member 'Stack::top' in read-only object

代码如下:
程序代码:
#include<iostream>
using namespace std;
struct customer{
    char fullname[35];
    double payment;
    };
typedef customer Item ;
class Stack{
private:
    double ui=0;
    int top;

    enum{MAX=10};
    Item a[MAX];
public:
    Stack(){
        top=0;                                           
    };

    bool isempty()const;
    bool isfull()const;
    bool push(const Item &st)const;
    bool pop(const Item &st)const;
};

bool Stack::isempty()const{
    return top==0;
}
bool Stack::isfull()const{
    return top==MAX;
}
bool Stack::push(const Item &st)const{
    if(top<MAX){
        a[top++]=st;
        return true;
    }
    else{
        return false;
    }
}
bool Stack::pop(const Item &st)const{
    if(top>0){
        st=a[--top];
        ui+=st.payment;
        return true;
    }
    else
        return false;
}
int Stack::show(){
    cout<<"ui的值为:"<<ui<<endl;
}

int main(){
    Stack asd;
    char ch;
    Item b={"qwer",44.3};
    while(cin>>ch&&ch!='Q'){
        while(cin.get()!="\n"){
            continue;
        }
    switch(ch){
        case 'A':
        case 'a':
            cin>>b.payment;
            if(asd.isfull())
                cout<<"Stack is full.";
            else
                asd.push(b);
            break;
        case 'p':
        case 'P':
            cin>>b.payment;
            if(asd.isempty())
                cout<<"Stack is empty.";
            else{
                asd.pop(b);
                cout<<"payment结构成员的值为:"<<b.payment<<endl;
                asd.show();
            }
            break;
        }
    }
    return 0;
}
2021-01-27 21:39
快速回复:为什么出现这个错误?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021944 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved