Declaring Multiple Client Functions Using STL

I don't know how to get to use more than one client function in a program with template I can do one function and have it work but when it comes to more than one and i attempt to use two or more , it gives me errors like this:

|17|error: 'T' was not declared in this scope|
|17|error: template argument 1 is invalid|
|17|error: template argument 2 is invalid|
|18|error: 'T' was not declared in this scope|
|18|error: template argument 1 is invalid|
|18|error: template argument 2 is invalid|
||In function 'int main()':|
|72|error: invalid initialization of reference of type 'int&' from expression of type 'std::queue<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::deque<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >'|
|17|error: in passing argument 1 of 'void print_queue(int&)'|
|73|error: invalid initialization of reference of type 'int&' from expression of type 'std::stack<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::deque<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >'|
|18|error: in passing argument 1 of 'void print_stack(int&)'|
|25|warning: unused variable 'pos'|
||=== Build finished: 10 errors, 1 warnings ===|

#include <iostream>

using namespace std;

#include <list>
#include <queue>
#include <stack>
template <typename T>        
void print_list(list<T> &);          // prototype for client function
void print_queue(queue<T> &);        // prototype for client function
void print_stack(stack<T> &);        // prototype for client function

int main()
{
    list< string > lst;
    queue< string > package, package_temp;
    stack< string > box, box_temp;
    string::size_type pos;
    string choice, str;
    bool choice_flag = true;

    do{
        cin >> choice;
        if(choice == "QUIT"){
            choice_flag = false;
            break;
        }
        else{
           lst.push_back(choice);
        }
    }while(choice_flag);

    print_list(lst);

    cout << endl;
    while(!lst.empty()){
        str = lst.front();
        if(str.find('P',0))
            box.push(str);
        else
            package.push(str);
            lst.pop_front();
        }

    print_queue(package);
    print_stack(box);

    return 0;
}

// client (not primitive) function to print each item on list lst
template <typename T>
void print_list(list<T> &lst)
{
    list<T> temp;
    T x;

    cout << "The elements on the list are: " << endl;
    while (!lst.empty()) {
        x = lst.front();
        lst.pop_front();
        cout << x << endl;
        temp.push_back(x);
    }

    while (!temp.empty()) {
        x = temp.front();
        temp.pop_front();
        lst.push_back(x);
    }

    return;
}

// client (not primitive) function to print each item on queue package
template <typename T>
void print_queue(queue<T1> &package)
{
    queue<T1> temp;
    T1 x;

    cout << "The elements on the list are: " << endl;
    while (!package.empty()) {
        x = package.front();
        package.pop();
        cout << x << endl;
        temp.push(x);
    }

    while (!temp.empty()) {
        x = temp.front();
        temp.pop();
        package.push(x);
    }

    return;
}

Answers


You have to have the template<...> above every template function you write. You forgot it above the print_queue function, so just add template<typename T> above it and that should solve it.

You also haven't #included the string header, which you need to do to use std::string.


Need Your Help

temporary pointer point to a using vector address

c++ parsing pointers binary-tree

I have a question about parsing the string s into a binary tree.

How to copy data of one array to another in C?

c arrays pointers memcpy

I have to copy Mac address stored as an array with a pointer reference to another. I want to perform a swap but, i dont know if i am copying right.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.