c++ - 使用 STL 声明多个客户端函数

标签 c++ templates stl typename

我不知道如何在带有模板的程序中使用多个客户端函数我可以执行一个函数并让它工作但是当涉及到多个并且我尝试使用两个或更多时,它给我这样的错误:

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

最佳答案

您必须拥有 template<...>在您编写的每个模板函数之上。你忘了它上面的 print_queue功能,所以只需添加 template<typename T>在它上面,应该可以解决它。

你也没有#include dstring header ,您需要这样做才能使用 std::string .

关于c++ - 使用 STL 声明多个客户端函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8074178/

相关文章:

c++ - 当一个线程锁定一张大 map 时如何避免卡住其他线程

c++ - 在 C++ 中分割字符串

c++ - 可以从外部访问私有(private)运营商吗?

c++ - WIN API ReadFile() 返回 GetLastError() ERROR_INVALID_PARAMETER

c++ - 对 C++ 空指针使用 NULL 或 nullptr?

c++ - 安全地直接访问类(class)成员

c++ - 模板成员变量特化

c++ - 编译器无法识别自定义 numeric_limits 成员函数

c++ - STL std::map 的更快替代方案

c++ - 为什么我不能使用 std::unique_ptr 来避免循环依赖?