Can not resolve overloaded on template

I'm trying to know which one is the bigger value at any type, for that propouse I have done a template, and I'm using a member function to resolve which one is the bigger, but I dont pass any argument I'm trying to resolve that with this...I dont If it can de done or not...here is my code I hope somebody can help me...

the copmiler gives to me an error in the resolve function about how I'm treating operator this...and at any call of resolve function It can not resolve because overload...

many thanks!!

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

template<class T>
class myclass{
	T a,b;
	
public:
	
	myclass (T x,T y):a(x),b(y){}; 
	
	T resolve(){
		T bigger;
		bigger =  this->a > this->? this->a:this->b;
		cout<<bigger<<"\n";
		return bigger;
	}
};

int main(){

	myclass<int> myobjectint(30,25);
	myclass<float> myobjectfloat(30.23,30.56);
	myclass<string> myobjectstring("malaga","granada");
	myobjectint.resolve;
	myobjectfloat.resolve;
	myobjectstring.resolve;
	return 0;
}
In line 15 you have forgotten b after this->

In lines 26,27,28 ... put parenthesis after resolve like this :

myobjectint.resolve();
myobjectfloat.resolve();
myobjectstring.resolve();
yes, tha's true I realized later on...thanks!!, now I'm having trouble resolving which on is ghe bigger string..look at the link if you want....


http://www.cplusplus.com/forum/general/166252/
why are you using 2 posts?

From your snippet in the link above, you are doing this->a.length(). This will work only if a is a string but that is not always the case as the class is a template class and can take any type.

Have you considered what happens if your class takes other custom types? Something like
1
2
3
4
class X
{};
myclass<X> myObject;
    


To solve you issue with strings, you can check if the type passed to the class is a string and then doing the corresponding action.

I'm using type_info to resolve what you suggested before, but now I'm having a trouble because the compiler says:: request for member 'length' in '((myclass<int>*)this)->myclass<int>::b', which is of non-class type 'int'

and with double andchar too..here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
void resolve( ){
		T bigger;
		if(typeid(this->a) == typeid(string())){
			 bigger =  this->a.length() > this->b.length() ? a:b;
			cout<<bigger<<"\n";
		}
		
		bigger =  this->a > this->b ? this->a:this->b;
		cout<<bigger<<"\n";
		
	}

Topic archived. No new replies allowed.