【问题标题】:Function receiving a const struct and cin commend接收 const 结构和 cin 推荐的函数
【发布时间】:2018-09-08 05:02:03
【问题描述】:
  1. 据我所知,当函数接收到 const 参数,函数不能改变它。所以,应该怎么做 当函数应该更改参数时会发生什么? (例如 该函数包含对 const 参数的“cin”推荐)。将 是编译错误吗?或者它会运行但参数 在实践中不改变?
  2. 我尝试在下面的代码中做一些测试。当我从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))' 到 'short 整数&'|

|19|错误:从 'const char*' 到 'short unsigned 的无效转换 int' [-fpermissive]|

|19|错误:无法绑定右值'(short unsigned int)((int)(&(students + ((sizetype)(((unsigned int)i) * 24u)))->Student::name))' 到 'short 无符号整数&'|

#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;
}

【问题讨论】:

    标签: c++ c++11


    【解决方案1】:

    您会收到编译错误,因为 operator>> 不适用于常量变量,正如您在 doc 中看到的那样。 所以错误是由于将变量设置为常量和使用运算符>>的组合而发生的。删除一个或另一个可以解决问题

    这是一个示例代码,您可以自己尝试查看。

    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&gt;&gt;' (operand types are 'std::istream {aka std::basic_istream}' and 'const char [20]') 表示此特定变量类型存在 no operator>> 重载,例如 const char[]。

    我希望这回答了你的问题。

    【讨论】:

    • 常数...什么? cin 不是“恒定输入流”。
    【解决方案2】:
    void print_student (Student *students, int size)
    {
        for (int i=0; i<size; i++)
            cout << "name: " << students[i].name<<"\taverage<<students[i].avg<<endl;
    
    }
    

    并称之为:

    print_student (&students,  size);
    

    【讨论】:

    • 确实,在print_student中调用std::ostream::operator&lt;&lt;是没有问题的,因为它不会修改值(只是打印出来)你可以完美地声明void print_student(const Student students[], int size);
    【解决方案3】:

    看起来像家庭作业... 给你:

    void read_student(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;
        }
    }
    

    提示很明显,//const Student VS Student,现在这个东西是一个常数就是常数,意思是创建它之后它的值不能改变。它们是确保在与它们交互时必须创建新容器的好方法,而不是与容器内的值混合。

    【讨论】:

      猜你喜欢
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多