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