Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++:using template containers in function

Options
  • 30-10-2008 10:38pm
    #1
    Closed Accounts Posts: 225 ✭✭


    hey all,
    quick question is this possible

    i want to use a template function that takes a list or vector container of some type, lets say a vector<int> and list<int> as the parameter
    template <class container>
    void printContents(container c)
    {
    	container::iterator iter = c.begin();
    	while (iter != c.end())
    	{
    		cout << *iter << endl;
    		++iter;
    	}
    }
    
    int main()
    {
    	vector<int> intVec;
    	
    	intVec.push_back(1);
    	intVec.push_back(2);
    	intVec.push_back(3);
    	
    	printContents(intVec);
    	
    	list<int> intlist;
    	
    	intlist.push_back(1);
    	intlist.push_back(2);
    	intlist.push_back(3);
    	
    	printContents(intlist)
    	
    	return 1;
    }
    

    is this possible????

    thanks


Comments

  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Add this to the start of the template function (as first two statements in body of funtion) and it'll work.
    typedef typename container::iterator cont_iter;
        cont_iter iter = c.begin();
    

    The reason this fails is because the "container::iterator" is a dependent type and the C++ compiler knows nothing about this type at compile time, or even that it is a nested type, so we need to tell the compiler it's one.

    In short: "if your type is qualified by a template type parameter, you must use typename."

    You generally shouldn't need typedef, but g++ 4.2 see to insist. :)


  • Closed Accounts Posts: 225 ✭✭marktsang


    thats great!!
    worked fine.

    thanks
    mark


  • Closed Accounts Posts: 225 ✭✭marktsang


    hey,
    on a related note how would i make the return of that function be an iterator of the con types class??
    template <class container, class iter>
    iter printContents(container c)
    {
    	container::iterator iter = c.begin();
    	while (iter != c.end())
    	{
    		cout << *iter << endl;
    		++iter;
    	}
            return c.begin();
    }
    
    int main()
    {
    	vector<int> intVec;
    	
    	intVec.push_back(1);
    	intVec.push_back(2);
    	intVec.push_back(3);
    	
    	printContents(intVec);
    	
    	list<int> intlist;
    	
    	intlist.push_back(1);
    	intlist.push_back(2);
    	intlist.push_back(3);
    	
    	printContents(intlist)
    	
    	return 1;
    }
    


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    I'm not 100% sure how to go about this, my template programming skills are very limited.

    If you ask on the comp.lang.c++ newsgroup (via groups.google.com) you're sure to get an answer, the guys on there really know there template code.


  • Closed Accounts Posts: 225 ✭✭marktsang


    thanks alot for having a look at it -
    i will have a look at comp.lang.c++ newsgroup

    cheers,
    mark


  • Advertisement
Advertisement