繁体   English   中英

对另一个函数中使用的异常的未定义引用

[英]Undefined reference to exception used in another function

我做了一个程序。 不幸的是,在尝试构建它时,我在函数中出现错误:未定义对 `RzymArabException::RzymArabException(std::string) 的引用。 当我抛出一个像 class Rzym{} 这样的简单类时; 没有错误。 但是,当我创建一个包含某种数据的类(其中的构造函数和消息不起作用)时,如果您能指出错误所在,我将不胜感激。

#include <iostream>
#include <string>

using namespace std;

class RzymArabException{                      //wyjatki
    private:
        string message;
        int pozazakres;
    public:
        RzymArabException(string message);
        RzymArabException(int pozazakres);
        string getMessage(){return message;};   

};



class RzymArab {
    private:
        static string rzym[13];              //konwersja z arabskich na rzymskie 
        static int arab[13];

        static char rzymskie[7];
        static int arabskie[7];              //konwersja z rzymskich na arabskie
    public:
        static int rzym2arab(string);
        static string arab2rzym(int);
};


string RzymArab::rzym[13] = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
int RzymArab::arab[13] = {1,4,5,9,10,40,50,90,100,400,500,900,1000};

int RzymArab::arabskie[7] = {1000,500,100,50,10,5,1};
char RzymArab::rzymskie[7] = {'M','D','C','L','X','V','I'};

 string RzymArab::arab2rzym(int x){
        string s="";
     if(x<1 || x>3999)
        throw RzymArabException("Podana liczba w zapisie arabskim nie nalezy do dozwolonego przedzialu:(1..3999)");
     else{
        int i=12;

        while(x>=1){
            if(x>=arab[i]){
                x-=arab[i];
                s=s+rzym[i];
            }
            else
                i-=1;
        }
        }       
    return s;

}

您需要为异常类方法提供定义,以便正确链接:

class RzymArabException{                      //wyjatki
private:
    string message;
    int pozazakres;
public:
    // Note the changes for the constructor methods!
    RzymArabException(string message_) : message(message_) {}
    RzymArabException(int pozazakres_) : pozazakres(pozazakres_) {}
    string getMessage(){return message;}   

};

此外,我建议派生用作异常的任何类,以从std::exception派生:

class RzymArabException : public std::exception {
private:
    string message;
    int pozazakres;
public:
    // ...
    // Instead of getMessage() provide the what() method
    virtual const char* what() const { return message.c_str(); }   

};

这确保了任何符合标准的代码都能够捕获您的异常而无需使用catch(...)

这是不言自明的。 你没有定义那个构造函数; 你只是宣布了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM