Member Avatar for Griff0527

I am getting an error during the build stating: "Illegal call of non-static member function" and I cannot figure out 1) what does this mean, or 2) how to resolve it. For detailed clarification, I will type out what the assignment was (I say was because the assignment was due over a week as a part of 6 assignments).

1)Use a class "romanTpye" to implement Roman numerals in a program.
2)Use a function "romanToDecimal" to convert Roman numerals into its equivalent decimal number.
3)Modify romanType so its data members are declared as protected. us the class sting to manipulate the strings. Furthermore, overload the stream insertion and stream extraction operators for easy input. I don't understand this part
4)Include member function "decimalToRoman" to convert decimal to Roman (must be positive integer)

I have not gotten to the point of overloading the << or >> operators, but from what I see this should compile correctly. Can someone have a look at the below code (header, implementation, and main files) and help me figure out the "Illegal call of non-static member function" error?

I will probably keep this thread open to get help with overloading the << and >> operators as I do not fully understand the concept yet, but am continuing to read, study, and research.


header file

#ifndef ROMAN_CONVERSION_H
#define ROMAN_CONVERSION_H

#include <iostream>
#include <string>

using namespace std;

class romanType
{
public:
	void romanToDecimal();
	//Function to convert roman numbers to decimal
	//Postcondition: outputs decimal equivilents of the Roman numeral input to the screen

	void decimalToRoman();
	//Function to convert decimal numbers to Roman numerals
	//Postcondition: outputs Roman numeral equivilents of the decimal input to the screen

protected:

};

#endif

implementation file

#include <iostream>
#include <string>
#include "romanType.h"

using namespace std;

void romanType::romanToDecimal()
{
    char roman_num[10];
    cout << "Please enter a Roman Numeral: "; //Prompt user
    cin >> roman_num;  //Receive input from user
    int len = strlen(roman_num);
    int number = 0;
    int counter = 0;
    int beforeSum = 0;
    int sum = 0;
     
    for (counter = len; counter >= 0; counter--)
	{
		if (roman_num[counter] == 'M' || roman_num[counter] == 'm')
		number = 1000;
		else if (roman_num[counter] == 'D' || roman_num[counter] == 'd')
		number = 500;
		else if (roman_num[counter] == 'C' || roman_num[counter] == 'c')
		number = 100;
		else if (roman_num[counter] == 'L' || roman_num[counter] == 'l')
		number = 50;
		else if (roman_num[counter] == 'X' || roman_num[counter] == 'x')
		number = 10;
		else if (roman_num[counter] == 'V' || roman_num[counter] == 'v')
		number = 5;
		else if (roman_num[counter] == 'I' || roman_num[counter] == 'i')
		number = 1;
		else
		number = 0;
     
		if (beforeSum > number)
		sum = beforeSum - number;
		else
		sum = sum + number;
		beforeSum = number;
	}
     
    cout << "The result is: " ;
    cout << sum << endl;
}

void romanType::decimalToRoman()
{
	double num;
	int intnum, m, d, c, l, x, v, i, n;
 
 
	cout << "Enter a number:";
	cin >> num;
	intnum = (int) num;
 
	m = intnum / 1000;
	d = ((intnum%1000)/500);
	c = ((intnum%500)/100);
	l = ((intnum%100)/50);
	x = ((intnum%50)/10);
	v = ((intnum%10)/5);
	i = (intnum%5);
	n = m+d+c+l+x+v+i;
 
 
	while (n > 0)
	{
		if (m > 0)
		{
			cout << "M";
			m--;
			n--;
		}
 
		else if (d > 0)
		{
			cout << "D";
			d--;
			n--;
		}
		else if (c > 0)
		{
			cout << "C";
			c--;
			n--;
		}
		else if (l > 0)
		{
			cout << "L";
			l--;
			n--;
		}
		else if (x > 0)
		{
			cout << "X";
			x--;
			n--;
		}
		else if (v > 0)
		{
			cout << "V";
			v--;
			n--;
		}
		else if (i > 0)
		{
			cout << "I";
			i--;
			n--;
		}
 
	}

	cout << endl;
 
 }

main file

#include <iostream>
#include <cstring>
#include "romanType.h"
     
using namespace std;
     
int main(int argc, char *argv[])
{
	romanType::romanToDecimal();
	romanType::decimalToRoman();
    system("PAUSE");
    return (0);
}

The Compiler is expecting you to use a Class object with those functions unless you declare them as static.
In your header, put static in front of the declarations (i.e. right before the word void), but not in the implementation.

Typically (though not always, I suppose) you would declare a variable of type romanType in main and call the member functions you want use on that variable:

main()
{
  romanType myRT;
  myRT.romanToDecimal();
  myRT.decimalToRoman();
}
Member Avatar for Griff0527

Thanks, that worked DeanMSands3.

It appears that my logic for Roman to decimal is skewed. MMCI comes out with 100 instead of 2101. Likewise MCMXI comes out to 1900 instead of 1911.

I'm looking at my logic now, but a second (or third) set of eyes would be much appreciated.

Member Avatar for Griff0527

Lerner, your way worked as well. Thanks guys. Now, I'm working the logic issue. My if-else statements aren't working as expected.
Specifically:

void romanType::romanToDecimal()
{
    char roman_num[10];
    cout << "Please enter a Roman Numeral: "; //Prompt user
    cin >> roman_num;  //Receive input from user
    int len = strlen(roman_num);
    int number = 0;
    int counter = 0;
    int beforeSum = 0;
    int sum = 0;
     
    for (counter = len; counter >= 0; counter--)
	{
		if (roman_num[counter] == 'M' || roman_num[counter] == 'm')
		number = 1000;
		else if (roman_num[counter] == 'D' || roman_num[counter] == 'd')
		number = 500;
		else if (roman_num[counter] == 'C' || roman_num[counter] == 'c')
		number = 100;
		else if (roman_num[counter] == 'L' || roman_num[counter] == 'l')
		number = 50;
		else if (roman_num[counter] == 'X' || roman_num[counter] == 'x')
		number = 10;
		else if (roman_num[counter] == 'V' || roman_num[counter] == 'v')
		number = 5;
		else if (roman_num[counter] == 'I' || roman_num[counter] == 'i')
		number = 1;
		else
		number = 0;
     
		if (beforeSum > number)
		sum = beforeSum - number;
		else
		sum = sum + number;
		beforeSum = number;
	}
     
    cout << "The result is: " ;
    cout << sum << endl;
}

To me roman numerals are read left to right so:

MCMXLIV = 1000 + (1000 − 100) + (50 − 10) + (5 − 1) = 1944

which is an example I found on Wikipedia.

Your problem is at line 31. Which order should number and beforeSum be, in order to perform the subtraction? Also, you have a problem with a number like "XIV", since when you determine you have "I" before "V", you throw out the previous sum -- which was probably "XI", so make extra sure you do everything you need to!

Oh, and at some point you should add some error checking, because even if you get your existing logic correct, "IC" isn't a valid roman numeral.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.