当一个类在c ++中的头文件中继承另一个类时出错

时间:2018-05-12 16:45:12

标签: c++ c++11 constructor header

//头文件

#include <exception>
#ifndef CRYPTSERVEXCEPTION_H
#define CRYPTSERVEXCEPTION_H


class cryptServException
{
public:
//these are classes defined in a class 
class CantOpenForEncryption : public std::exception
{
public:

    const char* what();
};


class DecryptKeyMismatch : public std::exception
{

public :

const char* what();
};

#endif // CRYPTSERVEXCEPTION_H

有这个隐式构造函数给出错误我只是无法理解原因,因为我们可以将构造函数的主体留空,如果有一些代码放在其中我不知道是什么它应该,希望你现在遇到了问题。

// CPP文件

#include "cryptServException.h"
//rest are just classes that are for exceptions
 cryptServException::cryptServException()
 {
 // this constructor declaration gives the error : definition of implicitly- declared 'constexpr myException::myException()'|
 }
//this is just a class in another class
cryptServException::CantOpenForEncryption : public exception
{

public :

const char* what()
{
    return " Can't Open File For Encryption ";
}
};

//this is just a class in another class
cryptServException::DecryptKeyMismatch : public exception
{

public :

const char* what()
{
    return " Incorrect Decryption Key ";
}
};

我无法确定上述隐式构造函数的错误原因。

感谢您的帮助

你能否建议我在.cpp文件中添加一个构造函数应该在其中放置什么代码

1 个答案:

答案 0 :(得分:0)

该错误意味着目前编译器不知道exception令牌。它希望在那时看到一个类名,但是上面没有声明名为exception的类。

要解决此问题,请将#include <exception>添加到您的标头文件中(如果您的意思是std::exception,当然)。可能在您的源文件中间接包含此文件。