-2

i've done a program. Unfortunately when trying to build it i got an error in function: undefined reference to `RzymArabException::RzymArabException(std::string). When i was throwing a simple class like class Rzym{}; there was no errors. But when i created a class with some kind data(constructors and messages inside it doesnt work) I would be grateful if u could point where the mistake is.

#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;

}
user3402584
  • 410
  • 4
  • 8
  • 18
  • That message just means you haven't implemented the `RzymArabException(string message)` constructor. (More specifically, it means something in your program is constructing a `RzymArabException` using that constructor, but the linker couldn't find a definition of it.) – Wyzard Mar 23 '14 at 18:10
  • I can't see any definitions for the functions you have declared n your `RzymArabException` class?!? – πάντα ῥεῖ Mar 23 '14 at 18:13
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Luchian Grigore Apr 09 '14 at 11:04

2 Answers2

1

You need to provide definitions for your exception class methods, to link properly:

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;}   

};

Also I would recommend to derive any class used as exception to derive from 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(); }   

};

This ensures that any standard compliant code will be able to catch your exception without having to use catch(...).

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • man thanks again. What book would u suggest for a newbie like me? It seems that learning from the net is not so efficient. – user3402584 Mar 23 '14 at 18:26
  • @user3402584 The site offers a [list of good books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), you can refer to. I personally learned the most from B. Stroustrup's 'The C++ Language'. There's also a number of good links in the [c++ tag wiki & FAQ](http://stackoverflow.com/tags/c%2b%2b/info). – πάντα ῥεῖ Mar 23 '14 at 18:31
0

It's self-explanatory. You did not define that constructor; you only declared it.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055