Today, I wrote a small piece of code. I thought it was correct, but after running it, it was somehow “discard qualifier”“
Here is the original program:
#include< iostream>
using namespace std;
class Date
{
int year;
public:
Date(int y):year(y){}
int get_ year()
{
return year;
}
int plus(const Date& p)
{
int total = p.get_ year()+year;
return total;
}
};
int main()
{
Date q(1000);
Date p(2000);
cout<& lt; p.plus(q);
system(“pause”);
}
Passing ` const date ‘as ` this’ argument of ` int date:: get_ What do you mean by year ()’discards qualifiers?Original const date & amp; p. The compiler decides to call const member function, that is, it can’t modify the member value of P call_ Year, just read, no modify?In principle, there is no modify, but the C + + compiler always assumes that you can modify the value. In fact, it can modify the value, so it is inconsistent with const member function. How can I change the two methods. First, remove const. Second, in get_ Add const after year
#include< iostream>
using namespace std;
class Date
{
int year;
public:
Date(int y):year(y){}
int get_ year()
{
return year;
}
int plus(const Date& p)const
{
int total = p.get_ year()+year;
return total;
}
};
int main()
{
Date q(1000);
Date p(2000);
cout<& lt; p.plus(q);
system(“pause”);
}
That’s right.
The above is just a little thinking for beginners, for your reference~~~
Similar Posts:
- [Solved] codeblocks Error: ‘to_string’ was not declared in this scope
- C++ error: cannot bind non-const lvalue reference of type ‘myString&’ to an rvalue of type ‘m…
- dynamic_cast Error: source type is not polymorphic
- C++ 11: How to Avoid Deadlock in unique_Lock and lock_Guard
- The solution of “no matching function for call to…” in G + + compilation
- [Solved] Operator overload must take either zero or one argument error
- PHP Fatal error: Cannot redeclare class
- C++ write and read file via fstream in ios::out,ios::in,ios::app mode
- Oracle ORA-00936: missing expression [How to Solve]
- QT use of undeclared identifier ‘cout’