How to implement this to not get compiler error C2664 - Const pointer to reference?  
Author Message
redcarbon





PostPosted: Mon Aug 28 10:27:46 CDT 2006 Top

Visual Studio C++ >> How to implement this to not get compiler error C2664 - Const pointer to reference?

#include <string>
#include <iostream>

class A {
public:
virtual void afunc(std::string& s) =3D 0;
};

void somefunc(A& aref) {
std::string message("Example");
aref.afunc(message);
}

class B {
public:
virtual void afunc(std::string& s) { std::cout << s << std::endl; };
void bfunc() { somefunc(this); };
};


int main(int argc, char** argv) {
B b;
b.bfunc();
}

1>c:\projects\foo\foo.cpp(17) : error C2664: 'somefunc' : cannot convert=
=

parameter 1 from 'B *const ' to 'A &'

I can modify class B, but class A and somefunc are not changable in this=
=

particular instance.

TIA,
Eric

Visual Studio140  
 
 
Eric





PostPosted: Mon Aug 28 10:27:46 CDT 2006 Top

Visual Studio C++ >> How to implement this to not get compiler error C2664 - Const pointer to reference? Small typo:

class B {

...should be...

class B : public A {

My original question still stands, though.

TIA,
Eric
 
 
Stefan





PostPosted: Mon Aug 28 10:47:07 CDT 2006 Top

Visual Studio C++ >> How to implement this to not get compiler error C2664 - Const pointer to reference? Eric Hill schrieb:
> #include <string>
> #include <iostream>
>
> class A {
> public:
> virtual void afunc(std::string& s) = 0;
> };
>
> void somefunc(A& aref) {
> std::string message("Example");
> aref.afunc(message);
> }
>
> class B {

Youe meant:

class B : public A {

> public:
> virtual void afunc(std::string& s) { std::cout << s << std::endl; };
> void bfunc() { somefunc(this); };

Should be:

void bfunc() { somefunc(*this); }

'this' is a pointer and somefunc() takes a reference.

> };
>
>
> int main(int argc, char** argv) {
> B b;
> b.bfunc();
> }

/S
--
Stefan Naewe
stefan_DOT_naewe_AT_atlas_DOT_de
 
 
David





PostPosted: Mon Aug 28 10:53:46 CDT 2006 Top

Visual Studio C++ >> How to implement this to not get compiler error C2664 - Const pointer to reference?

> Small typo:
>
> class B {
>
> ...should be...
>
> class B : public A {
>
> My original question still stands, though.
>
> TIA,
> Eric

Eric:

"this" is a pointer, not a reference.

somefunc(*this);

David Wilkinson
 
 
Eric





PostPosted: Mon Aug 28 11:04:47 CDT 2006 Top

Visual Studio C++ >> How to implement this to not get compiler error C2664 - Const pointer to reference? > Should be:
>
> void bfunc() { somefunc(*this); }
>
> 'this' is a pointer and somefunc() takes a reference.
>

Ah, that's it. I knew it had to be a simple thing. Thanks.

Eric