等待队列 - "no matching function for call to"

Waitable Queue - "no matching function for call to"

我正在使用 Mutex、Semaphore 和 Exceptions 编写 Waitable 队列,但找不到为什么会出现一些编译器错误,这是第一个:

"../Include/SemaphoreException.h:17:90: no matching function for call to ‘ImpException::ImpException()’ SemaphoreException::SemaphoreException(const char* _file, size_t _line, std::string& _msg)"

我是不是用错了std::exception?

SemaphoreException 代码:

#ifndef __SEMAPHORE_EXCEPTION_H__
#define __SEMAPHORE_EXCEPTION_H__

#include "ImpException.h"

class SemaphoreException : public ImpException
{
public:
    SemaphoreException(const char* _file, size_t _line, std::string& _msg);
    virtual ~SemaphoreException() throw();
};

/************************************ should be implemented in a separated cpp file ************************************/

SemaphoreException::SemaphoreException(const char* _file, size_t _line, std::string& _msg)
{
    ImpException(_file, _line, _msg);
}

SemaphoreException::~SemaphoreException() throw() {}

#endif

这是 ImpException 代码:

#ifndef __IMP_EXCEPTION_H__
#define __IMP_EXCEPTION_H__

#include <exception>
#include <string>
#include <iostream>
#include <sstream>

class ImpException : public std::exception
{
public:
    ImpException(const char* _file, size_t _line, std::string& _msg);
    virtual ~ImpException() throw();

private:
    std::string m_msg;

    void Notify(const char* _file, size_t _line, std::string& _msg);
};


/************************************ should be implemented in a separated cpp file ************************************/

ImpException::ImpException(const char* _file, size_t _line, std::string& _msg) 
{
    Notify(_file, _line, _msg);
}

ImpException::~ImpException() throw() {}

void ImpException::Notify(const char* _file, size_t _line, std::string& _msg)
{
    std::stringstream ss;

    ss << _line;

    m_msg = std::string(_file) + std::string(": ") + ss.str() + std::string(": ") + _msg;

    ss << m_msg;
}

#endif

您的问题在这里:

SemaphoreException::SemaphoreException(const char* _file, size_t _line, std::string& _msg)
// You are missing the call to the base class constructor here
{
    ImpException(_file, _line, _msg); // this does not do what you think it does.
}

它应该看起来像这样,因为必须首先调用基础 class 构造函数:

SemaphoreException::SemaphoreException(const char* _file, size_t _line, std::string& _msg)
: ImpException(_file, _line, _msg)
{}

您的代码实际上等同于:

SemaphoreException::SemaphoreException(const char* _file, size_t _line, std::string& _msg)
: ImpException()  // compiler can't find this and complains!
{
    ImpException(_file, _line, _msg); // a momentary separate instance only.
}