好吧,我试图修复这个程序,但我不断收到错误:
错误 1 error LNK2019: 未解析的外部符号“public: __thiscall ExcedeRangoInferior::ExcedeRangoInferior(void)” (??0ExcedeRangoInferior@@QAE@XZ) 在函数_main 中引用
错误2 error LNK2019: unresolved external symbol "public: __thiscall ExcedeRangoSuperior::ExcedeRangoSuperior(void)"(??0ExcedeRangoSuperior@@QAE@XZ) 在函数_main中引用
代码如下: 程序请求一个值,如果该值超过最小或最大范围,它会抛出异常
#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();
}
PD:这是一项家庭作业,其目的是修复其错误并使其正常运行,正如我在这里问的最后一件事所看到的那样,这段代码似乎可以显示更多错误,例如语法错误,也可能是设计和结构错误。
最佳答案
看起来您已经为所有异常类型声明了构造函数,但您还没有在任何地方定义这些构造函数。您收到链接器错误,指出未找到构造函数实现。尝试声明这些功能。例如:
ExcedeRangoInferior::ExcedeRangoInferior() {
// Implement me!
}
ExcedeRangoSuperior::ExcedeRangoSuperior() {
// Implement me!
}
希望这对您有所帮助!
关于C++ 异常/继承错误 LNK2019,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11043408/