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
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?
Related
I have been looking in different threads with this error which is quite common but it feels like the IDE I am using messed with my workspace and I can't quite find the problem. I am setting up an extremely basic class called "Movie" that is specified below:
Movie.hpp :
#ifndef MOVIE_HPP
#define MOVIE_HPP
#include <iostream>
#include <string>
using std::string, std::cout,std::size_t;
class Movie
{
private:
std::string name;
std::string rating;
int watched_ctr;
public:
Movie(const string& name, const string& rating, int watched_ctr);
~Movie();
//getters
string get_name() const;
string get_rating() const;
int get_watched() const;
//setters
void set_name(string name);
void set_rating(string rating);
void set_watched(int watched_ctr);
};
#endif // MOVIE_HPP
Movie.cpp:
#include <iostream>
#include <string>
#include "Movie.hpp"
using std::string, std::cout,std::size_t,std::endl;
Movie::Movie(const string& name, const string& rating, int watched_ctr)
: name(name) , rating(rating) , watched_ctr(watched_ctr) {
}
Movie::~Movie()
{
cout << "Destructor for Movies class called /n";
}
//Getters
string Movie::get_name(){return name;}
string Movie::get_rating(){return rating;}
string Movie::get_watched(){return watched_ctr;}
//Setters
void Movie::set_name(std::string n){this -> name = n;}
void Movie::set_rating(std::string rating){this -> rating = rating;}
void Movie::set_watched(int ctr){this -> watched_ctr = ctr;}
The main.cpp I am trying only consists in creating one Movie object:
#include <iostream>
#include <string>
#include "Movie.hpp"
using std::string, std::cout,std::size_t,std::endl;
int main()
{
Movie StarTrek("Star Trek", "G", 20);
}
As you can see, I set all the attribute to private in order to exercise with the set/get methods but I keep stumbling upon the same error on each of them stating >"C:/Users/.../ProjectsAndTests/MoviesClass/Movie.cpp:18:8: error: no declaration matches 'std::__cxx11::string Movie::get_name()"
if you could give me a hint on what might cause this error I would greatly appreciate thank you!
I tried opening another workspace with classes implemented inside of them and the syntax I am using is very close from this test workspace I opened which compiled fine (no error regarding declaration match).
There are 2 problems with your code.
First while defining the member functions outside class you're not using the const. So to solve this problem we must use const when defining the member function outside the class.
Second, the member function Movie::get_watched() is declared with the return type of string but while defining that member function you're using the return type int. To solve this, change the return type while defining the member function to match the return type in the declaration.
//----------------------vvvvv--------->added const
string Movie::get_name()const
{
return name;
}
string Movie::get_rating()const
{
return rating;
}
vvv------------------------------>changed return type to int
int Movie::get_watched()const
{
return watched_ctr;
}
Working demo
I have a problem that for you might look simple but I can't figure it out even though I really tried.
#include <iostream>
#include <string>
#include <vector>
//Functions Prototypes ---
void create_object(std::vector <Movie> &);
std::string set_name();
int set_ryear();
double set_rating();
//Classes ---
class Movie {
public:
std::string name;
int release_year;
double rating;
public:
//Constructors
Movie(std::string u_name, int u_release_year, double u_rating);
~Movie() {};
Movie(const Movie &);
};
//MAIN ---
int main() {
std::vector <Movie> Collection;
create_object(Collection);
return 0;
}
//Constructors ---
Movie::Movie(std::string u_name, int u_release_year, double u_rating)
:name{ u_name }, release_year{ u_release_year }, rating{ u_rating }{
}
//Stand-Alone Functions ---
char get_selection() {
char x;
std::cin >> x;
return x;
}
void create_object(std::vector <Movie> &x) {
std::string u_name = set_name();
int u_ryear = set_ryear();
double u_rating = set_rating();
Movie User_Movie{ u_name,u_ryear,u_rating };
}
std::string set_name() {
std::string x;
std::cout << "Name: ";
std::cin >> x;
return x;
}
int set_ryear() {
int x;
std::cout << "Release year: ";
std::cin >> x;
return x;
}
double set_rating() {
double x;
std::cout << "Rating: ";
std::cin >> x;
return x;
}
I'm just a beginner, so I don't know much, but for as much as I know classes should be types even if they are user-defined types, and they are supposed to give the possibility to be manipulated as primitive types.
So why, when I try to create a vector that expects a bunch of Movie types, or when I pass it by reference, I receive errors?
Error C2664 'void create_object(std::vector &)': cannot convert argument 1 from 'std::vector<Movie,std::allocator<Movie>>' to 'std::vector &'
Error C2923 'std::vector': 'Movie' is not a valid template type argument for parameter '_Ty'
Error C2065 'Movie': undeclared identifier
The main usage for it should be to be visible to all functions so that I can pass it to them as I want, and to make operations on its objects (like after creating a new object, I want to be able to add it into the collection using push_back() and all that stuff) after I create a menu system using switch.
You have not declared the Movie class by the time you reach this statement:
void create_object(std::vector <Movie> &);
The correct way to do C++ classes is to put the basic declaration in a separate file movie.h and then #include it in your code at the start.
If you cannot do that (although you really should), move the class declaration to the start of the file.
Newbie to C++ over here. I am trying to create 2 classes and interlink them using Composition, but I keep getting errors.
#include <iostream>
#include <string>
using namespace std;
class student
{
public: int roll_no;
string name;
string dob;
void student_display()
{
cout<<roll_no<< " "<<name<<" "<<dob<<endl;
}
student(int roll,string names,string dateofb)
{
roll_no=roll;
name=names;
dob=dateofb;
}
~student(){};
};
class course
{
public:string course_name;
int duration;
void course_display()
{
cout<<course_name<< " "<<duration<<" "<<endl;
s1.student_display()<<endl;
}
course (string c_name,int dur,student s2):course_name(c_name),duration(dur),s1(s2){}
~course(){};
private : student s1;
};
class college
{
public: string college_name;
string location;
course c1;
course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
~course(){};
};
int main() {
student s5(001,"Noel","28/04/1994");
s5.student_display();
course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
c1.course_display();
return 0;
}
The errors are as follows:
prog.cpp: In member function 'void course::course_display()':
prog.cpp:32:35: error: invalid operands of types 'void' and '<unresolved overloaded function type>' to binary 'operator<<'
s1.student_display()<<endl;
^
prog.cpp: At global scope:
prog.cpp:45:20: error: expected ')' before 'col_name'
course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
^
prog.cpp:47:13: error: declaration of '~course' as member of 'college'
~course(){};
^
prog.cpp: In function 'int main()':
prog.cpp:52:54: error: no match for call to '(student) (int, const char [5], const char [11])'
course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
Could someone please help me out? I have tried going through forums with the same error but cant figure it out
^
I have modified the code as follows:
#include <iostream>
#include <string>
using namespace std;
class student
{
public: int roll_no;
string name;
string dob;
void student_display()
{
cout<<roll_no<< " "<<name<<" "<<dob<<endl;
}
student(int roll,string names,string dateofb)
{
roll_no=roll;
name=names;
dob=dateofb;
}
~student(){};
};
class course
{
public:
string course_name;
int duration;
private:
student s1;
public:
void course_display()
{
cout<<course_name<< " "<<duration<<" "<<endl;
s1.student_display();
}
course (string c_name,int dur,student s2):course_name(c_name),duration(dur),s1(s2){}
~course(){};
//private: // shifted before course_display()
// student s1;
};
// Not used
//class college
//{
//public:
// string college_name;
// string location;
// course c1;
//
// course (string col_name,string loc,course c2):college_name(col_name),location(loc),c1(c2){}
//
// //~course(){}; // destructor mismatch
//};
int main() {
student s5(001,"Noel","28/04/1994");
s5.student_display();
//course c1("Engineering",4,s5(001,"Noel","28/04/1994"));
course c1("Engineering",4,s5);
c1.course_display();
return 0;
}
and the output is follows:
Line 30: Remove the semicolon.
Line 45: Looks like copy/paste error, should be the constructor of the college class, right? Then change the name from course to college. Same for the destructor on the following line.
Line 52: You create the object s5 2 lines above, I guess you want to pass this object to the constructor of the course object. If so then just remove the expression in the brackets after s5.
I am getting an error extra qualification ‘student::’ on member ‘student’ [-fpermissive].
And also why name::name such syntax is used in constructor?
#include<iostream>
#include<string.h>
using namespace std;
class student
{
private:
int id;
char name[30];
public:
/* void read()
{
cout<<"enter id"<<endl;
cin>>id;
cout<<"enter name"<<endl;
cin>>name;
}*/
void show()
{
cout<<id<<name<<endl;
}
student::student()
{
id=0;
strcpy(name,"undefine");
}
};
main()
{
student s1;
// s1.read();
cout<<"showing data of s1"<<endl;
s1.show();
// s2.read();
//cout<<"showing data of s2"<<endl;
//s2.show();
}
In-class definitions of member function(s)/constructor(s)/destructor don't require qualification such as student::.
So this code,
student::student()
{
id=0;
strcpy(name,"undefine");
}
should be this:
student()
{
id=0;
strcpy(name,"undefine");
}
The qualification student:: is required only if you define the member functions outside the class, usually in .cpp file.
It would be correct if definition of constructor would appear outside of class definition.
Similar code:
Change from:
class Solution {
public:
static int Solution::curr_h; // ----------------- ISSUE: "Solution::" is extra
static int Solution::curr_m; // ----------------- ISSUE: "Solution::" is extra
};
int Solution::curr_h = 0;
int Solution::curr_m = 0;
to:
class Solution {
public:
static int curr_h; // ----------------- FIX: remove "Solution::"
static int curr_m; // ----------------- FIX: remove "Solution::"
};
int Solution::curr_h = 0; // <------ good here - "Solution::" is required
int Solution::curr_m = 0; // <------ good here - "Solution::" is required
This happens because copy pasting statics require initialization outside of class, but also requires all the declaration (int) too.
Though it is correct, wish there is a easier/ better way to do the same.
I'm trying to create a member function that allows an user to set member array variables.
I've been looking everywhere but I can't find the problem in my code,
#include <string>
#include <iostream>
using namespace std;
class Employee
{
protected:
string name;
char ssn[11];
char id[5];
char hired[8];
public:
Employee(char ssn, char id, char hired); //Constructor
Employee(string name);
~Employee(); //Destructor
void setName(string n) { n = name; }
void setSSN(char i) { ssn = i; }
};
int main()
{
return 0;
}
Let's have a look at your setSSN function:
void setSSN(char i) { ssn = i; }
SNN, which most likely means social security number, doesn't consist of just one digit but 11, right? Then why would setSSN take as input only one character (digit) by (char i)? So setSSN function should rather take a string of characters containing SSN of the employee and that string should be of the same flavor as the ssn member variable of your Employee class in order to let you assign one string variable by another in the body of setSSN function. If you are already familiar with the string class of the C++ standard library, you should probably use that class for all your string storage and manipulation.