-1

This is my first time using classes and I'm writing a simple program that will take a certain roman numeral and print out the numerical equivalent. However, I'm struggling to code my user input, my cin >> romanObject.getRomanNumeral(); isn't working. I get error E0349 saying no operator ">>" matches these operands. However, when I used just a simple char variable declared in my main function instead it worked fine. Why doesn't my variable in my class work?

#include <iostream>
#include <string>

using namespace std;

class romanType {

public:

    void setRomanNumeral(string x) {
        romanNumeral = x;
    }
    string getRomanNumeral() {
        return romanNumeral;
    }
private:
    string romanNumeral;

};

void store();

int main()
{
    store();
    return 0;
}   

void store() // Takes user input.
{
    romanType romanObject;
    romanObject.setRomanNumeral("Blank");

    cout << "Enter one of the following roman numerals" << endl;
    cout << "I, V, X, L, C, D, M." << endl;
    cin >> romanObject.getRomanNumeral();

}
  • 1
    `string getRomanNumeral()` will return a brand new `string` that is a copy of `romanNumeral` and has a lifespan of darn near zero. You can't read into it. Instead, return a reference so you don't get a copy, you get `romanNumeral`. `string& getRomanNumeral() ` – user4581301 Feb 10 '21 at 01:55
  • 1
    @user4581301 Isn't that what the setter function was intended to do? – πάντα ῥεῖ Feb 10 '21 at 01:59
  • Darn good point. @πάνταῥεῖ . – user4581301 Feb 10 '21 at 01:59
  • Handy reading: [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues). Of immediate interest is the notion of a prvalue, the most famous of which are variables returned by value and the `this` pointer. – user4581301 Feb 10 '21 at 02:02

1 Answers1

1

Why doesn't my variable in my class work?

It works just fine, except that you're not returning it. When you are returning something by value, you are essentially making a temporary copy of the variable. Of course, you can return it by reference:

string &getRomanNumeral() {
    return romanNumeral;
}

However, that doesn't really make much sense, since you no longer need setRomanNumberal(), you can simply use getRomanNumberal():

romanObject.getRomanNumeral()="hello world";

The detail that you missed that setting the value of your class member is a part of initializing it by reading its value from std::cin. After you read the string, what do you need to do? You need to set it, so why do you want to use something called getRomanNumeral()? That doesn't make much sense, doesn't that?

So, really, your original approach was the correct one, something along the lines of:

string s;

cin >> s;

romanObject.setRomanNumeral(s);

You're getting the variable, from cin, and you're setting it in your object; you're not getting the existing value of this variable in your object, at any point.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148