我想實現異常並將其從我的方法中拋出。預期在';'之前的主表達式令牌
這裏是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
這裏cpp
文件
#include "../headers/exception.h"
using namespace core;
no_implementation::no_implementation(std::string error_message = "Not implemented!") {
this->error_message = error_message;
}
const char* no_implementation::what() const noexcept {
return this->error_message.c_str();
}
這裏是方法
std::string IndexedObject::to_string() {
throw noimp;
}
但它讓我錯誤
throw noimp; //expected primary-expression before ';' token
什麼問題?沒有你試着投類型,而不是一個對象的括號
throw noimp();
:
應該是'throw no_implementation'? Ps在傳遞'std :: String'時使用'const'和引用 –