C++ C++;异常/继承错误LNK2019
嗯,我正试图修复这个程序,但我不断发现错误: 错误1错误LNK2019:未解析的外部符号“public:u thiscall ExceederAngoSubsier::ExceederAngoSubsier(void)”(??0 ExceederAngoSubsier@@QAE@XZ)在函数_main中引用 错误2错误LNK2019:未解析的外部符号“public:u thiscall excederangoperior::excederangoperior(void)”(??0 excederangoperior@@QAE@XZ)在函数_main中引用 代码如下: 程序请求一个值,如果该值超过最小或最大范围,则抛出异常C++ C++;异常/继承错误LNK2019,c++,C++,嗯,我正试图修复这个程序,但我不断发现错误: 错误1错误LNK2019:未解析的外部符号“public:u thiscall ExceederAngoSubsier::ExceederAngoSubsier(void)”(??0 ExceederAngoSubsier@@QAE@XZ)在函数_main中引用 错误2错误LNK2019:未解析的外部符号“public:u thiscall excederangoperior::excederangoperior(void)”(??0 exceder
#include <iostream>
#include <exception>
class ExcepcionRango : public std::exception{
protected:
ExcepcionRango();
public:
virtual const char* lanzarExcepcion()=0;
};
class ExcedeRangoInferior : public ExcepcionRango{
public:
ExcedeRangoInferior();
const char* lanzarExcepcion() throw(){ //throw exception
return "Value out of minimal range";
}
};
class ExcedeRangoSuperior : public ExcepcionRango{
public:
ExcedeRangoSuperior();
const char* lanzarExcepcion() throw(){ //throw exception
return "value out of maximal range";
}
};
int obtainValue(int minimo, int maximo){ //obtain value
int valor; //value
std::cout<<"Introduce a value between "<<minimo<<" and "<<maximo<<" : "<<std::endl;
std::cin>>valor;
return valor;
};
int main(){
ExcedeRangoSuperior* exS = new ExcedeRangoSuperior();
ExcedeRangoInferior* exI= new ExcedeRangoInferior();
int min=3;
int max=10;
int valor=0; //value
try{
valor=obtainValue(min,max);
}catch(int){
if(valor<min){
exS->lanzarExcepcion();
}
if(valor>max){
exI->lanzarExcepcion();
}
}
delete exS;
delete exI;
std::cin.get();
}
#包括
#包括
类ExcepcionRango:public std::exception{
受保护的:
ExcepcionRango();
公众:
虚拟常量字符*LANZAREXEPCION()=0;
};
类别ExceederAngoSubsier:公共ExcepcionRango{
公众:
超下();
const char*lanzarexepcion()throw(){//throw异常
返回“值超出最小范围”;
}
};
班级例外上级:公共例外{
公众:
超越上级();
const char*lanzarexepcion()throw(){//throw异常
返回“值超出最大范围”;
}
};
int获取值(int最小值,int最大值){//获取值
int valor;//值
std::cout看起来您已经为所有异常类型声明了构造函数,但尚未在任何位置定义这些构造函数。您收到的链接器错误表明找不到构造函数实现。请尝试声明这些函数。例如:
ExcedeRangoInferior::ExcedeRangoInferior() {
// Implement me!
}
ExcedeRangoSuperior::ExcedeRangoSuperior() {
// Implement me!
}
希望这有帮助!看起来您已经为所有异常类型声明了构造函数,但您还没有在任何地方定义这些构造函数。您收到链接器错误,表示找不到构造函数实现。请尝试声明这些函数。例如:
ExcedeRangoInferior::ExcedeRangoInferior() {
// Implement me!
}
ExcedeRangoSuperior::ExcedeRangoSuperior() {
// Implement me!
}
希望这有帮助!是的,谢谢!我刚刚在所有构造函数中更改了它,它运行正常,但现在,在引入该值后,我没有得到任何异常:可能是我声明抛出异常的函数的方式有错误,这是一个单独的问题。您的代码实际上没有抛出任何异常(请注意,此处没有throw
关键字)。如果你想发布一篇后续文章寻求帮助,那么你可以让整个社区都来看看。是的,谢谢!我刚刚在所有构造函数中更改了它,它运行正常,但是现在,在引入该值之后,我没有得到任何异常:可能是我声明抛出异常的函数的方式有错误这是一个单独的问题。你的代码实际上不会抛出任何异常(请注意,这里没有throw
关键字)。如果你想发布一篇后续文章,寻求帮助,那么你可以让整个社区都来看看。