与'operator *'不匹配(操作数类型为'const string'{aka'const std :: __ cxx11 :: basic_string '}) ]

问题描述 投票:0回答:2

我正在尝试编写具有类Student的C ++程序。我正在尝试为name属性设置吸气剂,但出现此错误:

\ ApplicationFile.cpp:95:9:错误:'operator *'不匹配(操作数类型为'const string'{aka'const std :: __ cxx11 :: basic_string'})返回* 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;
}

我正在尝试编写一个有学生类的C ++程序。我正在尝试为name属性获取吸气剂,但出现此错误:\ ApplicationFile.cpp:95:9:错误:'...

c++ class pointers operator-keyword getter
2个回答
0
投票
不匹配

在学生班级中,将string * getName () const; //Get name更改为string * getName (); //Get name,然后更改:


0
投票

由于错误指出运算符*没有运算符,操作数类型为const string

© www.soinside.com 2019 - 2024. All rights reserved.