cannot convert from (__thiscall ) (void) const

I have the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

//Pointer to class member

class X {
public:

	int  visible;
	void print() const
	{ cout << "\nhide = " << hide << " visible = " << visible; }
	void reset() { visible = hide; }
	void set(int i) { hide = i; }

private:
	int  hide;
};

typedef void (X::*pfcn)();

int main()
{
	X  a, b, *pb = &b;
	int X::*pXint = &X::visible;
	pfcn pF = &X::print;

	a.set(8); a.reset();
	b.set(4); b.reset();
	a.print();
	a.*pXint += 1;
	a.print();
	cout << "\nb.visible = " << pb ->*pXint;
	(b.*pF)();
	pF = &X::reset;
	(a.*pF)();
	a.print();
	cout << endl;

	getchar();
}

From http://www.cse.ucsc.edu/~pohl/C++BD/04Chap/show_hide.cpp

and if I compile this code in Visual Studio 2010 i have the err:
1
2
1>e:\vs_cpp\show_hide\show_hide\main.cpp(31): error C2440: 'initializing' : cannot convert from 'void (__thiscall X::* )(void) const' to 'pfcn'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


Can somebody help me?
Tnx!
closed account (yUq2Nwbp)
it could be from declaration typedef void (X::*pfcn)()....maybe you must add const after delcaration...
Can you give me the code..?
I realy don't need a 'could be'..I need the solution!

Please?? anybody else?????

C'MON! !!!!
Last edited on
C'mon people !!
closed account (zb0S216C)
You're missing out some of the code. Here, pfcn isn't defined. Include this into your original post. Forget what I said in this post.

Wazzak
Last edited on
reset and print have different signatures. You can't use one definition to point to both functions without overriding the compiler's type system. For the purposes of your sample, you can give them the same signature:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

using namespace std;

class X
{
public:
	int  visible;
	void print() { cout << "\nhide = " << hide << " visible = " << visible; }
	void reset() { visible = hide; }
	void set(int i) { hide = i; }

private:
	int  hide;
};

typedef void (X::*pfcn)();

int main()
{
	X  a, b, *pb = &b;
	int X::*pXint = &X::visible;
	pfcn pF = &X::print;

	a.set(8); a.reset();
	b.set(4); b.reset();
	a.print();
	a.*pXint += 1;
	a.print();
	cout << "\nb.visible = " << pb ->*pXint;
	(b.*pF)();
	pF = &X::reset;
	(a.*pF)();
	a.print();
	cout << endl;

	return 0;
}
@kbw
Tnx!
I don't understand what exactly did you do 'cuz i'm a begginner :).
But.. There is no errors any more :)
Tnx .. :)
Topic archived. No new replies allowed.