Hello. Im trying to make a program that converts roman numerals to arabic numerals. I keep getting a error saying 'unresolved external', and I do not know why this is.
I'm guessing because you never made a constructor in your class. Also what are you trying to do on line 19 call a function or name an object of your class?
To name an object try this instead romanType roman; then on line 25 it would be
Okay, so I would call the function by having line 25 as roman.setRoman(romanString);, and having line 29 as void romanType::setRoman(string romanString)?
@giblit: yes, I was naming an object.
@andy: I am referring to the void romanType::setRoman(string romanString) function.
Okay, so I would call the function by having line 25 as roman.setRoman(romanString);, and having line 29 as void romanType::setRoman(string romanString)?
Yeah that is how you would do it.
By the way have you considered using a switch instead of all the if else?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
siwtch( romanString[i] )
{
case'M':
sum += 1000; //you += means the old value + something or sum = sum + 1000
if( previous < 1000 )
{
sum -= ( 2 * previous ); //same as before but - instead of +
}
previous = 1000;
break;
case'D':
//stuff
break;
/* ^^ do that for all of them instead of all the else if's ^^*/
}
What exactly does it try and I suppose I can try and compile it myself lol
Oh duh lol you are declaring the class functions after the main. move the main function under the class function declarations.
oh yeah sorry the definitions of the functions
I'm pretty sure what the op is trying to do is something like this
1 2 3 4 5 6 7 8 9
int main( int argc , char **argv)
{
function();
}
void function( void )
{
std::cout << "I am not going to be called ever!" << std::endl;
}
unless classes act as a prototype I normally just put it before the main.
Actually forget that...the class acts as the prototype I dont know what I was thinking.
I think the error is just because you didn't name the object correctly earlier.
Also on line 34 .length is a function so you need to put .length();
Line 42 missing a semicolon
line 52 missing a semicolon
line 63 missing a semicolon
line 72 missing a semicolon
line 82 missing a semicolon
line 92 missing a semicolon
@Daleth: Yes, I did.
@giblit: Yes i have thought about using 'switch', but I'd rather do it as I am doing it now to avoid more confusion. And it doesn't try anything, I just get the 'unresolved external' error.
IMO, you shouldn't have the setRoman function. Instead provide a constructor that takes a string & uses an initiliser list to set the romanNum variable.
You have a romanToDecimal function but it isn't defined or used anywhere.
Your setRoman function is of type void & doesn't set any member variables, so the local sum variable can't be used by anything. So that code should be in the romanToDecimal function and sum should be a member variable.
As giblit said use a switch, but also make sure it has a default clause to catch any bad input.
Also avoid having global variables like you do on line 17 - at least put it in main() instead.
This code sum = sum + 5; can be better written as sum += 5; if the value is 1 then sum++;
Try to avoid having 'magic numbers' in your code - numbers like 1000, 500 etc should be declared & initialised as const int at the beginning of main(). Then you can use that variable throughout your code.