void read_student (Student students[], int size)
到void read_student (const Student students[], int size)
,我收到以下错误消息(这些只是其中的一些)。是否因为“ const”参数和“ cin”推荐值的组合而发生?如果是,我应该如何从这些消息中了解这一点?| 19 |错误:与“ operator >>”不匹配(操作数类型为“ std :: istream {aka std :: basic_istream}”和“ const char [20]”)|
| 19 |错误:根据类型'bool'|的右值对类型'bool&'的非常量引用进行了无效的初始化|
| 19 |错误:从'const char *'到'short int'的无效转换[-fpermissive] |
| 19 |错误:无法将右值'(short int)((int)(&(students +((sizetype)((((unsigned int)i)* 24u)))-> Student :: name))'绑定到'短int&'|
| 19 |错误:从'const char *'到'short unsigned int'的无效转换[-fpermissive] |
| 19 |错误:无法将右值'(short unsigned int)((int)(&(students +((sizetype)((((unsigned int)i)* 24u)))-> Student :: name))绑定到'short unsigned int&'|
#include <iostream>
using namespace std;
const int max_students=3;
struct Student
{
char name [20];
float avg;
};
void read_student (const Student students[], int size) //const Student VS Student
{
for (int i=0; i<size; i++)
{
cout << "enter name and avg for student #" << i+1 << endl;
cin >> students[i].name >> students[i].avg;
}
}
void print_student (const Student students[], int size)
{
for (int i=0; i<size; i++)
cout << "name: " << students[i].name << "\taverage: " << students[i].avg <<endl;
}
int main()
{
Student students[max_students];
read_student(students, max_students);
cout << "ell students: \n";
print_student(students, max_students);
return 0;
}
您会收到编译错误,因为运算符>>不适用于doc中所见的常量。因此,由于将变量设置为常量并使用operator >>的组合而发生错误。卸下其中一个即可解决问题
这是一个示例代码,您可以尝试自己看看。
void Foo(int const x) {
std::cin >> x; // doesn't compile because operator>> not defined for int const
}
void Foo2(int x) {
std::cin >> x; // Compiles properly
}
在你的情况下,函数的构造应read_student(Student students[], int size)
,因为students[]
在功能修改。
错误行error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream}' and 'const char [20]')
指示此特定变量类型(例如)存在no operator >>重载const char[]
。
我希望这能回答您的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句