1

hpp file :

#pragma once
#include <string>

using namespace std;

class LiczbaException: public exception{
private:
      string s;

public:
    LiczbaException(string ss) :
                    s(ss){
                                            };

    virtual ~ LiczbaException() throw () {
    };

    virtual const char * erorrr() const throw () {
            return s.c_str();
            }
};


class RzymArab {

private:

static string liczby[13];
static int arabliczby[13];

public:
static int rzym2arab (string rzym);
static string arab2rzym (int arab) throw(LiczbaException);

};

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

test cpp :

#include <iostream>
#include <cstdlib>
#include <cstring>

#include "RzymArab.hpp"

using namespace std;

int main(int argc, char * argv[]) {
    bool sprawdz = true;
    int t = 0;
    string pom;
    for (int l = 1; l < argc; l++) {
            try {
                    t = atoi(argv[l]);
                    if (t == 0)
                            throw pom;
                    else
                            sprawdz = true;
            } catch (string pom){
                    sprawdz = false;
            }
            if (sprawdz == false){
                    try{
                        int wynikk = RzymArab::rzym2arab(argv[l]);
                        if (argv[l] != RzymArab::arab2rzym(wynikk))
                            cout << "Nieistniejaca liczba" << endl;
                        else
                            cout << wynikk << "\n";
                    } catch (LiczbaException& w) {
                            cout << w.what() << endl;
                    }
            } else {
                    try {
                        string wynik = RzymArab::arab2rzym(t);
                        cout << wynik << "\n";
                    } catch (LiczbaException& w){
                            cout << w.what() << endl;
                    }
            }
    }
    return 0;
}

cpp file:

#include <iostream>
#include <cstdlib>
#include <cstring>

#include "RzymArab.hpp"

using namespace std;


class RzymArab 
    { 
    static int rzym2arab(string rzym) {
                    int wynik = 0;
                            for (int i = 0; i < rzym.length(); i++) {
                                    int wart1 = 0;
                                    int wart2 = 0;
                                    for (int j = 0; j < 13; j++) {
                                            if (rzym.at(i)==liczby[j].at(0)) {
                                                    wart1 = arabskie[j];
                                            }
                                    }
                                    for (int k = 0; k < 13; k++) {
                                            if (i<rzym.length()-1) {
                                                    if (rzym.at(i+1)==liczby[k].at(0))
                                                            wart2 = arabskie [k];  
                                            }
                                    }
                                    if (wart1 >= wart2) {
                                            wynik+=wart1;
                                    }else {
                                            wynik+=(wart2 - wart1);
                                            i = i + 1;
                                    }
                            }

            if (wynik > 3999)
                    cout << "Za duza liczba!";
            if (wynik == 0)
                    cout << "Something get's wrong!";
            return wynik;
    }

    static string arab2rzym(int arab)  throw (LiczbaException) {
            string wynik = "";
            if (arab < 1 || arab > 3999)
                    throw LiczbaException("Zla podstawa");
            for (int i = 0; i < 13; i++) {
                    while (arab >= arabskie[i]) {
                            wynik += liczby[i];
                            arab -= arabskie[i];
                    }
            }
            return wynik;
    }
};

I got the errors while compiling: " RzymArab.cpp:10:7: error: redefinition of 'class RzymArab' class RzymArab

In file included form RzymArab.cpp:5:0: RzymArab.hpp:26:7: error: previous definition of 'class RzymArab' class RzymArab"

I don't know what is wrong, maybe something with pragma once but when i use old guard nothing change.

dandan78
  • 13,328
  • 13
  • 64
  • 78
user3529200
  • 11
  • 1
  • 2

1 Answers1

2

The class RzymArab is defined in the header (hpp) and in the body (cpp), this violates the one definition rule of C++ (ODR).

In the body, delete the class definition and define the members as following:

int RzymArab::rzym2arab(string rzym)
{
    ....
}

string RzymArab::arab2rzym(int arab) throw (LiczbaException)
{
    ....
}

Furthermore, defining RzymArab::liczby and RzymArab::arabliczby in the header also violates the ODR once the header is included more than once. So, move these definitions to the body too.

Finally, don't import namespaces in a header file (using namespace xxx). Generally, users of a header file expect their namespaces not to be polluted by including a header.

PS: You want to use a named constant instead of the magic number 13.

Exception specifications (throw (xx)) are an obsolete feature of C++. Since C++11 they have been superceded by noexcept specifiers. One reason that exception specifications have been made obsolete is that it is generally difficult to ensure that none but the specified exceptions are thrown. If the specification is violated, your program will by default terminate().

Community
  • 1
  • 1
Peter G.
  • 14,786
  • 7
  • 57
  • 75