'Call to member function x is ambiguous' when calling an overloaded member from a .h file

I get the following error in XCode whenever I try to access the member I created 'randomGen' in a separate class in a different header file.

I have made sure to include the header file and have tried to access it through an object.

This is the code I enter when trying to access the method from randomiser.h in main.cpp. It is also an overloaded function with doubles and integers:

1
2
RandomG randomiser;
randomiser.randomGen(); // 'Call to member function 'randomGen' is ambiguous' 


This is the code inside randomiser.h:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <string>
#include <iostream>

using std::string;
using std::cout;
using std::endl;

class RandomG
{
    
private:
    // This is private
public:
    // Public
    
    RandomG();
    ~RandomG();
    
    // Methods
    int randomGen(int generatedNumber = NULL, int range = 2);
    
    int randomGen(double generatedNumber = NULL, int range = 1);
};

class passwordManagement
{
public:
    // Public
    
    passwordManagement();
    ~passwordManagement();
    
    // Methods
    int passReset();
    
    
private:
    // Private
    
};

passwordManagement::passwordManagement()
{
    std::cout << "Constructing password management class" << std::endl;
}

passwordManagement::~passwordManagement()
{
    std::cout << "Destructing password management class" << std::endl;
}

RandomG::RandomG()
{
    std::cout << "Constructing random class" << std::endl;
    srand((unsigned int)time(NULL));
}

RandomG::~RandomG()
{
    std::cout << "Destructing random class" << std::endl;
}

int RandomG::randomGen(int generatedNumber, int range)
{
    // Generates a random number from range and returns it
    generatedNumber = 1+(rand()%range);
    return generatedNumber;
};
int RandomG::randomGen(double generatedNumber, int range)
{
    // Generates a random decimal number from range and returns it
    generatedNumber = 1+(rand()%range);
    return generatedNumber;
};

int passwordManagement::passReset()
{
    return 0;
};


This is the error inside xcode:
http://puu.sh/77i1f.png

I have tried seperating the code for the functions in another class (main.cpp) and then running and it seems to works, so I'm not sure why I can't put everything in the .h file and then access it?

I would like it in a seperate file so it doesn't clutter my main. I am writing a game with SDL so that might be confusing and I would like the window to have a random title and other random properties, so it would be easier to use a function.
Its also practice with using them, because I am new to C++. Please make your answer as noob friendly as possible, so I can understand.

Thanks in advance, and sorry if my post needs amending in some way. If so I will.

- exitcode

(I didn't shorten the code inside the header file as I think it is all relavent).
1
2
3
4
    // Methods
    int randomGen(int generatedNumber = NULL, int range = 2);
    
    int randomGen(double generatedNumber = NULL, int range = 1);


The compiler cannot know which one to call when you call it with no arguments; either function would fit.
The compiler cannot know which one to call when you call it with no arguments; either function would fit.


But even with arguments, If I want to access the integer version, it will not know whether I want the double or the int, because it fits both. I might create seperate methods for each at the moment but, although I see what the problem is, I'm not sure how to let the compiler know I want to access the int version for example.

Thanks for clarifying though.

EDIT: There was no need for a double version of the function anyway, my code was appalling - I managed to fix it anyway, but I deleted the double function.

Thanks again!
Last edited on
If you pass an int as the first argument it should pick the int version of the function.
 
randomiser.randomGen(1);


If you pass a double as the first argument it should pick the double version of the function.
 
randomiser.randomGen(1.0);


To avoid the ambiguity when no argument is passed remove the default argument from one of them. For instance, if you want the int function to be called when no argument is passed remove the default argument from the double function.
1
2
3
4
    // Methods
    int randomGen(int generatedNumber = NULL, int range = 2);
    
    int randomGen(double generatedNumber, int range = 1);


Now, what is the purpose of having these two functions in the first place? They do exactly the same thing only the return type is different.
Topic archived. No new replies allowed.