gpt4 book ai didi

c++ - C++中异常类的继承

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:07 31 4
gpt4 key购买 nike

我正在用 C++ 编写一些继承自基类的异常类,但我不明白为什么它无法编译。任何帮助,将不胜感激。

基类:
RandomAccessFileException.h

#ifndef RANDOMACCESSFILEEXCEPTION_H
#define RANDOMACCESSFILEEXCEPTION_H

class RandomAcessFileException
{
public:
RandomAcessFileException();
virtual const char* getMessage() = 0;
protected:
char m_message[100];
};

#endif

派生类:
RandomAccessFileNotFoundException.h

#ifndef RANDOMACCESSFILENOTFOUNDEXCEPTION_H
#define RANDOMACCESSFILENOTFOUNDEXCEPTION_H

#include "RandomAccessFileException.h"

class RandomAccessFileNotFoundException : public RandomAccessFileException
{
public:
RandomAccessFileNotFoundException(const char* p_filename);
const char* getMessage();
};

#endif

RandomAccessFileNotFoundException.cpp

#include <cstring>

#include "RandomAccessFileException.h"
#include "RandomAccessFileNotFoundException.h"

RandomAccessFileNotFoundException::RandomAccessFileNotFoundException(const char* p_filename)
{
strcat(m_message, "RandomAccessFileNotFoundException: File: ");
strcat(m_message, p_filename);
}

const char* RandomAccessFileNotFoundException::getMessage()
{
return m_message;
}

g++ 说:

在 RandomAccessFileNotFoundException.cpp:4:0 包含的文件中:RandomAccessFileNotFoundException.h:13:1: 错误:“{”标记前的预期类名RandomAccessFileNotFoundException.cpp:在构造函数“RandomAccessFileNotFoundException::RandomAccessFileNotFoundException(const char*)”中:RandomAccessFileNotFoundException.cpp:8:12: 错误:“m_message”未在此范围内声明RandomAccessFileNotFoundException.cpp:在成员函数‘const char* RandomAccessFileNotFoundException::getMessage()’中:RandomAccessFileNotFoundException.cpp:14:12: 错误:“m_message”未在此范围内声明

最佳答案

第一个问题:

你必须:

#include "RandomAccessFileException.h"

在您的 RandomAccessFileNotFoundException.h 头文件中,因为它包含 RandomAccessFileNotFoundException 基类的定义(即 RandomAccessFileException)。

总而言之,您的头文件 RandomAccessFileNotFoundException.h 头应该是:

#ifndef RANDOMACCESSFILENOTFOUNDEXCEPTION_H
#define RANDOMACCESSFILENOTFOUNDEXCEPTION_H

#include "RandomAccessFileException.h"

class RandomAccessFileNotFoundException : public RandomAccessFileException
// ^^^^^^^^^^^^^^^^^^^^^^^^^
// This class is defined in the
// RandomAccessFileException.h
// header, so you have to #include
// that header before using this
// class as a base class.
{
public:
RandomAccessFileNotFoundException(const char* p_filename);
const char* getMessage();
};

#endif

第二个问题:

另请注意,您有错别字。您的基类称为:

RandomAcessFileException
// ^

代替:

RandomAccessFileException
// ^^

这是您在 RandomAccessFileException.h 中使用的名称。

第三个问题:

最后,您缺少基类的 (RandomAccessFile) 构造函数的定义,您只为其提供了一个声明:

class RandomAcessFileException
{
public:
RandomAcessFileException();
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
// This is a DECLARATION of the constructor, but the definition is missing
virtual const char* getMessage() = 0;
protected:
char m_message[100];
};

如果不提供定义,链接器将发出 Unresolved 引用错误。

关于c++ - C++中异常类的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16491104/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com