'this'参数的类型为const,但函数没有标记为const [英] 'this' argument has type const but function is not marked const

查看:1723
本文介绍了'this'参数的类型为const,但函数没有标记为const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我在C ++中有点小事,在第二次任务中,我需要使用公共和私人参数等进行类化。基本上,mutator函数将不起作用,因为显然它们不是类型为const?



这是包含类的头文件:

  class Customer {

private:
string PhoneNumber_;
string Name_;
字符串Address_;
$ b public:
string get_PhoneNumber()const {return PhoneNumber_;} // Accessor
const void set_PhoneNumber(unsigned x){PhoneNumber_ = x;} // Mutator

string get_Name()const {return Name_;}
const void set_Name(unsigned x){Name_ = x;}
$ b $ string get_Address()const {return Address_;}
const void set_Address(unsigned x){Address_ = x;}
};

//使用默认值声明CreateCustomer函数原型$ b $ Customer * CreateCustomer(const string& id = BLANK,const string& name = BLANK,const string& address = BLANK);

Customer * CreateCustomer(const string& id,const string& name,const string& address){
Customer * temp = new Customer();

temp-> get_PhoneNumber()= id; //由于存取者和突变号PhoneNumber,Name和Address现在是函数
temp-> get_Name()= name;
temp-> get_Address()=地址;

return temp;
}

这是我在main.cpp文件中得到的错误:

  cout<< \\\\\\\\\\\' 
cout<< Charge [0] .Holder.set_Name()<< (<<<<<<<<<<>>< // DisplayCustomer(customer);

cout<< ,\ n<< Charge [0] .Holder.set_Address()<< \\\
\\\

基本上,确切的错误信息是:



lockquote

成员函数'set_Name'不可行:'this'参数的类型为'const
Customer',但函数不是const类型


它也发生在set_PhoneNumber和set_Address中。任何帮助将不胜感激!感谢!



更新:我得到了它的工作。感谢大家帮助我!

解决方案

如果您想设置一个值,请使用set方法。 get方法只是获取变量,而不是设置一个类的内部变量(如果它们是按照你的方式定义的)。

正确的用法是:客户*创建客户(const string& id,const string& name,const string& address){
Customer * temp =新客户();

temp-> set_PhoneNumber(id);
temp-> set_Name(name);
temp-> set_Address(address);

return temp;
}

另外,您必须更改方法的接口:

  class Customer {

private:
string PhoneNumber_;
string Name_;
字符串Address_;
$ b public:
string get_PhoneNumber()const {return PhoneNumber_;} // Accessor
void set_PhoneNumber(const string& x){PhoneNumber_ = x;} // Mutator
$ b string get_Name()const {return Name_;}
void set_Name(const string& x){Name_ = x;}
$ b $ string get_Address()const {return Address_ ;}
void set_Address(const string& x){Address_ = x;}
};

因为您要设置字符串而不是数字。

使用 const string& 作为函数参数优于字符串,以便在将字符串作为参数传递时不复制字符串。由于它是一个const引用,所以您不必担心该函数可能会操纵输入。

Okay so I'm a bit of a noob at C++ and in my second assignment I am required to make classes with public and private arguments etc, etc. Basically the mutator functions won't work because apparently they're not of type const?

This is the header file with the class:

class Customer {

private:
    string PhoneNumber_;
    string Name_;
    string Address_;

public:
    string get_PhoneNumber() const {return PhoneNumber_;} // Accessor
    const void set_PhoneNumber(unsigned x) {PhoneNumber_ = x;} // Mutator

    string get_Name() const {return Name_;}
    const void set_Name(unsigned x) {Name_ = x;}

    string get_Address() const {return Address_;}
    const void set_Address(unsigned x)  {Address_ = x;}
};

// declare the CreateCustomer function prototype with default values
Customer* CreateCustomer(const string& id = BLANK, const string& name = BLANK, const string& address = BLANK);

Customer* CreateCustomer(const string& id, const string& name, const string& address) {
    Customer* temp = new Customer();

    temp->get_PhoneNumber() = id; // Due to the Accessors and Mutators PhoneNumber, Name and Address are now functions
    temp->get_Name() = name;
    temp->get_Address() = address;

    return temp;
}

And this is the error I get in the main.cpp file:

cout << "\n\nDear ";
    cout << Charge[0].Holder.set_Name() << " (" << Charge[0].Holder.set_PhoneNumber() << ")";  //  DisplayCustomer(customer) ;

    cout << ",\n" << Charge[0].Holder.set_Address() << "\n\n"

Basically, the exact error message is:

Member function 'set_Name' not viable: 'this' argument has type 'const Customer', but function is not type const

It happens with set_PhoneNumber and set_Address as well. Any help would be greatly appreciated! Thanks!

UPDATE: I got it working. Thanks everyone for helping me out!

解决方案

If you want to set a value, use the set method. get Methods are only to obtain variables, not to set the inner variables of a class (If they are defined the way you did).

The correct usage is:

Customer* CreateCustomer(const string& id, const string& name, const string& address) {
    Customer* temp = new Customer();

    temp->set_PhoneNumber( id );
    temp->set_Name( name );
    temp->set_Address( address );

    return temp;
}

Also, you have to alter the interface of your methods:

class Customer {

private:
    string PhoneNumber_;
    string Name_;
    string Address_;

public:
    string get_PhoneNumber() const {return PhoneNumber_;} // Accessor
    void set_PhoneNumber(const string& x) {PhoneNumber_ = x;} // Mutator

    string get_Name() const {return Name_;}
    void set_Name(const string& x) {Name_ = x;}

    string get_Address() const {return Address_;}
    void set_Address(const string& x)  {Address_ = x;}
};

Since you want to set strings and not numbers.

Using const string& as function arguments is better than string to not copy the string when passing it as an argument. Since it is a const reference, you don't have to fear the function could manipulate the input.

这篇关于'this'参数的类型为const,但函数没有标记为const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