Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Template method returning type T

Status
Not open for further replies.

RichardF

Programmer
Oct 9, 2000
239
GB
Hi,

Im not sure if this is possible.

I want clients of this class to use AddField - so the class will store field information internally.

Then, after the data has been stored in memory, i want the client to call GetData :-

class cDataSet {
public:
cDataSet() {};
~cDataSet() {};

template<class T> void AddField(string Name, T Type) {};
template<class T> T GetValue() {} ;
};


However im having problems compiling (vc6,win2k)
// in main() ..

cDataSet cDS;
int i;

i = cDS.GetValue<int> (); // type 'int' unexpected.



Any ideas ?
Thanks in advance,
Rich.

&quot;Programmers are tools for converting caffeine into code - BSRF&quot;
 
Boy i don't know. You should get an error on the line
[red]template<class T> T GetValue() {} ;[/red]

because GetValue() must return a value.
[bugeyed]

Also i don't understand how your class is supposed to work without being a template itself. Won't it need to store T values somehow to return them from GetValue()? In which case, template members don't really make sense then do they?

-pete
 
pete,

i am assuming you can do this for methods, like you can do this for template functions :

// not in a class ...
template <class T> T Max(T v1, T v2)
{
return v1 > v2 ? v1 : v2;
};

...
printf(&quot;%lf&quot;,max<float> (15f,14.9f));

following this assumption, the following line should return type T :

template<class T> T GetValue() {} ;

finally, the encapsulating class should not be a template class because i need GetValue to return different types.

i guess im trying to be too smart, and should just have an overridden function that returns each type independantly.

regards.

&quot;Programmers are tools for converting caffeine into code - BSRF&quot;
 
>> following this assumption, the following line should >> return type T :

template<class T> T GetValue() {} ;

richard, i am totaly lost, that function has nothing in the body so how can it return anything? Since it declares a return value and does not contain a return statement in the function body, it should produce a compiler error, it does on my system.

>> the encapsulating class should not be a template class
>> because i need GetValue to return different types.

Ok, but where are these types comming from? In your code there is no member variable in the class so where is the member function &quot;GetValue&quot; going to get it's return value from?
Code:
template<class T> T GetValue(){
  return ??? // what? return what?
}

-pete
 
sorry, i was more concerned if the function declaration was correct and completely forgot about the implementation.

to keep it simple:

template<class T> T GetValue()
{
return (T) 1;
};

although, i am still getting the same compile error

Rich.

&quot;Programmers are tools for converting caffeine into code - BSRF&quot;
 
The following compiles and executes as expected in VC7 on Windows XP Professional
Code:
class cDataSet {                
public:
    cDataSet() {};
    ~cDataSet() {};

    template<class T> void AddField(string Name, T Type) {};
    template<class T> T GetValue() {
		 return (T)12;
	 } ;
};

int _tmain(int argc, _TCHAR* argv[])
{
	cDataSet cds;
	int n = cds.GetValue<int>();
	cout << &quot;n: &quot; << n << endl;


-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top