0
votes

I'm sure this was answered somewhere on the site but can not find it... I'm writing under VS10 in C++. I'm writing a class that holds details of a student. One of the members is

string studentName[30];

There should be a function that returns this string on request, this could be done using traditional C strings and a pointer however I would like to use C++ strings.

My get function looks like so:

string Student::getName()
{
    return studentName;
}

On compile, I get this error from VS10:

Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'std::string [30]' to 'const std::basic_string<_Elem,_Traits,_Ax> &' f:\c++\hw1\hw1\hw3\hw3.cpp 56 1 HW3

I'm not sure what this means. If anyone can clarify I would be thankful. Also, in these get functions is it common to return a reference for the string or actual literal values (hope this is the correct lingo).

StudentName declared as such:

protected:
    string studentName[30];
    int  studentGrades[8];
    int  studentAge;
};
2
How studentName is defined in your class?Adrian Toma
string studentName[30]; is an array of thirty strings - is that really what you wanted?RichieHindle
studentName is defined to be not a string, but an array of 30 strings. You cannot pass an array of strings as a string.ach
you simply need to declare it as string studentName; it's enougthAdrian Toma
@at0ma - what do you mean?user34920

2 Answers

2
votes

You defined data member studentName as having type string[30]

string studentName[30];

At the same time function getName has return type string

string Student::getName()

Now please answer how has the compiler to convert an object of type string[30] to an object of type string?

I think you meant the following

string Student::getName()
{
    return studentName;
}
protected:
    string studentName;
    int  studentGrades[8];
    int  studentAge;
};

that is instead of string studentName[30] there should be simply string studentName because I do not see a greate sense to store the name of a student in 30 strings though maybe in Brasil there are names that contains 30 words.:)

0
votes

studentName is a string* (array -> pointer) but you return a string from the function. The error message says it all. Either return a single string or

string* Student::getName()
{
    return studentName;
}