Overloading function needed?

1
2
3
4
5
6
7
8
template <class T>
T Geometric(T val1, T val2, T val3)
{
	T v = (1/3);
	T temp;
	temp = (val1 * val2 * val3);
	return (pow(temp,v));
}


pow ambiguous call to overloaded function. Well, in short how to fix it?
Have you overloaded pow() to accept T as a value? As far as I know the default parameters of pow() you can find here:
http://www.cplusplus.com/reference/clibrary/cmath/pow/

To sum it up they are:

1
2
3
4
5
double pow (      double base,      double exponent );
long double pow ( long double base, long double exponent );
float pow (       float base,       float exponent );
double pow (      double base,         int exponent );
long double pow ( long double base,         int exponent );
how to overload it? I know how to overload operators like = * / - for vector addition and stuff but i never learn how to overload a function pow. Or do i HAVE to overload it to make this function work? is there any power function that i can use other than pow?
Last edited on
you could always write your own power function right?

something like:
1
2
3
4
mYpow(T num)
{
    return num*num;
}
Just to do what Zap said and to create your own version of the function. e.g.

1
2
3
4
pow(T value, int exp)
{

}


it will overload the original as long as the parameters are different.
Last edited on
I can't, i still run into the same error. It saids as long as i am using template the error say it might be int, float and stuff.
1
2
3
4
5
template <class T>
T myPow(T val1,int val2)
{
	return(pow(val1, val2));
}


1
2
3
4
5
6
7
8
9
10
template <class T>
T Geometric(T val1, T val2, T val3)
{
	T v = (1/3);
	T temp;
	temp = (val1 * val2 * val3);
	return (myPow(temp, 3));
}

}

You don't understand. You can't throw in pow(T, int) without overloading that function with those parameters. This is what i mean:

1
2
3
4
5
6
7
8
template <class T>
T pow(T val1,int val2)
{
        T result;
	result.value = val1.value * val2;

        return result;
}


Now "val1.value" is something that I made up because I don't have your template definition.
Last edited on
What do u mean template definition?

If ur asking for the formula i am going to use is this

(val1 * val2, * val3) ^(1/3) <-- that's what i want to return. the ^ is the power in math not the bitwise operator.
Ok, I just have to ask: Given that pow() has 5 overloads shown by the good GodPyro, what in the name of the lord are you passing as type T to your Geometric() function? I mean, it has to be a double, float, int, long double, or maybe long long int (which would be the only one not having an overload).

Also, you have T v = (1/3);. If you know the value of 'v' and you know it is fixed, why declare it of being of the T data type? Why not double v = (1/3);? I am guessing this alone may very well fix your issue.
oh, i see. Erm. Alright i will post my complete code and u'll understand.
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include<cmath>
using namespace std;

template <class T>
T funnyfunc(T val1, T val2, T val3);
template <class T>
T Harmonic(T val1, T val2, T val3);
template <class T>
T Arithmetic(T val1, T val2, T val3);
template <class T>
T Geometric(T val1, T val2, T val3);
void testvalue(int value, short unsigned int code);
void drawmenu(void);
void drawmenu1(void);
template <typename T>
void runprogram();

int main(void)
{

	short unsigned int choice = 0; 
	while(choice!=4)
	{
		cout<<"please select from the following menu ";
		drawmenu();
		cin>>choice;
		try
		{
			testvalue(choice, 3);
			break;
		}

		catch (char* eString)
		{
			cout<< eString <<endl;
			cout<<" please re enter your menu choice ";
		}
	}

	if (choice == 4) 
	{
		cout <<"the program is over";
	}
	else if (choice == 1)
	{
		runprogram<int>();
	}
		
	else if (choice == 2)
	{
		runprogram<float>();
	}
		
	else if (choice == 3)
	{
		runprogram<double>();
	}
	

	




}


template <class T>
T funnyfunc(T val1, T val2, T val3)
{
	return ((val1+val2)*val3);
}

template <class T>
T Harmonic(T val1, T val2, T val3)
{
	return (3/((1/val1)+(1/val2)+(1/val3)));
}

template <class T>
T Arithmetic(T val1, T val2, T val3)
{
	return ((val1 + val2 + val3)/3);
}

template <class T>
T Geometric(T val1, T val2, T val3)
{
	T v = (1/3);
	T temp;
	T n = 3;
	temp = (val1 * val2 * val3);
	return (myPow(temp, (1/n)));
}


void testvalue(int value, short unsigned int code)

{
	switch(code)
	{
	case 1:
		if(value < 0 )
			throw "error - value cannot be negative \n";
			break;
	case 2:
		if(value==0)
			throw "error you can't divided by zero \n";
		break;
	case 3:
		if (value < 1|| value > 4)
			throw "error - invalid menu selection";
		break;
	default:
		cout<<" unhandled exception";
		cout<<"program will stop";
		exit(1);
	}
}

void drawmenu(void)
{
	cout<<endl<<"\t1 - interger"<<endl;
	cout<<"\t2 - float" <<endl;
	cout<<"\t3 - double"<<endl;
	cout<<"\t4 - end this program"<<endl;

}

void drawmenu1(void)
{
	cout<<endl<<"\t1 - Harmonic"<<endl;
	cout<<"\t2 - Arithmetic" <<endl;
	cout<<"\t3 - Geometric"<<endl;
	cout<<"\t4 - end this program"<<endl;

}

