等待队列-“没有匹配的呼叫功能”

时间:2019-01-04 15:48:30

标签: c++ exception mutex semaphore

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

  

“ ../ Include / SemaphoreException.h:17:90:没有匹配的调用函数   到“ 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

1 个答案:

答案 0 :(得分:0)

您的问题在这里:

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

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

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