c++ - 等待队列 - “没有匹配的调用函数”
问题描述
我正在使用 Mutex、Semaphore 和 Exceptions 编写等待队列,但找不到为什么会出现一些编译器错误,这是第一个错误:
“../Include/SemaphoreException.h:17:90: 没有匹配函数调用‘ImpException::ImpException()’ SemaphoreException::SemaphoreException(const char* _file, size_t _line, std::string& _msg)”
我是否错误地使用了 std::exception?
信号量异常代码:
#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.
}
它应该看起来像这样,因为必须首先调用基类构造函数:
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.
}