School Project; Compiling Errors?

Hello everyone, I am back with another school project! This project wants us to create a class that will take a string of roman numerals and convert them to positive integers and then print either the roman numeral or positive integer depending on users preference.

My teacher uploaded a header file and implementation file, for us to reference:
(Implementation Reference File)

// Programming Exercise 10-6
// Roman Number Implementation file
// Lambert



#include <iostream>

#include <string>

#include "roman.h"



using namespace std;



void romanType::printPositiveInteger() const

{
  cout << num;

}



void romanType::printRoman() const

{

  cout << romanNum;

}



void romanType::setRoman(string rString)

{

    romanNum = rString;

    romanToPositiveInteger();

}



void romanType::romanToPositiveInteger()

{
  int sum = 0;
  int length = romanNum.length();
  int i;

  int previous = 1000;


  for (i = 0; i < length; i++)

  {
    switch (romanNum[i])

    {

      case 'M': sum += 1000;

        if (previous < 1000)

          sum -=  2 * previous;

        previous = 1000;

        break;

      case 'D': sum += 500;
        if (previous < 500)
 
          sum -= 2 * previous;

        previous = 500;

        break;

      case 'C': 
sum += 100;

        if (previous < 100)

          sum -= 2 * previous;

        previous = 100;

        break;

      case 'L': sum += 50;
         if (previous < 50)
           sum -= 2 * previous;
         previous = 50;
         break;
      case 'X': sum += 10;

        if (previous < 10)
          sum -= 2 * previous;

        previous = 10;
        break;
      case 'V': sum += 5;

        if (previous < 5)
          sum -= 2 * previous;
        previous = 5;
        break;

      case 'I': sum += 1;
        previous = 1;

     }
 // End switch
  }
 // End for
  
  num = sum;

} // End romanType::romanToPositiveInteger



romanType::romanType()

{
  romanNum = 'I';

  num = 1;

}



romanType::romanType(string rString)

{

  romanNum = rString;

  romanToPositiveInteger();

}

(Header Reference File)

// Programming Exercise 10-6
// Roman Num Header file



#include <string>


using namespace std;



class romanType

{

  public:

    void setRoman(string);
    void romanToPositiveInteger();

    void printPositiveInteger() const;
    void printRoman() const;

    romanType();

    romanType(string);



  private:
    string romanNum;
    int num;

};

(concerning the above code: I understand some but not all of it, if anyone is up for clarifying the purpose of romanType::romanType(); and romanType::romanType(string); it would be much appreciated!)

Following are my Main File, Header File, and Implementation File:
(Main File)

#include <iostream>
#include "romanImp.cpp"


using namespace std;

int main() {
    // Write your main here
    string choice;
    void storeRoman(string roman);
    
    void convertRoman();
    
    cout << "Enter 'roman' to print the roman numeral,\n"
         << "or enter 'arabic' to print the arabic integer: ";
    cin >> choice;
    cout << endl;
    
    if (choice == "roman")
        void printRoman();
    else if (choice == "arabic")
        void printArabic();
    return 0;
}

(Header File)


using namespace std;

class romanType
{
    public:
    void storeRoman(string);
   //Function to store the given Roman numeral
   
    void convertRoman();
    //Function to convert & store Roman numeral as a positive
    //integer
    
    void printRoman();
    //Function to print the number as a Roman numeral
    
    void printArabic();
    //Function to print the number as a positive integer
    
    private:
    string roman;
    //variable to store the roman numeral
    int arabic;
    //variable to store the arabic integer
};

(Implementation File)

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

using namespace std;

void romanType::storeRoman(string roman)
{
    cout << "Enter a roman numeral: ";
    cin >> roman;
    cout << endl;
};

