(C++)在单独的文件中创建 std::expection 类时,输入错误末尾的预期"{"
(C++) Expected '{' at end of an input error while creating std::expection class in separate files
我需要创建自己的类,该类继承自std::exception。我需要在单独的文件中执行此操作。我使用了讲座中提供的例子,问题是它显示在一个.cpp文件中,一旦它被拆分,我就会得到一个错误。我该怎么修?
RzymArabException.h文件:(这是我在第11行得到错误的地方)
#ifndef RZYMARABEXCEPTION_H_INCLUDED
#define RZYMARABEXCEPTION_H_INCLUDED
#include <string>
#include <exception>
using namespace std;
class RzymArabException: public exception {
private:
string s;
public:
RzymArabException(string ss) : s(ss);
virtual ~RzymArabException() throw();
virtual const char* what() const throw();
};
#endif // RZYMARABEXCEPTION_H_INCLUDED
RzymArabException.cpp:
#include "RzymArabException.h"
#include <iostream>
#include <exception>
#include <string>
using namespace std;
RzymArabException(string ss) : s(ss) {}
virtual ~RzymArabException() throw() {}
virtual const char* what() const throw() {
return s.c_str();
}
错误是由于您有一个构造函数初始化列表,但这里没有格式良好的构造函数定义:
RzymArabException(string ss) : s(ss);
如果要在.cpp
中实现构造函数,请在标头中正确声明:
RzymArabException(string ss);
请注意,异常规范已被弃用,因此我已将它们从下面的代码中删除。
您的下一个问题是,成员定义都需要在RzymArabException
范围内:
//RzymArabException.cpp
RzymArabException::RzymArabException(string ss) : s(ss) {}
RzymArabException::~RzymArabException() {}
const char* RzymArabException::what() const {
return s.c_str();
}
相关文章:
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 堆栈中大小变量输入错误 (C++)
- 实现 DFS 在较短的输入下工作正常,但在较大的输入下会抛出分段错误
- C++中的输入限制和错误检查
- 关于 SIGTSTP 错误和无休止的输入
- 为什么不能通过在错误输入后设置 std::cin.clear() 来使用 std::cin?
- 这段代码在获得错误输入时不会阻止 stoi 运行
- 无法转换 .CATPart 文件.错误:输入文件路径似乎包含不支持的字符
- 使用 cin 错误输入多个浮点数
- 构造函数错误:输入末尾的预期“{”
- 如何一次一个单词而不是一行一行地消除错误输入
- 错误输入未在此范围中显示
- c++在没有tryCatch块的情况下捕获错误输入
- c++中的纠错循环,查找字符串中的特定字符并将其标记为错误输入
- 如何为错误输入编写异常处理
- Swig错误-错误:输入(2)中的语法错误
- 错误:输入末尾应为“}”
- std::cin无限循环错误输入
- 如何避免来自用户的错误输入?