【发布时间】:2017-06-02 07:55:41
【问题描述】:
我是使用 c++ 编写异常的新手,我正在努力解决一个错误。我不会详细介绍,因为我认为这与我遇到的问题无关。
#ifndef _STUDENT_H_
#define _STUDENT_H_
#include <string>
#include <map>
#include <vector>
#include <stdexcept>
#include <iostream>
class NoMarkException: public exception
{
public: NoMarkException():exception(){
cout<< "No marks were found." << endl;
}/*
NoMarkException(const string &name){
cout << "No marks for " << name << " were found."<< endl;
}*/
};
#endif
这是我的 NoMarkException 类
float Student::getMaxMark() const throw(NoMarkException) {
if (marks.empty()) {
throw NoMarkException::NoMarkException();
}
float final = 0;
for (it = marks.begin(); it != marks.end(); it++) {
if ((*it).second > final) {
final = (*it).second;
}
}
return final;
}
这是我的 Student.cpp
当我构建项目时,我得到 error: cannot call constructor 'NoMarkException::NoMarkException' directly 任何想法为什么会导致问题?
【问题讨论】:
-
在没有“NoMarkException::”的情况下重试。
-
附注。名称以下划线开头,后跟大写字母保留给实现。而且我们不再使用异常规范。
-
throw()异常规范有点过时了;试试noexcept
标签: c++ exception constructor