Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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++_Assert - Fatal编程技术网

C++ 验证程序的断言失败

C++ 验证程序的断言失败,c++,assert,C++,Assert,我创建了一个类和一个验证器,所以我想使用验证器的断言进行一些测试。这就是我要说的: 阶级医学 class Medicine{ private: int ID; string name; public: Medicine(){ this->ID=0; this->name=""; } Medicine(int ID, string name, float concentration, int quantity){

我创建了一个类和一个验证器,所以我想使用验证器的断言进行一些测试。这就是我要说的:

阶级医学

class Medicine{
private:
    int ID;
    string name;
public:
    Medicine(){
        this->ID=0;
        this->name="";
    }
    Medicine(int ID, string name, float concentration, int quantity){
        this->ID=ID;
        this->name=name;
    }
    ~Medicine(){};

    //inline get methods

    int getID(){
        return ID;
    }

    string& getName(){
        return name;
    }

    //inline set methods

    void setID(int newID){
        this->ID = newID;
    }

    void setName(string newName){
        this->name = newName;
    }
};
例外情况

#include <string>
using namespace std;

#include <string>
using namespace std;

class MyException
{
public:
    MyException(string msg):message(msg){}
    const string& getMessage() const {return message;}
private:
    string message;
};

class ValidatorException: public MyException
{
public:
    ValidatorException(string msg): MyException(msg){}
};

class RepositoryException: public MyException
{
public:
    RepositoryException(string msg): MyException(msg){}
};
#包括
使用名称空间std;
#包括
使用名称空间std;
类MyException
{
公众:
MyException(字符串msg):消息(msg){}
常量字符串&getMessage()常量{return message;}
私人:
字符串消息;
};
类验证程序异常:公共MyException
{
公众:
ValidatorException(字符串消息):MyException(消息){}
};
类RepositoryException:公共MyException
{
公众:
RepositoryException(字符串msg):MyException(msg){}
};
药物验证器

#include "exceptions.h"
#include "medicine.h"

class MedicineValidator{
public:
    void validate(Medicine& m) throw (ValidatorException);
};

#include "medicineValidator.h"

void MedicineValidator::validate(Medicine& m) throw (ValidatorException){
    string message="";
    if(m.getID()<1){
        message+="The ID should be positive and >0!";
    }
    if(m.getName()==""){
        message+="The name field is empty, and it shouldn't!";
    }
    if(m.getConcentration()<1){
        message+="The concentration should be positive(>0)!";
    }
    if(m.getQuantity()<1){
        message+="The quantity should be positive(>0)!";
    }
}
#包括“exceptions.h”
#包括“medicine.h”
类药物验证器{
公众:
无效验证(医学和医学)抛出(验证异常);
};
#包括“medicineValidator.h”
作废MedicineValidator::validate(Medicine&m)抛出(ValidatorException){
字符串消息=”;
if(m.getID()验证(*m);
m->setID(-2);//ID不应小于1,因此它应该捕获异常,但测试失败
试一试{
medValidator->validate(*m);
断言(假);
}
捕获(异常和异常){
断言(正确);
}
删除m;
}
我做错了什么?我只是看不到错误…也许你可以。:)当我运行程序时,我得到的是:“断言失败:false,file..\src/utils/。/domain/testMedicine.h,第32行

此应用程序已请求运行时以异常方式终止它。
请与应用程序的支持团队联系以获取更多信息。“

您不会在验证方法中抛出异常消息


和assert(false)中止程序执行

您不会在验证方法中抛出异常消息


和assert(false)中止程序执行

如果你告诉我们发生了什么,而不是仅仅转储你的所有代码……到底是什么问题?@DanielDaranas我还能说什么?当我运行程序时,它说“断言在第行失败…”。测试失败了。也许如果你告诉我们发生了什么,而不是仅仅丢弃你所有的代码……到底是什么问题?@DanielDaranas我还能说什么?当我运行程序时,它说“断言在第行失败…”。测试失败。发现问题。谢谢我忘了…发现了问题。谢谢我忘了。。。
void testValidator(){
    Medicine* m = new Medicine(1,"para",30,40);
    MedicineValidator* medValidator = new MedicineValidator();
    medValidator->validate(*m);

    m->setID(-2); //the ID should not be < 1 so it should catch the exception but the test fails
    try{
        medValidator->validate(*m);
        assert(false);
    }
    catch(ValidatorException& ex){
        assert(true);
    }
    delete m;
}