void romanType::convertRoman()
{
    int sum = 0;
    //variable to add up converted arabic integer
    int length = roman.length();
    //string operation to find length of roman numeral
    int i;
    //variable to increment in future for function
    
    int former = 1000;
    //if a former digit is less than a following digit
    //it is meant to signal subtraction (i.e. IV is one less
    //than five); this will help subtract
    
    for (i = 0; i < length; i++) //for function to work through ea. digit of the roman numeral
    {
        switch (roman[i]) //switch using array subscript operator to determine value of ea. position in string(roman numeral)
        {
            case 'M': sum = sum + 1000;
                if (former < 1000) // if function using former variable to determine if there is subtraction
                    sum = sum - (2 * former);
                former = 1000;
                break;
            case 'D': sum = sum + 500;
                if (former < 500) // if function using former variable to determine if there is subtraction
                    sum = sum - (2 * former);
                former = 500;
                break;
            case 'C': sum = sum + 100;
                if (former < 100) // if function using former variable to determine if there is subtraction
                    sum = sum - (2 * former);
                former = 100;
                break;
            case 'L': sum = sum + 50;
                if (former < 50) 
                    sum = sum - (2 * former);
                former = 50;
                break;
            case 'X': sum = sum + 10;
                if (former < 10) 
                    sum = sum - (2 * former);
                former = 10;
                break;
            case 'V': sum = sum + 5;
                if (former < 5) 
                    sum = sum - (2 * former);
                former = 5;
                break;
            case 'I': sum = sum + 1;
                if (former < 1) 
                    sum = sum - (2 * former);
                former = 1;
                break;
        }//end switch
    }//end for
    int arabic = sum;
};//end romanType::convertRoman

void romanType::printRoman() //function to print the roman numeral
{
    cout << roman;
};

void romanType::printArabic() //function to print the arabic integer
{
    cout << arabic;
};

And finally the compiler errors:

In file included from main.cpp:2:0:
romanImp.cpp: In member function 'void romanType::convertRoman()':
romanImp.cpp:70:9: warning: unused variable 'arabic' [-Wunused-variable]
            int arabic = sum;
                  ^~~~~
romanImp.cpp: In member function 'void romanType::convertRoman()':
romanImp.cpp:70:9: warning: unused variable 'arabic' [-Wunused-variable]
            int arabic = sum;
                  ^~~~~
/tmp/ccTE90Cu.o: In function 'romanType::storeRoman(std::_cxx11::basic_string<char, std::char-traits<char>, std::allocator<char> >)':
romanImp.cpp:(.text+0x0): multiple definition of 'romanType::storeRoman(std::_cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccF9f6IS.o:main.cpp:(.text+0x0): first defined here
/tmp/ccTE90Cu.o: In function 'romanType::convertRoman()':
romanImp.cpp:(.text+0x50): multiple definition of  'romanType::convertRoman()'
/tmp/ccF9f6IS.o:main.cpp:(.text+0x50): first defined here
/tmp/ccTE90Cu.o: In function 'romanType::printRoman()':
romanImp.cpp:(.text+0x1b6): multiple definition of 'romanType::printRoman()'
/tmp/ccF9f6IS.o:main.cpp:(.text+0x1b6): first defined here
/tmp/ccTE90Cu.o: In function 'romanType::printArabic()':
romanImp.cpp:(.text+0x1d8): multiple definition of 'romanType::printArabic()'
/tmp/ccF9f6IS.o:main.cpp:(.text+0x1d8): first defined here
collect2: error: ld returned 1 exit status
/bin/bash: line 4: ./a.out: No such file or directory

The compiler errors were hand typed because I can’t copy & paste, sorry if there are any errors. I don’t know if it was necessary but I went ahead and formatted it as code? Thank you for hearing me out! I’m about to go to work soon, but I will check for any replies when I get back!

before you look at purpose, what at all are they?
are they loops?
are they… numbers? modules…

what information do they process, what instructions do they carry out, when do they get used? that might say a lot about what their purpose is

your error messages are quite plain. read them. and don’t confuse the warnings for errors, they’re not. it says they’re not. but you should fix them too. something about multiple definitions. should that something be defined multiple times? you could look at where they’re mentioned in the code, maybe you’re repeating something. maybe you’re telling your compiler to compile the same thing several times, that would mean that there are duplicates

I pasted your files here.
https://repl.it/repls/LemonchiffonPrimeDrawing
It results in some error messages. They don’t match yours. Many of them say a lot about what needs to change.

1 Like