0

I am trying to use cin to read private members of a class within a member function of the class and i am getting an error [Error] no match for 'operator>>' in 'std::cin >> ((const Course*)this)->Course::courseID' ? Is it possible to do that? Here is my code

//Course.h
#ifndef Course_H
#define Course_H
#include<string>
#include<iostream>
using namespace std;
class Course
{
    private:
        string courseID;
        string courseName;
        int credits;
        Course * nextCourse;
    public:
        void EnterCourse() const;
        void SetNext(Course *);
        string GetCourseID() const;
        string GetCourseName() const;
        int GetCredits() const;
};

void Course::EnterCourse() const
{
    cout<<"=======Enter Course Information========\n"
        <<"Enter Course ID: \n";
    cin>>courseID;
    cout<<"Enter Course Name: \n";
    cin>>courseName;
    cout<<"Enter credits: \n";
    cin>>credits;
}



string Course::GetCourseID() const
{
    return courseID;
}

string Course::GetCourseName() const
{
    return courseName;
}

int Course::GetCredits() const
{
    return credits;
}

void Course::SetNext(Course * cou)
{
    nextCourse=cou;
}



#endif

1 Answers1

2

Using std::cin is considered writing to member data. But you can not change the data members using a const qualified member function. Remove the const specifier from the function declaration and definition if you want to achieve that functionality:

void Course::EnterCourse();

More info on the subject in these SO posts:
Meaning of “const” last in a C++ method declaration?
What is meant with “const” at end of function declaration?

Ron
  • 14,674
  • 4
  • 34
  • 47