繁体   English   中英

'operator*' 不匹配(操作数类型为 'const string' {aka 'const std::__cxx11::basic_string<char> '})</char>

[英]no match for 'operator*' (operand type is 'const string' {aka 'const std::__cxx11::basic_string<char>'})

我正在尝试编写一个具有 class Student的 C++ 程序。 我正在尝试为name属性创建一个 getter,但出现此错误:

\ApplicationFile.cpp:95:9: error: no match for 'operator*' (operand type is 'const string' {aka 'const std::__cxx11::basic_string'})
  return *name; 

任何想法为什么?

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

#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;
}

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

如果返回name的地址,则可以返回指针:

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

您可以/应该返回参考:

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

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

但是,由于getName()被声明为const ,它的隐式this指针是const ,因此它以const object 访问name 您不能返回指向const object 的指向非 const的指针(不使用const_cast ,您应该避免使用)。

非变异的 getter 没有充分的理由返回指向内部数据的指向非 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());

但是,由于指针永远不会是 null,因此最好通过reference-to-const而不是pointer-to-const返回name object :

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 object(这将返回string数据的副本):

string Student::getName () const;

...

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

...

cout << Giwrgos.getName();

在学生 class 中,更改

string * getName () const; //Get name

string * getName (); //Get name

然后改变:

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

至:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM