-3

I'm trying to make this program set my name and print it. I get error on this line.

 cin >> FirstName.setName();

I have also tried setting up a variable in main but it doesn't print it, it just takes my my input I think?

 string x;
 cin >> x;

Whole code below.

#include <iostream>
#include <string>
using namespace std;

class FirstClass{
public:
    void setName(string x){
        name = x;
    }

    string getName(){
        return name;
    }
private:
    string name;
};

int main()
{
string x;
FirstClass FirstName;
cin >> FirstName.setName();
cout << FirstName.getName() << endl;

return 0; 
} 
Dave Martinez
  • 122
  • 2
  • 8
  • 1
    Your previous question already has the answer to this. And no, reading from cin doesn't write to cout. – Mat May 03 '14 at 11:46
  • 1
    As mentioned before `cin >> FirstName.setName();` doesn't work. Go back to your [other question](http://stackoverflow.com/questions/23443458/cin-command-to-use-for-inserting-value-on-a-function) and rethink what to do! – πάντα ῥεῖ May 03 '14 at 11:46
  • Got it. Sorry, I just thought that using cin for variable, objects, and classes are different. I figured it out. I looked back at my previous question. Thanks. @πάνταῥεῖ – Dave Martinez May 03 '14 at 11:53
  • Also as mentioned before, what you are actually using here is [`std::istream& ::operator>>(std::istream&, const std::string&)`](http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt2), not `std::cin`! – πάντα ῥεῖ May 03 '14 at 11:57
  • Sorry, but I can't seem to understand these complexity yet. Can you please elaborate it more for as I am just starting out. My third day.@πάνταῥεῖ – Dave Martinez May 03 '14 at 12:00
  • @Rakken Read the linked reference and the study the examples there. – πάντα ῥεῖ May 03 '14 at 12:06

2 Answers2

1

You cannot do this:

cin >> FirstName.setName();

Instead try:

string s;
cin >> s;
FirstName.setName( s );
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
0

that's not how you use setName().

you need to read from cin to x just the way you said and then call setName(x)

Pavel
  • 7,436
  • 2
  • 29
  • 42
  • I am not sure how to use it properly yet, I am just following [NewBoston's C++ tutorial](http://youtu.be/jTS7JTud1qQ), lesson 13 to be exact. – Dave Martinez May 03 '14 at 11:55
  • Then skip to 6:40, you will see how he is calling setName with a string as argument. – Pavel May 03 '14 at 12:24