944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3080
  • C++ RSS
May 22nd, 2006
0

C++ Questions

Expand Post »
I am trying to write a program to convert a roman numeral to decimal. If I define a variable with: char roman_num[15]; can I then select individual characters in that roman numeral with: roman=roman_num[i]; where "i" is a number value of the character I want to retrieve from 0-14?

Also, I am getting an error when I try to compile my implementation file (my header file compiles fine). I can't locate my mistake...

Here are my compiler errors:
romanTypeImp.cpp: In function `int main()':
romanTypeImp.cpp:15: error: `romanToDecimal' undeclared (first use this function)
romanTypeImp.cpp:15: error: (Each undeclared identifier is reported only once for each function it appears in.)


I am using cygwin on XP and gcc (compiling with g++):
gcc version 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125)

Here is my header file:
C++ Syntax (Toggle Plain Text)
  1. class romanType
  2. {
  3. public:
  4. romanType();
  5. // Default constructor
  6. void romanToDecimal(char roman);
  7. // Convert from roman numeral to decimal
  8. void printRoman();
  9. // Print roman numeral
  10. void printDecimal();
  11. // Print decimal value
  12. private:
  13. char roman; // Store a single roman numeral
  14. int decimal; // Store decimal value
  15. char roman_num[15]; // Store roman numeral array of 15 characters
  16. int numberM; // Store value of roman numeral M
  17. int numberD; // Store value of roman numeral D
  18. int numberC; // Store value of roman numeral C
  19. int numberL; // Store value of roman numeral L
  20. int numberX; // Store value of roman numeral X
  21. int numberV; // Store value of roman numeral V
  22. int numberI; // Store value of roman numeral I
  23. };

Here is my implementation file:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include "romanType.h"
  3. using namespace std;
  4. char roman;
  5. void romanType::romanToDecimal(char roman)
  6. {
  7. cout<<"Testing"<<roman;
  8. }
  9. int main()
  10. {
  11. cout<<endl<<"Enter a Roman Numeral: ";
  12. romanToDecimal('M');
  13. }
Similar Threads
Reputation Points: 13
Solved Threads: 0
Newbie Poster
MrLew is offline Offline
9 posts
since May 2006
May 22nd, 2006
0

Re: C++ Questions

romanToDecimal is a member function lacking an object.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 23rd, 2006
0

Re: C++ Questions

I'm not grasping what you mean by an object... I tried looking it up, but I don't think I found the right solution. I made the two changes below, but still get the following error when I compile:

undefined reference to `romanType::romanType()'

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include "romanType.h"
  3.  
  4. using namespace std;
  5. char roman;
  6. romanType myroman; //Change #1
  7.  
  8. void romanType::romanToDecimal(char roman)
  9. {
  10. cout<<"Testing"<<roman;
  11. }
  12.  
  13. int main()
  14. {
  15. cout<<endl<<"Enter a Roman Numeral: ";
  16. myRoman. romanToDecimal('M'); //Change #2
  17. }
Reputation Points: 13
Solved Threads: 0
Newbie Poster
MrLew is offline Offline
9 posts
since May 2006
May 23rd, 2006
0

Re: C++ Questions

You tell the compiler that you'll write a function:
class romanType
{
public:
 romanType();
Then you don't provide one. The compiler feels left out.

Note also that the language is case-sensitive, so myroman is not the same as myRoman.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 23rd, 2006
0

Re: C++ Questions

Thanks for the pointers... I fixed the additional problems based off of your last tip. I've got a new error now...

romanTypeImp.cpp:22: error: return type specification for constructor invalid

Header file:
C++ Syntax (Toggle Plain Text)
  1. class romanType
  2. {
  3. public:
  4. romanType();
  5. // Default constructor
  6. void romanToDecimal(char roman);
  7. // Convert from roman numeral to decimal
  8. void printRoman();
  9. // Print roman numeral
  10. void printDecimal();
  11. // Print decimal value
  12. private:
  13. char roman; // Store a single roman numeral
  14. int decimal; // Store decimal value
  15. char roman_num[15]; // Store roman numeral array of 15 characters
  16. int numberM; // Store value of roman numeral M
  17. int numberD; // Store value of roman numeral D
  18. int numberC; // Store value of roman numeral C
  19. int numberL; // Store value of roman numeral L
  20. int numberX; // Store value of roman numeral X
  21. int numberV; // Store value of roman numeral V
  22. int numberI; // Store value of roman numeral I
  23. };

Implementation file:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include "romanType.h"
  3. using namespace std;
  4. void romanType::romanToDecimal(char roman)
  5. {
  6. cout<<"Testing"<<roman;
  7. }
  8. void romanType::printRoman()
  9. {
  10. cout<<"Testing 2";
  11. }
  12. void romanType::printDecimal()
  13. {
  14. cout<<"Testing 3";
  15. }
  16. void romanType::romanType()
  17. {
  18. printDecimal();
  19. }
  20. int main()
  21. {
  22. romanType myRoman;
  23. cout<<endl<<"Enter a Roman Numeral: ";
  24. myRoman.romanToDecimal('M');
  25. }
Reputation Points: 13
Solved Threads: 0
Newbie Poster
MrLew is offline Offline
9 posts
since May 2006
May 23rd, 2006
0

Re: C++ Questions

void romanType::romanType()
Constructors don't have return values.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Console Color
Next Thread in C++ Forum Timeline: Quick question about switch()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC