简体   繁体   English

编译错误:无效使用void表达式

[英]Compilation error: invalid use of void expression

I have a function which will pop task-codes from a queue and search it in a map of task-codes and tasks(methods) and push the the matched task into another queue which I want to pop out and executed sequentially later. 我有一个函数,该函数将从队列中弹出任务代码,并在任务代码和任务(方法)的映射中进行搜索,然后将匹配的任务推入另一个我要弹出并稍后顺序执行的队列中。 Below is the function: 下面是功能:

void CertManMgmtEECertController::PrepareTask()
{
    while(_taskCodeQueue.empty())
    {
        _taskQueue.push((_taskStore.find(_taskCodeQueue.pop()))->second);
    }
}

Below is the header where the class of this function is defined: 下面是定义此函数的类的标题:

class CertManMgmtEECertController : public virtual CertManMgmtCertificate
{
    public:
        CertManMgmtEECertController();
        ~CertManMgmtEECertController();
        void PerformTask();
        void SetTask(CertManMgmtEETaskCode taskCode);
        typedef void (CertManMgmtEECertController::*Task)();

    private:
        CertManMgmtEETaskCode _task;
        queue<Task> _taskQueue;
        queue<CertManMgmtEETaskCode> _taskCodeQueue;
        map<CertManMgmtEETaskCode,Task> _taskStore;

        void LoadTasks();
        void PrepareTask();
        void ExecuteTaskQueue();
        void GetEECert();
        bool GetCertificate();
};

But I am getting this below error while compiling: 但是我在编译时遇到以下错误:

CertManMgmtDomainController.h:49: error: 'CertManMgmtEECertController' does not name a type
CertManMgmtEECertController.cpp: In member function 'void certman::CertManMgmtEECertController::PrepareTask()':
CertManMgmtEECertController.cpp:42: error: invalid use of void expression
CertManMgmtEECertController.cpp: In member function 'void certman::CertManMgmtEECertController::ExecuteTaskQueue()':
CertManMgmtEECertController.cpp:55: error: void value not ignored as it ought to be

Where am I going wrong? 我要去哪里错了? I know there are other compilation errors here too. 我知道这里也有其他编译错误。 Please help. 请帮忙。

In principle the question contains enough information to answer about invalid use of void expression error. 原则上,该问题包含足够的信息来回答有关invalid use of void expression错误的信息。

std::queue::pop() returns void . std :: queue :: pop()返回void What you want in PrepareTask is probably 您在PrepareTask想要的可能是

while (!_taskCodeQueue.empty()) {
//    ^^^ also fixed condition
    _taskQueue.push((_taskStore.find(_taskCodeQueue.front()))->second);
    _taskCodeQueue.pop();
}

A good rule of thumb is to start with the first error message . 一个好的经验法则是从第一个错误消息开始

In your case that is 你的情况是

 CertManMgmtDomainController.h:49: error: 'CertManMgmtEECertController' does not name a type 

Possibly you have a typo, possibly the header is not included in the implementation file, possibly the identifier has been redefined as a function, whatever. 可能是您输入错误,可能是标头未包含在实现文件中,也可能是已将标识符重新定义为函数,无论如何。 But start with this. 但是从此开始。 Ignore the rest of the errors: they are 忽略其余错误:它们是 quite probably 很可能 possibly caused by this first one. 可能由第一个引起的。

Edit: After writing the above I see in another answer that you're using a std::queue , and indeed, its pop method returns void . 编辑:写完上面的内容后,我在另一个答案中看到您使用的是std::queue ,实际上,它的pop方法返回void This is for exception safety. 这是出于例外安全性。 There is no convenience method that combines pop and front . 没有结合popfront 便捷方法


Regarding the typo possibility, note that 关于错别字的可能性,请注意

  • modern IDEs such as Visual Studio provide tooltips about identifiers, and 诸如Visual Studio之类的现代IDE提供了有关标识符的工具提示,并且

  • C++ provides namespaces to deal with common prefixes in a more practical way. C ++提供了以更实际的方式处理通用前缀的名称空间

查看是否在typedef帮助中添加namespace

 typedef void (certman::CertManMgmtEECertController::*Task)();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM