Thread: Template question

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Template question

    Hi,

    I've just started reading about templates and have an error that I don't quite understand:
    Code:
    template<class T> struct S {
    
    	T val;
    	S(T n):val(n) {}
    };
    
    
    int main()
    {
       
    	S<int> a = 2;
    
    	cout << a;
    
    	cin.get();
    
    	return 0;
    }
    When I run, I get the error message: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'S<T>'

    Why won't this print? It looks right to me?


    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Oops....what a stupid error! I was trying to print the object rather than the objects member value!

    Fixed now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template already instantiated
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2010, 12:42 PM
  2. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Quick question about class template
    By merixa in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2005, 11:43 PM