| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1660 人关注过本帖
标题:为什么出现这个错误?
只看楼主 加入收藏
hffjhhh
Rank: 1
等 级:新手上路
帖 子:127
专家分:7
注 册:2019-4-10
结帖率:90.38%
收藏
已结贴  问题点数:14 回复次数:7 
为什么出现这个错误?
以下代码主要为实现在栈中添加和删除结构,但显示有很多错误。
比如这行有以下错误:
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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:14 
你的 struct Item 定义在哪里?
2021-01-24 20:22
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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
回复 3楼 hffjhhh
你下次把题目也贴出来。因为你的代码逻辑错误太多,猜不出题目要求是什么

程序代码:
struct Item
{
    char fullname[35];
    double payment;
};

class Stack
{
public:
    Stack() : top_(0)
    {
    }

    bool isempty() const
    {
        return top_==0;
    }
    bool isfull() const
    {
        return top_==maxsize_;
    }
    bool push( const Item& st )
    {
        if( isfull() )
            return false;
        arr_[top_++] = st;
        return true;
    }
    bool pop( Item& st )
    {
        if( isempty() )
            return false;
        st = arr_[--top_];
        return true;
    }

private:
    static const size_t maxsize_ = 10;
    Item arr_[maxsize_];
    size_t top_;
};

#include <iostream>
using namespace std;

int main( void )
{
    Stack asd;
    double ui = 0;
    for( char ch; cin>>ch && ch!='Q' && ch!='q'; )
    {
        switch( ch )
        {
        case 'A':
        case 'a':
            if( asd.isfull() )
                cout << "Stack is full.\n";
            else
            {
                Item b = { "qwer", 0 };
                cout << "输入金额: ";
                cin >> b.payment;
                asd.push( b );
            }
            break;
        case 'P':
        case 'p':
            if( asd.isempty() )
                cout << "Stack is empty.\n";
            else
            {
                Item b;
                asd.pop( b );
                ui += b.payment;
                cout << "payment结构成员的值为:" << b.payment << '\n';
                cout << "ui的值为:" << ui << endl;
            }
            break;
        }
    }
    return 0;
}


一个可能的执行结果是
p
Stack is empty.
a
输入金额: 1
a
输入金额: 2
a
输入金额: 3
a
输入金额: 4
a
输入金额: 5
a
输入金额: 6
a
输入金额: 7
a
输入金额: 8
a
输入金额: 9
a
输入金额: 10
p
payment结构成员的值为:10
ui的值为:10
p
payment结构成员的值为:9
ui的值为:19
p
payment结构成员的值为:8
ui的值为:27
p
payment结构成员的值为:7
ui的值为:34
p
payment结构成员的值为:6
ui的值为:40
p
payment结构成员的值为:5
ui的值为:45
p
payment结构成员的值为:4
ui的值为:49
p
payment结构成员的值为:3
ui的值为:52
p
payment结构成员的值为:2
ui的值为:54
p
payment结构成员的值为:1
ui的值为:55
p
Stack is empty.
q
2021-01-25 09:22
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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
回复 5楼 hffjhhh
题目挺恶心的,因为C++就不应该这么写,有点儿侮辱C++了

#include<iostream>
#include<cctype>

//typedef unsigned long Item;
struct customer{
    char fullname[35];
    double payment;
};
typedef customer 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;
    Item po;
    double total = 0;

    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;

            cout<<"Enter a PO number to add.\n";
            cout << "fullname: ";
            cin >> po.fullname;
            cout << "payment: ";
            cin >> po.payment;

            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";
                cout << "PO# {" << po.fullname << ", " << po.payment << "} popped\n";
                total += po.payment;
                cout << "total = " << total << '\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;
}


一个可能的输出是

Please enter A to add a purchase order,
P to process a PO,or Q to quit.
a
Enter a PO number to add.
fullname: abc
payment: 123
Please enter A to add a purchase order,
P to process a PO,or Q to quit.
a
Enter a PO number to add.
fullname: def
payment: 456
Please enter A to add a purchase order,
P to process a PO,or Q to quit.
p
PO# {def, 456} popped
total = 456
Please enter A to add a purchase order,
P to process a PO,or Q to quit.
p
PO# {abc, 123} popped
total = 579
Please enter A to add a purchase order,
P to process a PO,or Q to quit.
p
Stack is empty.Please enter A to add a purchase order,
P to process a PO,or Q to quit.
q
bye
2021-01-26 14:02
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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
回复 7楼 hffjhhh
那你知道 bool pop(const Item &st)const 中的 const 是什么意思吗?

你既然说不更改对象的逻辑值,但实际上你又在更改
bool Stack::pop(const Item &st)const{
    if(top>0){
        st=a[--top];
        ui+=st.payment;
        return true;
    }
    else
        return false;
}
2021-01-28 11:41
快速回复:为什么出现这个错误?
数据加载中...
 
   



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

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