C++ 在';之前应为主表达式';代币
我试图实现异常并将其从方法中抛出 这是异常的C++ 在';之前应为主表达式';代币,c++,C++,我试图实现异常并将其从方法中抛出 这是异常的h文件 #ifndef EXCEPTION_H #define EXCEPTION_H #include <exception> #include <string> namespace core { class no_implementation: public std::exception { private: std::string error_message; public: no_implement
h
文件
#ifndef EXCEPTION_H
#define EXCEPTION_H
#include <exception>
#include <string>
namespace core {
class no_implementation: public std::exception {
private:
std::string error_message;
public:
no_implementation(std::string error_message);
const char* what() const noexcept;
};
typedef no_implementation noimp;
}
#endif
这里是方法
std::string IndexedObject::to_string() {
throw noimp;
}
但它显示了我的错误
throw noimp; //expected primary-expression before ';' token
有什么问题吗?要创建一个临时类型,您需要使用这样的符号(注意额外的括号):
如果没有括号,则尝试抛出类型而不是对象。除非将默认值移动到声明中,否则您还将指定字符串:默认值在使用时需要可见。首先,默认参数,如
std::string error\u message=“Not implemented!”
应该放在函数声明中,而不是定义中。就是写,
...
public:
no_implementation(std::string error_message = "Not implemented!");
...
第二,抛出值,而不是类型。您已经编写了
抛出noimp
但是noimp
是类的名称。这可能是throw noimp()
或抛出noimp(“此处有消息”)代码>这应该是不抛出任何实现
?Ps在传递std::String
throw noimp();
...
public:
no_implementation(std::string error_message = "Not implemented!");
...