C++ 异常/继承错误 LNK2019

标签 c++

好吧,我试图修复这个程序,但我不断收到错误:

错误 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/

相关文章:

python - 等效的 C++ 时区转换?

尝试创建标记 vector 时出现 C++ 错误

c++ - 有没有办法替代使用函数的所有定义,并自动在源代码中更改变量名称?

c++ - Cygwin 上的 MinGW。关于链接 GNU 科学图书馆的一些问题

c++ - 体验在 Raspberry Pi 上为 PCF8575 I/O 扩展器编写 C 代码

C++ Boost::ASIO 线程池问题

c++ - 我的c++代码。会不会出现内存泄漏?

c++ - OpenGL 中的可视化问题(glOrtho 和投影)

c++ - Qt 容器中如何添加 noname 栈数据?

c++ - uri 正则表达式模式匹配