簡體   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