0

I am new to C++ and am currently working on a project that wants me to turn a Roman Numeral into a Hindu-Arabic number (our normal number system). I'm currently writing a class RomanNumeralType which needs to store a roman numeral type string. When I try to run the code below I get error code C3646: unknown override specifier and also error codes C4430 and C2061 which both address string (in bold) not being a valid identifier. It was my impression that #include fixed this however I am unsure why it is not working. I currently do not have a function that actually converts the numeral to a number but I want to fix this problem first. How do I get the string data type to work in my class?

Here is a minimally reproduced example of my code

//main
#include <iostream>
#include <iomanip>
#include "Header2.h"
#include <string>
using namespace std;

int main() {

    string ans;

    cout << "Please enter a string of Roman Numerals (M,D,C,L,X,V,I): " << endl; 
    cin >> ans;
    }

    RomanNumeralType r1(ans);

    cout << "Your original Roman Numeral was: " << r1.getNumeral() << endl;

    cout << "Your Roman Numeral as a integer is: " << r1.getNumber(r1.getNumeral()) << endl;

    return 0;
}

//Header2.h

#ifndef HEADER2
#define HEADER2
#include <string>

class RomanNumeralType {
    public:

        **string** romanNumeral;

        RomanNumeralType(**string** x)

        {romanNumeral = x;}

        **string** getNumeral()

        {return romanNumeral;}

}
  • 1
    Within the header, give `string` its fully qualified (unambiguous) name i.e. `std::string`. If you also do that in `main()`, the using directive (`using namespace std`) is not needed - and quite a few people prefer that. – Peter Jul 24 '21 at 03:24
  • what does #include do? – Eric Roach Jul 24 '21 at 03:31
  • `#include ` will mean that your translation unit sees declarations for `std::string` and some related functionality. It doesn't mean that it will automatically convert the unqualified name `string` to the qualified name `std::string`. As [this question](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) gets into, many programmers prefer being explicit about which identifiers come from which namespaces, especially in the case of the C++ standard library. – Nathan Pierson Jul 24 '21 at 03:50

1 Answers1

-1

When you do #include <string> think of it as though you are pasting in the contents of a file shared across all developers that has all the definitions for string. That class lives within the std namespace, so when you want to use a string, you have to tell the compiler that string is in std. The preferred method is to use the fully qualified name, so every time you use string, you would replace it with std::string.

The other option is to do what you did in main.cpp, using namespace std;This tells the compiler that the namespace to look in is in std. This is generally considered bad practice for actual programs, but it is OK if it is a small, one off program like you seem to be writing.

If you do the first option, you will have to change everything in the std namespace such as cout, but it is clearer that you didn't write your own.

Hawkeye5450
  • 672
  • 6
  • 18
  • A using directive like `using namespace std` in a header file is considered VERY bad practice in C++. If you do a search here (or online more generally) it is pretty easy to find explanations about why it is bad practice. Doing that in a single compilation unit (like main.cpp, in this example) is not as bad a practice, but it is not exactly good practice either. – Peter Jul 24 '21 at 04:20