CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2003
    Location
    Azerbaijan
    Posts
    74

    Help with templates in C++

    Why doesn't this code work???

    Code:
    template<class t>
    t sum( t a, t b )
    {
    	return a + b;
    }
    
    template<class t>
    class c_x {
    public:
    	t a, b, c;
    
    	void sum()
    	{
    		c = sum( a, b );
    	}
    };
    
    void main()
    {
    	c_x<int> x;
    	x.a = 10;
    	x.b = 20;
    	x.sum();
    }
    Error
    C2660: 'c_x<t>::sum' : function does not take 2 arguments
    with
    [
    t=int
    ]

  2. #2
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Code:
    template<class t>
    t sum( t a, t b )
    {
    	return a + b;
    }
    Code:
    	void sum()
    	{
    		c = sum( a, b );
    	}
    in case you don't get it, what does:

    sum(a,b) resolve to in class c_x...
    Last edited by Mick; May 1st, 2004 at 07:23 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured