First off, I am a beginner at C++ coding, so if possible try to explain things in the simplest possible terms so that I can make heads or tails of it.
I have done a lot of research, but most of the examples just don't make any sense to me. I have tried implementing various solutions, but I either get the same bug or make things worse.
The program is supposed to be able to take in a string of Roman numerals, and convert them to an integer value. Since this is a homework assignment, I must all of this within a Roman_int class and perform the operation through member functions within the class.
The program I have created contains a member function called as_int() which does the work of converts a string of roman numeral into a numeric integer value. As far as I can tell, that part works fine.
Another thing to note, I am using a special header provided by my university, which handles things like using the std library, so things might look a bit weird.
Here's what I have:
#include "std_lib_facilities_4.h"
/*
M=1000
D=500
C=100
L=50
X=10
V=5
I=1
*/
class Roman_int
{
string r;
public:
int as_int()
{
int val;
for(int i=r.size()-1; i>0; --i)
{
if(i==r.size()-1)
{
switch(r[i])
{
case 'M':
val+=1000;
break;
case 'D':
val+=500;
break;
case 'C':
val+=100;
break;
case 'L':
val+=50;
break;
case 'X':
val+=10;
break;
case 'V':
val+=5;
break;
case 'I':
val+=1;
break;
default:
error("invalid input");
break;
}
}
else
{
switch(r[i])
{
case 'M':
val+=1000;
break;
case 'D':
if(r[i+1]== 'M')
val-=500;
else
val+=500;
break;
case 'C':
if(r[i+1]== 'M' || r[i+1]== 'D')
val-=100;
else
val+=100;
break;
case 'L':
if(r[i+1]== 'M' || r[i+1]== 'D' || r[i+1]== 'C')
val-=50;
else
val+=50;
break;
case 'X':
if(r[i+1]== 'M' || r[i+1]== 'D' || r[i+1]== 'C' || r[i+1]== 'L')
val-=10;
else
val+=10;
break;
case 'V':
if(r[i+1]== 'M' || r[i+1]== 'D' || r[i+1]== 'C' || r[i+1]== 'L' || r[i+1]== 'X')
val-=5;
else
val+=5;
break;
case 'I':
if(r[i+1]== 'I')
val+=1;
else
val-=1;
break;
default:
error("invalid input");
break;
}
}
}
return val;
}
friend istream& operator>>(istream& is, Roman_int& roman)
{
string rom_num;
is >> rom_num;
if(!is) return is;
roman = Roman_int(rom_num);
return is;
}
friend ostream& operator<<(ostream& os, Roman_int& roman)
{
cout << roman.r << endl;
return os;
}
};
int main()
{
Roman_int r;
cout << "Enter a Roman numeral in all capital letters: " << endl;
cin >> r;
cout << "Roman " << r << " equals " << r.as_int() << endl;
}
The problem I am having is with declaring my Roman_int object in my main function. I get this error:
hw6_pr3.cpp: In function ‘std::istream& operator>>(std::istream&, Roman_int&)’:
hw6_pr3.cpp:114:28: error: no matching function for call to ‘Roman_int::Roman_int(String&)’
roman = Roman_int(rom_num);
^
hw6_pr3.cpp:114:28: note: candidates are:
hw6_pr3.cpp:17:7: note: Roman_int::Roman_int()
class Roman_int
^
hw6_pr3.cpp:17:7: note: candidate expects 0 arguments, 1 provided
hw6_pr3.cpp:17:7: note: Roman_int::Roman_int(const Roman_int&)
hw6_pr3.cpp:17:7: note: no known conversion for argument 1 from ‘String’ to ‘const Roman_int&’
hw6_pr3.cpp:17:7: note: Roman_int::Roman_int(Roman_int&&)
hw6_pr3.cpp:17:7: note: no known conversion for argument 1 from ‘String’ to ‘Roman_int&&’
From what I have read, I'm pretty sure it has something to do with either the default constructor or how my class is being initialized, but I have no idea where I am supposed to go from there, as I have never encountered this sort of problem with any of the classes I have created. If anyone could explain this in simple terms, that would extremely helpful.
Aucun commentaire:
Enregistrer un commentaire