c++ 编译器在main中抛出错误,此错误之前未声明

s5a0g9ez  于 2023-07-01  发布在  其他
关注(0)|答案(1)|浏览(198)
#include<iostream>
#include<string>
using namespace std;

class customerNode{
    public:
        int c_id;
        int quantity;
        string c_name;
        string type;
        customerNode* next_node;
};
class Queue{
    public:
    customerNode* front=NULL;
    customerNode* rear=NULL;
    int getc_id();
    string getc_name();
    int getquantity();
    int setc_id(int c_id);
    string setc_name(string c_name);
    int setquantity(int quantity);
    void display();
    void enqueue(int c_id,int quantity,string c_name);
    void dequeue();
    int nor_queue,exp_queue;
};
int Queue::getc_id(){
    int c_id;
    cout<<"enter customer id:"<<endl;
    cin>>c_id;
    return c_id;
    
}
int Queue::getquantity(){
    int quantity;
    cout<<"enter quantity customer purchased:"<<endl;
    cin>>quantity;
    return quantity;
} 
string Queue::getc_name(){
    string c_name;
    cout<<"enter customer name:"<<endl;
    cin>>c_name;
    return c_name;
}
int Queue::setc_id(int c_id){
    return c_id;
    
}
    int Queue::setquantity(int quantity){
        return quantity;
    }
    string Queue::setc_name(string c_name){
        return c_name;
    }
    void Queue:: enqueue(int c_id,int quantity,string c_name){
        int exp_queue,nor_queue;
        cout<<"enter customer information"<<endl;
        customerNode* new_node=new customerNode;
        new_node->c_id=c_id;
        new_node->c_name=c_name;
        new_node->quantity=quantity;
        new_node->next_node=NULL;
        if(front==NULL){
            rear=front;
            rear=new_node;
            rear->next_node=NULL;
        }
        else{
            while(rear->next_node!=NULL)
            rear=rear->next_node;}
            rear->next_node=new_node;
            rear=new_node;
            if(new_node->quantity<=5)
{
                new_node->type="express";
                exp_queue++;
                cout<<"customer entered in express queue"<<endl;
                cout<<"total customer in express queue="<<exp_queue<<endl;
            }
            else{
                new_node->type="normal";
                nor_queue++;
                cout<<"customer entered in normal queue"<<endl;
                cout<<"total customer in normal queue="<<nor_queue<<endl;
            }
        
            }
                void Queue::display(){
                    customerNode* ptr=front;
                    cout<<"normal queue customer information"<<endl;
                        while(ptr!=NULL)
                        {
                            if(ptr->type=="normal"){
                                cout<<"customer name:"<<setc_name(ptr->c_name)<<endl;
                                cout<<"customer id:"<<setc_id(ptr->c_id)<<endl;
                                cout<<"item puchased by customer :"<<setquantity(ptr->quantity)<<endl;
                                nor_queue--;
                                cout<<"total customer in normal queue:"<<nor_queue<<endl;
                            
                        }
                        ptr=ptr->next_node;
                        }
                        
                        cout<<"express queue customer information"<<endl;
                        while(ptr!=NULL)
                        {
                            if(ptr->type=="normal"){
                                cout<<"customer name:"<<setc_name(ptr->c_name)<<endl;
                                cout<<"customer id:"<<setc_id(ptr->c_id)<<endl;
                                cout<<"item purchased by customer :"<<setquantity(ptr->quantity)<<endl;
                                nor_queue--;
                                cout<<"total customer in normal queue:"<<exp_queue<<endl;
                        
            }
        }
        }
        
 main(){
        Queue q;
        char i;
        do{
        q.enqueue(c_id,quantity,c_name );
        cout<<"do you want to enter another customer?input y or Y for yes and n or N for no:";
        cin>>i;
    }
        while(i=='y'||i=='Y');
        q.display();
        return(0);
    
        };`

在主要功能我得到错误c_id,数量,c_name之前没有声明,当我使用int c_id,int quantity,string c_name比它显示预期的主要表达式befor int和strinng..我不知道哪个表达式丢失或如何解决错误,请帮助我解决这个问题我必须尽快提交assing.

wz3gfoph

wz3gfoph1#

一个简单得多的类似错误的例子是:

#include <iostream>
struct foo {
    int x = 0;
    int y = 0;
    void assign(int a, int b){
        x = a;
        y = b;
    }
};

int main()
{
    foo f;
    f.assign(x,y);
}

错误是:

<source>: In function 'int main()':
<source>:14:14: error: 'x' was not declared in this scope
   14 |     f.assign(x,y);
      |              ^
<source>:14:16: error: 'y' was not declared in this scope
   14 |     f.assign(x,y);
      |                ^

xy在类的作用域中声明。foo的示例具有该名称的成员。要访问这些成员,可以写f.xf.y
c_idquantityc_name未在main中声明。我不确定你想做什么,这是太多的代码来解决所有的问题。但是,如果你想在main中声明这个名字的变量,那么你需要这样做:

int main(){    // main must return int
    Queue q;
    char i;
    int c_id = 42;
    int quantity = 0;
    string c_name{"some fancy name"};
    q.enqueue(c_id,quantity,c_name );
    // ...
}

这是一个有点令人惊讶的是,你写的代码与先进的东西,如指针,类和什么,但不知道的范围。尝试搜索“范围”并阅读有关它的信息。
你的代码中有更多的问题。例如,int Queue::setquantity(int quantity){ return quantity;}不设置任何内容。不过,正如我之前所写的,这只是太多的代码来解决所有这些问题。我只能建议你从更少的代码开始,只有当你知道你已经编译并通过测试的代码才能写更多的代码。这不仅仅是给初学者的建议,任何人都在犯错误,你宁愿同时解决一个问题,而不是很多问题。

相关问题