【发布时间】:2014-07-13 17:58:10
【问题描述】:
我确定这在网站上的某个地方得到了回答,但找不到它......我正在用 C++ 在 VS10 下编写。我正在写一个包含学生详细信息的课程。其中一位成员是
string studentName[30];
应该有一个函数可以根据请求返回这个字符串,这可以使用传统的 C 字符串和一个指针来完成,但是我想使用 C++ 字符串。
我的 get 函数如下所示:
string Student::getName()
{
return studentName;
}
在编译时,我从 VS10 收到此错误:
错误 1 错误 C2664: 'std::basic_string<_elem>::basic_string(const std::basic_string<_elem> &)' : 无法转换参数 1 从 'std::string [30]' 到 'const std::basic_string<_elem> &' f:\c++\hw1\hw1\hw3\hw3.cpp 56 1 HW3
我不确定这意味着什么。如果有人能澄清我将不胜感激。此外,在这些 get 函数中,通常会返回对字符串或实际文字值的引用(希望这是正确的术语)。
这样声明的学生姓名:
protected:
string studentName[30];
int studentGrades[8];
int studentAge;
};
【问题讨论】:
-
在您的班级中如何定义 studentName?
-
string studentName[30];是一个包含 30 个字符串的数组 - 这真的是您想要的吗? -
studentName被定义为不是字符串,而是由 30 个字符串组成的数组。您不能将字符串数组作为字符串传递。 -
您只需将其声明为字符串 studentName;够了
-
@at0ma - 你是什么意思?