Cant define template member function

Hi, I can't make this code to run. I think i did everything like in my book but I'm getting these errors. Before i tried to add this get() function everything were working fine.
Any ideas what I did wrong?

Error	2	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	
Error	5	error C2509: 'get' : member function not declared in 'S<T>'	
Error	1	error C2146: syntax error : missing ';' before identifier 'get'	
Error	4	error C2143: syntax error : missing ';' before 'S<T>::get'	
Error	6	error C1903: unable to recover from previous error(s); stopping compilation	



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;


template<typename T>
struct S{
	T val;
	S(T value) : val( value ){}

	/*5. Add a funtion template get() that returns a reference to val*/
	&T get();
};

template<typename T>
&T S<T>::get(){
	return &val;
}
The return type &T is invalid.

If you want the function to return a reference to val you should change the return type to T& and remove the & from line 16.

If you want the function to return a pointer to val you should change the return type to T* and leave line 16 as it is.
Last edited on
Ohh, thank you very much :)
Topic archived. No new replies allowed.