I can't overload these 2 functions why?

My book is asking me to add two functions to my existing class that return the respective values and its asking me to make them have the same return values and signature, I'm not sure I understand the question can someone explain it to me and why does the compiler not let me overload the getthedatamembertwo function?

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
42
43
44
45
46
47
#include<iostream>

class myClass
{
public:
myClass(int data):theDataMember(data){howManyInstances++;}
~myClass(){howManyInstances--;}
static int getHowManyInstances(){return howManyInstances;}
int setTheDataMemberTwo(int memberTwo){theDataMemberTwo=memberTwo;}
float setTheDataMemberTwo(float memberTwo){theDataMemberTwo=memberTwo;}
int getTheDataMember(){return theDataMember;}
int getTheDataMemberTwo(){return theDataMemberTwo;}
float getTheDataMemberTwo(){return theDataMemberTwo;}
private:
int theDataMember;
int theDataMemberTwo;
float theDataMemberTwo;
static int howManyInstances;
};

int myClass::howManyInstances = 0;

int main()
{


myClass ObjectOne(5);
myClass ObjectTwo(10);
myClass ObjectThree(20);

int (myClass::*fPtr)()=&myClass::getTheDataMember;

std::cout<<"There are : " <<myClass::getHowManyInstances()<< " objects."<<std::endl;


std::cout<<"Object one data member = " <<(ObjectOne.*fPtr)()<<std::endl;




float (myClass::*fPtrTwo)()=&myClass::getTheDataMemberTwo;

ObjectOne.setTheDataMemberTwo(5.5f);

std::cout<<"Object one's data member 2 = " <<(ObjectOne.*fPtrTwo)()<<std::endl;

}
Last edited on
You are not permitted to create functions that differ ONLY in return type. How could the compiler know which one you meant to call if that's the only difference?

1
2
int getTheDataMemberTwo(){return theDataMemberTwo;}
float getTheDataMemberTwo(){return theDataMemberTwo;}


When the compiler sees you try to call one of these:
ObjectOne.getTheDataMemberTwo(); how could it know which function you meant?


1
2
int theDataMemberTwo;
float theDataMemberTwo;

And this. Variables with the same name. How is the compiler meant to know which one you mean when you try to use one?
Last edited on
Topic archived. No new replies allowed.