Объявление нескольких клиентских функций с использованием STL - PullRequest
0 голосов
/ 10 ноября 2011

Я не знаю, как заставить использовать более одной клиентской функции в программе с шаблоном. Я могу сделать одну функцию и заставить ее работать, но когда дело касается нескольких и я пытаюсь использовать две или более, онавыдает мне такие ошибки:

|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;
}

1 Ответ

1 голос
/ 10 ноября 2011

Вы должны иметь template<...> над каждой функцией шаблона, которую вы пишете. Вы забыли его над функцией print_queue, поэтому просто добавьте template<typename T> над ним, и это должно решить ее.

У вас также нет #include d заголовка string, который вам нужно сделать, чтобы использовать std::string.

...