c++ - 将队列用作全局队列并从 main 处理其内容

标签 c++ linux ubuntu

我收到有关模板参数无效的错误,并且没有看到我将队列变量声明为全局变量..,我尝试将它们放入函数中(但这不是我想要的,因为我想从main) 也不起作用..这是我的错误和代码

program.cpp: In function ‘int main(int, char**)’:
program.cpp:62:24: error: argument of type ‘bool (std::queue<std::basic_string<char> >::)()const’ does not match ‘bool’
program.cpp:62:24: error: in argument to unary !
program.cpp:68:23: error: argument of type ‘bool (std::queue<std::basic_string<char> >::)()const’ does not match ‘bool’
program.cpp:68:23: error: in argument to unary !






#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>
#include <string>
#include <queue>
#include <iostream>

#define SIZE 100

using namespace std;

  queue<string> bfFilesQueue;
  queue<string> bfDirsQueue;

static int display_info(const char *fpath, const struct stat *sb,
             int tflag, struct FTW *ftwbuf)
{

    //Check the type of Flag (i.e File or Directory)
    switch(tflag)
    {
        case FTW_D: 
        case FTW_F: 
                    bfFilesQueue.push(fpath);
                    break;

        case FTW_DP: 
                     bfDirsQueue.push(fpath);
                     break;
    }
    return 0; /* Continue */
}

int main(int argc, char *argv[])
{

    if (argc != 2) {
        fprintf(stderr, "Usage: %s directory_name\n", argv[0]);
        return 1; 
    }  

    int flags = FTW_DEPTH | FTW_MOUNT | FTW_PHYS;


    if (nftw(argv[1], display_info, 20, 1) == -1)
    {
        perror("nftw");
        return 255;
    }
   printf("***************************\n");

   while(!bfFilesQueue.empty)
   {
       cout << bfFilesQueue.front() << " ";
        bfFilesQueue.pop();
   }

   while(!bfDirsQueue.empty)
   {
       cout << bfDirsQueue.front() << " ";
        bfDirsQueue.pop();
   }


    return 0;
}

最佳答案

您的 while 语句试图检查函数 std::queue::empty() 本身,而不是函数调用的结果。错误消息中显示的类型是函数的类型。

正如 overcoder 的评论所说,添加括号将修复代码。

关于c++ - 将队列用作全局队列并从 main 处理其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7630384/

相关文章:

c++ - 与 nvidia 链接的 gcc 版本错误

c++ - 轮询 TCP 套接字空闲监听

linux - 没有 curl 或 wget 的 HTTP 请求(Ubuntu Core Bash)

c++ - 为什么enable_shared_from_this必须公开继承?

c++ - 对于参数 'std::string {aka std::basic_string<char>}' 到 'char*',无法将 '2' 转换为 'int Save(int, char*)'

c++ - OpenGL 透视图看起来不对

c++ - 为什么连续调用 new[] 不分配连续内存?

linux - Linux 上的 pthread_mutex_timedlock

c - 读取 wav 文件,持续时间/数据大小的计算总是错误的

java - Android Studio 仅在 Ubuntu 上以控制台模式运行