Dear Friends:

I am struggling to overload the operators on this exercise. It was working fine when I just had the addition operator overloaded for the extRomanType but now I can't get them to work again. I have searched through the internet and I cannot figure out what I am doing wrong. Any assistance in this matter would be greatly appreciated.

Dani

/*

#include <iostream>

#include "Assignch2ex20.h"
#include <string>
#include <conio.h>

using namespace std;



int main()

    string rmn, decVal;
    int value,choice=0, deci;
    tryAgain:
    cout<< "Which would you like to convert?n";
    cout<< "Type 1 for Roman Numeral to Decimal.n";
    cout<< "Type 2 for Decimal to Roman Numeral.n";
    cout<< "Type 3 for manipulating 2 Roman Numeral's (+,-,*,/).n";
    cin>>choice;
    if (choice==1){
    cout << "Please enter the Roman Numeral to convert : ";
    cin >> rmn;
    RomanType myRomanType(rmn);
    value = myRomanType.romanToDecimal();

    cout << "The input Roman Numeral " << rmn << " is equal to the numerical " << value <<endl;
    }

    else if (choice==2){
            negDec:
            cout<< "Please enter a Decimal to convert less than 50000 : ";
    cin>>deci;
    if (deci<=0){cout<< "Please enter a number greater than 0.n";
    goto negDec;}
    if (deci>50000){cout<< "Please enter a number less than 50000.n";
    goto negDec;}
    RomanType myRomanType (deci);
    decVal = myRomanType.decimalToRoman();
    cout<<"The input decimal "<<deci<<" is equal to the Roman Numeral "<<decVal<<".n";
    }
    else if (choice==3){
            string rmn1, rmn2;
            int manip=0;
            negNum:
        cout<< "Please enter your first Roman Numeral: n ";
        cin>>rmn1;
        cout<< "Please enter your second Roman Numeral: n ";
        cin>>rmn2;

        RomanType rmncon1 (rmn1);
        RomanType rmncon2 (rmn2);

        cout<< "n Please enter your choice for manipulation.n";
        cout<< "1-Additionn 2-Subtractionn 3. Multiplicationn 4. Divisionn";
        cin>>manip;
        if (manip==1){

       extRomanType temp = rmncon1 + rmncon2;
        cout<<" Result of the addition is: "<<temp.decimalToRoman()<<"n";
        }
  /*     else if (manip==2){
           if (rmn1<rmn2){
                cout<<"The first number must be greater than the second to subtract. Please re-enter your numbers.n";
                goto negNum;
            }
            extRomanType temp = rmncon1 - rmncon2;
            cout<<" Result of the subtraction is: "<<temp.decimalToRoman()<<"n";
        }

        else if (manip==3){

       extRomanType temp = rmncon1 * rmncon2;
        cout<<" Result of the multiplication is: "<<temp.decimalToRoman()<<"n";
        }
        else if (manip==4){
            if (rmn1<rmn2){
                cout<<"The first number must be greater than the second to divide. Please re-enter your numbers.n";
                goto negNum;
            }
            extRomanType temp = rmncon1 / rmncon2;
            cout<<" Result of the division is: "<<temp.decimalToRoman()<<"n";
        }
            }
    else{
        cout<<"Incorrect choice please try again.n"<<endl;
        goto tryAgain;}*/

getch(); 


Here is my scecond program: 

#include <iostream>

#include <string>

using namespace std;

