"this discards qualifiers", what does it mean?
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
class b
{
public:
void show( b &ob)
{
//this =ob;
cout<<"show";
}
};
int main()
{
b const ob;
b ob1;
ob.show(ob1);
// your code goes here
return 0;
}
prog.cpp: In function ‘int main()’:
prog.cpp:23:14: error: passing ‘const b’ as ‘this’ argument discards qualifiers [-fpermissive]
ob.show(ob1);
^
Here you've declared ob to be a const object:
b const ob;
But here you are calling a member function (show) that is not marked as const:
ob.show(ob1);
Mark member functions that does not alter the object (*this) as const:
void show( b &ob) const // <- like this
{
//this =ob;
cout<<"show";
}
In this particular case, I also recommend that you change show so it does not take any arguments and only show the contents of *this:
#include <iostream>
class b
{
private:
int example_of_a_member_variable;
public:
// example of a converting constructor
explicit b(int value) : example_of_a_member_variable(value) {}
void show() const
{
std::cout << example_of_a_member_variable << '\n';
}
};
int main() {
b const ob(1234);
ob.show(); // prints 1234
}
Related
Could anyone help me to fix this bug " error: invalid use of non-static member function" in line test(3, a.foo); when i compile the codes
#include <iostream>
#include <string>
class A {
public:
int x = 0;
void foo(int y){
x = x + y;
}
};
template<typename Func>
void test(int d, Func f){
f(d);
}
int main()
{
A a;
a.foo(4);
std::cout << a.x;
test(3, a.foo);
}
Code1:
#include <iostream>
using namespace std;
class Test1 {
public:
Test1() { num_ = 10; }
int GetNum() { return num_; }
private:
int num_;
};
class Test2 {
public:
int GetNum() const { return t1_.GetNum(); }
private:
static Test1 t1_;
};
Test1 Test2::t1_;
int main(int argc, char *argv[]) {
Test2 t2;
cout << t2.GetNum() << endl;
}
Code2:
#include <iostream>
using namespace std;
class Test1 {
public:
Test1() { num_ = 10; }
int GetNum() { return num_; }
private:
int num_;
};
class Test2 {
public:
int GetNum() const { return t1_.GetNum(); }
private:
Test1 t1_;
};
int main(int argc, char *argv[]) {
Test2 t2;
cout << t2.GetNum() << endl;
}
The difference between two code is that code1's t1_ is static.
And Code1 works. But Code2 raise a error:
error: passing ‘const Test1’ as ‘this’ argument of ‘int Test1::GetNum()’ discards qualifiers [-fpermissive]
int GetNum() const { return t1_.GetNum(); }
Why Code1 can work?
As non-static data member, t1_ becomes const accordingly in const member function. Then t1_.GetNum() leads to the error because non-const member function can't be called on const object.
On the other hand, static data member doesn't belong to object; it won't become const just because in const member function.
The code was as below
#include <set>
#include <iostream>
#include <string>
#include <memory>
using namespace std;
class Quote {
public:
int getnum() {
return num;
}
private:
int num;
};
class basket {
public:
void add_item(const shared_ptr<Quote> &b) {
setQuo.insert(b);
}
private:
static bool compare(shared_ptr<Quote> &l, shared_ptr<Quote> &r) {
return l->getnum() < r->getnum();
}
multiset<shared_ptr<Quote>, decltype(compare)*> setQuo{compare};
};
int main()
{
cout << "start" << endl;
}
I found thatsetQuo.insert(b);will lead complie error. Complie error was as below
*/usr/include/c++/7/bits/stl_tree.h:2069:51: error: binding reference of type ‘std::shared_ptr<Quote>&’ to ‘const key_type {aka const std::shared_ptr<Quote>}’ discards qualifiers
__x = _M_impl._M_key_compare(__k, _S_key(__x)) ?*
*/usr/include/c++/7/bits/stl_tree.h:1750:10: error: binding reference of type ‘std::shared_ptr<Quote>&’ to ‘const std::shared_ptr<Quote>’ discards qualifiers*
The code was looks right for me, this question is really confused me.
Your compare function is not supposed to change the elements it's comparing so take the arguments by const&:
static bool compare(const shared_ptr<Quote> &l, const shared_ptr<Quote> &r) {
return l->getnum() < r->getnum();
}
Demo
I want to store a callback to a std::function<int(int,int>> object. When I use lambda, it works fine. But when I use std::bind to a member function, it compiled error.
There is the sample error code.
#include <functional>
#include <iostream>
using namespace std;
class A
{
public:
void foo()
{
std::function<int(int,int)> callback = std::bind(&A::calc, this);
// std::function<int(int,int)> callback = [this](int a, int b) { return this->calc(a, b); }; // lambda works fine
int r = callback(3, 4);
cout << r << endl;
}
int calc(int b, int c) { return b + c; }
};
int main()
{
A a;
a.foo();
return 0;
}
error message:
In member function 'void A::foo()':
12:72: error: conversion from 'std::_Bind_helper<false, int (A::*)(int, int), A*>::type {aka std::_Bind<std::_Mem_fn<int (A::*)(int, int)>(A*)>}' to non-scalar type 'std::function<int(int, int)>' requested
code link: http://cpp.sh/9pm3c
You need to indicate that A::calc takes 2 parameters, use std::placeholders::X to do it:
std::function<int(int,int)> callback = std::bind(&A::calc, this, std::placeholders::_1,std::placeholders::_2);
I try to implement Scott Mayer book code example, the example is about calling functor through function object
the header file gameCharachter.h
#ifndef GAMECHARACTER_H
#define GAMECHARACTER_H
#include <iostream>
#include <typeinfo>
using namespace std;
#include <tr1/functional>
class GameCharacter;
int defaultHealthCalc(const GameCharacter& gc);
class GameCharacter
{
public:
typedef std::tr1::function<int (const GameCharacter&)> HealthCalcFunc;
explicit GameCharacter(HealthCalcFunc hcf = defaultHealthCalc)
: healthFunc(hcf)
{
}
~GameCharacter()
{
}
int healthValue() const
{
return healthFunc(*this);
}
private:
HealthCalcFunc healthFunc;
};
class EyeCandyCharacter: public GameCharacter // another character
{
public:
explicit EyeCandyCharacter(HealthCalcFunc hcf = defaultHealthCalc)
: GameCharacter(hcf)
{
cout<<typeid(*this).name()<<"::"<<__FUNCTION__<<""<<endl;
}
};
struct HealthCalculator
{
/*explicit*/ HealthCalculator()
{
}
int operator()(const GameCharacter& gc) const // calculation function
{
cout<<typeid(*this).name()<<"::"<<__FUNCTION__<<""<<endl;
return 0;
}
};
#endif // GAMECHARACTER_H
the main.cpp is :
#include "gamecharacter.h"
int main()
{
EyeCandyCharacter ecc1(HealthCalculator());
ecc1.healthValue();
}
why function<> object refuse to call the operator() function in healthvalue()
EyeCandyCharacter ecc1(HealthCalculator());
declares a function called ecc1 that takes an argument of type "pointer to function taking no arguments and returning a HealthCalculator" and returns a EyeCandyCharacter. I assume that this isn't your intent.
this is the correct call , it should be called by bind
#include "gamecharacter.h"
int main()
{
HealthCalculator hc;
EyeCandyCharacter ecc1(std::tr1::bind(&HealthCalculator::operator(),hc,tr1::placeholders::_1));
ecc1.healthValue();
}