error: passing 'const A' as 'this' argument of 'void A::hi()' discards qualifiers [-fpermissive]błąd: przekazanie "const ..." jako "tego" argumentu kwalifikatorów "..." odrzucających
Nie rozumiem, dlaczego otrzymuję ten błąd, nie zwracam niczego po prostu przez podanie odniesienia do obiektu i to jest to.
#include <iostream>
class A
{
public:
void hi()
{
std::cout << "hi." << std::endl;
}
};
class B
{
public:
void receive(const A& a) {
a.hi();
}
};
class C
{
public:
void receive(const A& a) {
B b;
b.receive(a);
}
};
int main(int argc, char ** argv)
{
A a;
C c;
c.receive(a);
return 0;
}
@edit
Naprawiłem go za pomocą const poprawności ale teraz staram się wywołać metody wewnątrz tej samej metody i uzyskać ten sam błąd, ale dziwne jest to, że ja” nie przekazuję odniesienia do tej metody.
#include <iostream>
class A
{
public:
void sayhi() const
{
hello();
world();
}
void hello()
{
std::cout << "world" << std::endl;
}
void world()
{
std::cout << "world" << std::endl;
}
};
class B
{
public:
void receive(const A& a) {
a.sayhi();
}
};
class C
{
public:
void receive(const A& a) {
B b;
b.receive(a);
}
};
int main(int argc, char ** argv)
{
A a;
C c;
c.receive(a);
return 0;
}
error: passing 'const A' as 'this' argument of 'void A::hello()' discards qualifiers [-fpermissive]
error: passing 'const A' as 'this' argument of 'void A::world()' discards qualifiers [-fpermissive]
'hi' jest funkcją członkowską non-'const'; "a" to "const". –
przekazywanie oznacza jako (niejawny) argument. Nie ma nic wspólnego z powrotem. – Deduplicator
Możliwy duplikat [błąd: podanie xxx jako "ten" argument kwalifikatorów xxx discards] (http://stackoverflow.com/questions/5973427/error-passing-xxx-as-this-argument-of-xxx-discards- kwalifikatory) –