【问题标题】:no match for 'operator*' (operand type is 'const string' {aka 'const std::__cxx11::basic_string<char>'})'operator*' 不匹配(操作数类型为 'const string' {aka 'const std::__cxx11::basic_string<char>'})
【发布时间】:2020-04-02 22:30:17
【问题描述】:

我正在尝试编写一个具有 Student 类的 C++ 程序。我正在尝试为name 属性创建一个吸气剂,但我收到了这个错误:

\ApplicationFile.cpp:95:9:错误:'operator*' 不匹配(操作数类型为'const string' {aka 'const std::__cxx11::basic_string'}) 返回*名称;

有什么想法吗?

这是我到目前为止所做的:

#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>

using namespace std;

class Student
{
    char *AM;
    string name;
    int semester, lessons;
    float *passed;

public:
    Student (const char *am, string n); //Constructor that I give only the serial number (AM) and the name
    Student (const char *am, string n, int semester); //Constructor that I give only the serial number (AM), the name and the semester
    Student (const char *am, string n, int semester, int lessons, float * passed); //Constructor that  I give values to all the attributes
    Student (Student &x);
    void setAm (const char *am); //Set serial number
    char * getAm () const; //Get serial number 
    void setName (string n); //Set name
    string * getName () const; //Get name
};

//Only AM and Name
Student::Student(const char *am, string n)
{
    int l = strlen (am);
    AM = new char [l + 1];
    strcpy (AM, am);

    name = n;
    semester = 1;
    lessons = 0;
    *passed = {0};
}

//Only serial number (am), name (n), semester (e)
Student::Student(const char * am, string n, int e)
{
    int l = strlen (am);
    AM = new char [l + 1];
    strcpy (AM, am);
    name = n;
    semester = e;
    lessons = 0;
    *passed = {0};
}

//Constructor that we give values to all variables
Student::Student(const char * am, string n, int e, int perasm, float *p)
{
    int l = strlen (am), i;
    AM = new char [l + 1];
    strcpy (AM, am);
    name = n;
    semester = e;
    lessons = perasm;

    *passed = *p;
}

void Student::setAm(const char *am)
{
    delete [] AM;
    int l = strlen(am);
    AM = new char[l + 1];
    strcpy (AM, am);
}

char * Student::getAm() const
{
    return AM;
}

void Student::setName (const string s)
{
    name = s;
}

string * Student::getName () const
{
    return *name;
    //return c;
}

int main()
{
    Student Kostas("123", "Kostas");
    cout << Kostas.getAm() <<endl;
    Kostas.setAm("354");
    cout << Kostas.getAm() <<endl;

    float p[] = {5.1, 4.4, 0.0, 0.0, 0.0};
    Student Giwrgos("678", "Giwrgos", 6, 5, p);
    cout << Giwrgos.getName();
    return 0;
}

【问题讨论】:

  • 你的代码中充满了指针——为什么? std::string*通常是一个错误的标志。
  • 附带说明,float *passed; 永远不会被指定指向任何东西,所以*passed = {0}; 在运行时会有未定义的行为

标签: c++ class pointers operator-keyword getter


【解决方案1】:

由于错误状态为运算符* 没有mach,操作数类型为const string,该操作没有意义,您试图取消引用非指针变量并将其作为指针返回。

如果返回name的地址就可以返回一个指针:

const string *Student::getName () const
{
    return &name;    
}

您可以/应该返回参考:

const string& Student::getName () const
{
    return name;
}

【讨论】:

  • 行不通,我就做吧 const string getName () const;
  • @Costas96,这两个选项都应该有效,您是否更改了声明?请注意,通过引用传递是更好的选择。
  • 是的,* 仍然是同样的错误。但是使用 const string& Student::getName () const 它可以工作。
【解决方案2】:

name 成员被声明为 string 对象,而不是指向 string 对象的 string* 指针。您的getName() 方法被声明为返回string* 指针,但它试图使用* 运算符将name 对象转换为指针,这将不起作用。 std::string 没有实现这样的 operator*,这就是您收到编译器错误的原因。但是,更重要的是,* 运算符根本就不是用来创建指向name 的指针的错误运算符。您需要改用&amp; 地址运算符。

但是,由于getName() 被声明为const,其隐含的this 指针为const,因此它将name 作为const 对象访问。您不能返回指向const 对象的pointer-to-non-const(不使用您应该避免的const_cast)。

非变异的 getter 没有充分的理由返回指向内部数据的 指向非 const 的指针。它应该返回一个 pointer-to-const,例如:

const string* Student::getName () const;
// or: string const * Student::getName () const;

...

const string* Student::getName () const
// or: string const * Student::getName () const
{
    return &name;
}

然后,当您将string 传递给std::cout 时,您需要取消引用该指针,例如:

cout << *(Giwrgos.getName());

但是,由于指针永远不能为空,所以最好通过 reference-to-const 而不是 pointer-to-const 返回 name 对象em>:

const string& Student::getName () const;
// or: string const & Student::getName () const;

...

const string& Student::getName () const
// or: string const & Student::getName () const
{
    return name;
}

...

cout << Giwrgos.getName();

或者,您可以按值返回name 对象(这将返回string 数据的副本):

string Student::getName () const;

...

string Student::getName () const
{
    return name;
}

...

cout << Giwrgos.getName();

【讨论】:

    【解决方案3】:

    在Student类中,改变

    string * getName () const; //Get name

    string * getName (); //Get name

    然后改变:

    string * Student::getName () const
    {
        return *name;
        //return c;
    }
    

    到:

    string * Student::getName ()
    {
        return &name;
        //return c;
    }
    

    【讨论】:

    • 虽然这个解决方案“有效”,但从 getter 中删除尾随的 const 并不是一个好主意。这是一件好事。正如我在回答中所描述的,您应该改用返回值来解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多