template <typename T>
void runprogram()
{
  T a, b, c;
  short unsigned int choice = 0; 

  cout << "please enter first number ";
  cin >> a;
  cout << "please enter second number ";
  cin >> b;
  cout << "please enter third number ";
  cin >> c;

  
  while(choice!=4)
	{
		cout<<"please select what type of means you want ";
		drawmenu1();
		cin>>choice;
		try
		{
			testvalue(choice, 3);
			break;
		}

		catch (char* eString)
		{
			cout<< eString <<endl;
			cout<<" please re enter your menu choice ";
		}
	}

	enum MEAN {HARMONIC = 1, ARITHMETIC,GEOMETRIC};
	MEAN myOps1 = HARMONIC;

		myOps1  = MEAN(choice);
	switch(myOps1)
	{
	case HARMONIC:
		cout<<"the result of Harmonic mean is  " <<Harmonic(a, b, c)<<endl;
		break;
	case ARITHMETIC:
		cout<<"the result of arithmetic mean is " <<Arithmetic(a, b, c)<<endl;
		break;
	case GEOMETRIC:
		cout<<"the result of geometric mean is " <<Geometric(a, b, c)<<endl;
		break;

	default:
		cout<<"thank you for using !"<<endl;
	}

}



So the user got to choose whether it's int/float/double. That's why i would want to make it a template.
THis code ask you "choose int, float, double" then it will ask you "choose harmonic mean, geometric mean, algorithm mean" As i mentioned that's the reason why it's T instead of double/int/float becuase it's what the user choose.
Ok, so the problem is with the int datatype because there is no overload that takes int as base. My recommendation: Make a specialization of your function for int. And change the data type of v to double as I suggested earlier.

Oh, and you need to modify the last line: return T(pow(temp, v));
How to make a specialize function for int? i call the function in the draw up menu

I mean first thing first i need to correct my currrent function first and right now i still go the same error.
1
2
3
4
5
6
7
8
template <class T>
T Geometric(T val1, T val2, T val3)
{
	double v = (1/3);
	T temp;
	temp = (val1 * val2 * val3);
	return T(pow(temp, v));
}
Ok, that function looks better. To create a specialization, refer to: http://www.cplusplus.com/doc/tutorial/templates/. It shows a specialization of a class, but should be the same for a function (I have never done a specialized function myself).

But let me ask before you proceed: What does the compiler tell you exactly? I would think the compiler is telling you exactly which data type is the problematic one, right?
for my previous post the problem is
d:\program files\vs 2010\vc\include\math.h(583): could be 'long double pow(long double,int)'
1> d:\program files\vs 2010\vc\include\math.h(581): or 'long double pow(long double,long double)'
1> d:\program files\vs 2010\vc\include\math.h(535): or 'float pow(float,int)'
1> d:\program files\vs 2010\vc\include\math.h(533): or 'float pow(float,float)'
1> d:\program files\vs 2010\vc\include\math.h(497): or 'double pow(double,int)'
1> d:\program files\vs 2010\vc\include\math.h(122): or 'double pow(double,double)'
1> while trying to match the argument list '(float, double)'
1> d:\spring 2010\cmpsc\cmpsc 122\project7_2\project7_2\project 7_2.cpp(184) : see reference to function template instantiation 'T Geometric<T>(T,T,T)' being compiled
1> with
1> [
1> T=float
1> ]
I see, so it is with float, not int. Or maybe it is with int too, but what you show is complaining about float. I see that there is no float pow(float, double), so I guess it is a righteous complaint. Although I cannot help wonder why implicit casting is not taking place.

Anyway, let's get back to making v of type T. This will be good because when T = double, it should have no issues using the overload "double pow(double, double)", and when T = float, it should have no issues using the overload "float pow(float, float)". That leaves us with int. Tehre is nothing even near to "int pow(int, int)". After making variable v of type T, recompile and see the error message. If it throws the error with T = int, then create the template specialization.
Here's the error

1
2
3
4
5
6
7
8
template <class T>
T Geometric(T val1, T val2, T val3)
{
	int v = (1/3);
	int temp;
	temp = (val1 * val2 * val3);
	return T(pow(temp, v));
}



1>------ Rebuild All started: Project: project7_2, Configuration: Debug Win32 ------
1> project 7_2.cpp
1>d:\spring 2010\cmpsc\cmpsc 122\project7_2\project7_2\project 7_2.cpp(93): error C2668: 'pow' : ambiguous call to overloaded function
1> d:\program files\vs 2010\vc\include\math.h(583): could be 'long double pow(long double,int)'
1> d:\program files\vs 2010\vc\include\math.h(535): or 'float pow(float,int)'
1> d:\program files\vs 2010\vc\include\math.h(497): or 'double pow(double,int)'
1> while trying to match the argument list '(int, int)'
1> d:\spring 2010\cmpsc\cmpsc 122\project7_2\project7_2\project 7_2.cpp(184) : see reference to function template instantiation 'T Geometric<T>(T,T,T)' being compiled
1> with
1> [
1> T=int
1> ]
1> d:\spring 2010\cmpsc\cmpsc 122\project7_2\project7_2\project 7_2.cpp(47) : see reference to function template instantiation 'void runprogram<int>(void)' being compiled
1>d:\spring 2010\cmpsc\cmpsc 122\project7_2\project7_2\project 7_2.cpp(93): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Jimmy, int v = (1/3); will result in v = 0. Why did you declare it as int? Declare it as T, and then create a specialization for int. Read the link I gave you about templates.
Topic archived. No new replies allowed.