Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在';之前应为主表达式';代币_C++ - Fatal编程技术网

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!");
...