Wednesday, 1 June 2016

c++ - Function receiving a const struct and cin commend




  1. To the best of my knowledge, when a function receives a const
    parameter, the function cannot change it. so, what supposed to
    happen when the function should change the parameter? (For instance
    the function contains "cin" commend to the const parameter). Would
    it be compilation error? or would it run but the parameter
    don't change in practice?

  2. I tried to do some tests in the code below. When I set from void

    read_student (Student students[], int size)
    to void read_student
    (const Student students[], int size)
    , I receive the following
    error messages (these are only some of them). Does this happen
    because the combination of the 'const' parameter and the 'cin'
    commend? If it is, how am I supposed to understand that from
    these messages?




|19|error: no match for 'operator>>' (operand types are 'std::istream

{aka std::basic_istream}' and 'const char [20]')|



|19|error: invalid initialization of non-const reference of type
'bool&' from an rvalue of type 'bool'|



|19|error: invalid conversion from 'const char*' to 'short int'
[-fpermissive]|



|19|error: cannot bind rvalue '(short int)((int)(&(students +
((sizetype)(((unsigned int)i) * 24u)))->Student::name))' to 'short

int&'|



|19|error: invalid conversion from 'const char*' to 'short unsigned
int' [-fpermissive]|



|19|error: cannot bind rvalue '(short unsigned int)((int)(&(students +
((sizetype)(((unsigned int)i) * 24u)))->Student::name))' to 'short
unsigned int&'|





#include 

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 {
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 cout << "name: " << students[i].name << "\taverage: " << students[i].avg <}





int main()
{
Student students[max_students];
read_student(students, max_students);
cout << "ell students: \n";
print_student(students, max_students);



return 0;
}

Answer



You get a compilation error because the operator>> doesn't apply to a constant variable as you can see in the doc.
So the error happens because of the combination of setting the variable to a constant and using operator>>. Removing one or the other solves the issue



Here is a sample code you can try out to see for yourself.



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
}


In your case the function constructor should be read_student(Student students[], int size) since students[] is modified in the function.




The error line error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream}' and 'const char [20]') indicates that the no operator>> overloading exists for this specific variable type e.g const char[].



I hope this answered your question.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...