Template question

This is a discussion on Template question within the C++ Programming forums, part of the General Programming Boards category; Hi, I've just started reading about templates and have an error that I don't quite understand: Code: template<class T> struct ...

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21