Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

C++:using template containers in function

  • 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, Registered Users 2 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, Registered Users 2 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
Advertisement