C++ VS错误C2664(函数返回字符串)C++;
我确信这个问题在网站上的某个地方得到了回答,但是找不到。。。我在VS10中用C++编写。我正在写一个包含学生详细信息的课程。其中一名成员是C++ VS错误C2664(函数返回字符串)C++;,c++,string,return,C++,String,Return,我确信这个问题在网站上的某个地方得到了回答,但是找不到。。。我在VS10中用C++编写。我正在写一个包含学生详细信息的课程。其中一名成员是 string studentName[30]; 应该有一个函数,它在请求时返回这个字符串,这可以使用传统的C字符串和指针来完成,但是我想使用C++字符串。 我的get函数如下所示: string Student::getName() { return studentName; } 编译时,我从VS10中得到以下错误: 错误1错误C2664: 's
string studentName[30];
应该有一个函数,它在请求时返回这个字符串,这可以使用传统的C字符串和指针来完成,但是我想使用C++字符串。 我的get函数如下所示:
string Student::getName()
{
return studentName;
}
编译时,我从VS10中得到以下错误:
错误1错误C2664:
'std::basic_string::basic_string(常量
std::basic_string&'):无法转换参数1
从“std::string[30]”到“const std::basic_string”
&'f:\c++\hw1\hw1\hw3\hw3.cpp 56 1 hw3
我不知道这是什么意思。如果有人能澄清,我将不胜感激。此外,在这些get函数中,返回字符串或实际文字值的引用是很常见的(希望这是正确的行话)
学生姓名声明如下:
protected:
string studentName[30];
int studentGrades[8];
int studentAge;
};
studentName
是一个字符串*
(数组->指针),但您可以从函数返回一个字符串
。错误消息说明了一切。返回单个字符串或
string* Student::getName()
{
return studentName;
}
您将数据成员studentName定义为具有类型
字符串[30]
string studentName[30];
同时函数getName具有返回类型string
string Student::getName()
现在请回答编译器如何将string[30]
类型的对象转换为string
类型的对象
我想你的意思是
string Student::getName()
{
return studentName;
}
protected:
string studentName;
int studentGrades[8];
int studentAge;
};
这不是
string studentName[30]
而是应该简单地string studentName
,因为我看不出用30个字符串存储学生的名字有什么意义,尽管在巴西可能有包含30个单词的名字。studentName在你的类中是如何定义的?string studentName[30]
是一个包含30个字符串的数组-这真的是您想要的吗?studentName
定义为一个包含30个字符串的数组,而不是一个字符串。不能将字符串数组作为字符串传递。只需将其声明为string studentName;它是enougth@at0ma-什么意思?问题在于成员声明,而不是acces函数,我冒昧地建议string studentName代码>是有意的。