class RomanType
{
public:
string decCon;
int sum, total;

string get()
{
    return romanType;
}

void show()
{
    cout << "Roman Number is " << romanType << endl;
}
RomanType(const string &input)
{
    romanType = input;
    total = romanToDecimal();
}

RomanType(int &input)
{
    sum = input;
    decCon = decimalToRoman();
}
int romanToDecimal()
{
    int length = romanType.length();
    int previous = 0;
    bool error = false;
    int nIndex = 0;
    sum = 0;
    ;

    while( (error == false) && (nIndex < length) )
    {
        switch(romanType[nIndex])
        {
        case 'M':
            sum += 1000;
            if(previous < 1000)
            {
                sum -= 2 * previous;
            }
            previous = 1000;
            break;
        case 'D':
            sum += 500;
            if(previous < 500)
            {
                sum -= 2 * previous;
            }
            previous = 500;
            break;
        case 'C':
            sum += 100;
            if(previous < 100)
            {
                sum -= 2 * previous;
            }
            previous = 100;
            break;
        case 'L':
            sum += 50;
            if(previous < 50)
            {
                sum -= 2 * previous;
            }
            previous = 50;
            break;
        case 'X':
            sum += 10;
            if(previous < 10)
            {
                sum -= 2 * previous;
            }
            previous = 10;
            break;
        case 'V':
            sum += 5;
            if(previous < 5)
            {
                sum -= 2 * previous;
            }
            previous = 5;
            break;
        case 'I':
            sum += 1;
            if(previous < 1)
            {
                sum -= 2 * previous;
            }
            previous = 1;
            break;
        default:
            cout << romanType[nIndex] << " is not a Roman Numeral!" << endl;
            error = true;
            sum = 0;
        }

        nIndex++;

    }
    return sum;
}

int length()
{
    return romanType.length();
}
string decimalToRoman()
{
    int i=0,decimal=sum;
    while (decimal>=1000){decCon += "M",decimal-=1000,i++;}
    while (decimal>=500){decCon+="D",decimal-=500,i++;}
    while (decimal>=100){decCon+="C",decimal-=100,i++;}
    while (decimal>=50){decCon+="L",decimal-=50,i++;}
    while (decimal>=10){decCon+="X",decimal-=10,i++;}
    while (decimal==9){decCon+="I",i++,decCon+="X",decimal-=9,i++;}
    while (decimal>=5){decCon+="V",decimal-=5,i++;}
    while (decimal==4){decCon+="I",i++,decCon+="V",decimal-=4,i++;}
    while (decimal>=1){decCon+="I",decimal-=1,i++;}
    return decCon;
}; 

protected:
string romanType;
int romanType2;

};

class extRomanType :public RomanType{
public:
extRomanType operator+ (RomanType param){
RomanType t("X");
t.total = total + param.total;
return t;
}

Edited 3 Years Ago by daniel1977

What do you mean by "can't get them to work"? Please provide specific examples of what you expect to see and what is actually happening. It makes it much easier to help you find what's wrong.

The problem I am having starts on line 136 of the header file and 64 of the main file. I am attempting to overload the + operator using the extRomanType class which is derived from RomanType. But when I try compiling I get an error on line 141 stating "base `RomanType' with only non-default constructor in class without a constructor". I am passing 2 strings into the extRomanType class and the RomanType has a constructor for this but I cannot figure out why the extRomanType does not seem to be inheriting this. If it worked properly then the main function would pass the rmncon1 and rmncon2 to the + operator in the extRomanType where it would add the totals from the RomanType romantoDecimal and return that back to the main. I have the remainder of the code written for this assignment once I have this issue fixed. Then it would convert that total back to roman and print the output. The frustrating part is that when I wrote just the operator+ portion it actually compiled and worked right but after I added in the other operators it stopped working and even with the others commented out it doesn't work now.

It is hard to know precisely what is wrong because you give no context but here are a few possibilities (with reference to the line numbers in the code you posted).

  1. Line 235 you create a variable of RomanType, you try to return this from operator+ but the defined return type is extRomanType and there is no way to convert between the 2.
  2. If you try to change RomanType at line 235 to extRomanType you get an error because extRomanType has no constructor that takes a string. In fact there is a complete mismatch in constructors between RomanType and extRomanType. The first has constructors taking string and int but no default constructor while the second has a default constructor but no other type of constructor.
  3. The default constructor of extRomanType calls the, non-existant, default constructor of RomanType. From the sketchy information you give this looks like the likely cause of your error.

Banfa:

I know it is better to code each class in their own file and build the functions in a separate file. How is this more beneficial than coding them together in a single file? It seems more intuitive and easier to troubleshoot if everything is in front of you in one file rather than having to search through multiple different ones to try and find a syntax error. I have a textbook that builds both the classes and the functions in the same file and always uses a header file.

Dani

This article has been dead for over six months. Start a new discussion instead.