void value not ignored as it ought to be

My programming is not running how to remove error

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
#include<iostream>
using namespace std;

class Data
{
    public:
    int a;
    void print() 
    { 
        cout << "a is "<< a; 
    }
};

int main()
{
    Data d, *ptr;
    
	Data *dp = &d;     // assign address of object to pointer to class type 

    int Data = dp->print();
    d.*ptr=10;
    d.print();

    dp->*ptr=20;
    dp->print();
}
 


error: void value not ignored as it ought to be
int Data = dp->print();
^
error: 'ptr' cannot be used as a member pointer, since it is of type 'Data*'
d.*ptr=10;
Remove the int Data =
I tried I get another error

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
#include<iostream>
using namespace std;

class Data
{
    public:
    int a;
    void print() 
    { 
        cout << "a is "<< a; 
    }
};

int main()
{
    Data d, *ptr;
    
	Data *dp = &d;     // assign address of object to pointer to class type 

    dp->print();
    d.*ptr=10;
    d.print();

    dp->*ptr=20;
    dp->print();
}


error: 'ptr' cannot be used as a member pointer, since it is of type 'Data*'
d.*ptr=10;
^~~
error: 'ptr' cannot be used as a member pointer, since it is of type 'Data*'
dp->*ptr=20;
^~~
d.*ptr=10; makes no sense. What are you trying to do?

Topic archived. No new replies allowed.