Little help with my first steps (text input and public/private)

Being as straightforward as i can: i was messing around with input codes, trying to get myself used with C++ and i came to realize that this code works fine so far:
1
2
3
4
5
6
7
8
class QA{
	std::string userQuestion()
	{
		std::string userInput;
    	        std::cin >> userInput;
    	        return userInput;
	}
};


But if i try to simply declare my function public or private, like this:

1
2
3
4
5
6
7
8
9
class QA{
	std::string userQuestion()
	{
		public:
		std::string userInput;
    	std::cin >> userInput;
    	return userInput;
	}
};

It doesn't work anymore, debug saying that "userInput" was not specified in that scope. So, i don't ~really~ need to make anything in this class public or private for what i'm trying to accomplish, but it's really bugging me that i can't understand why it's coming off wrong when i try it...
Thank you all very much (:
Last edited on
To declare a class function private or public, the declaration goes OUTSIDE the function.

1
2
3
4
5
6
7
8
9
10
11
class QA{
   private:  // everything after this is private
	std::string userQuestion()
	{
		std::string userInput;
    	        std::cin >> userInput;
    	        return userInput;
	}
    public:  // everything after this is public
    
};
Last edited on
That's not how you use the public: thing. In your first code, your function is private by default, everything in a class is private by default. If you want to make it public, which Im sure you would want that -

1
2
3
4
5
6
7
8
9
10
11
12
13
class QA{
public: // this goes here

	std::string userQuestion()
	{	
            std::string userInput;
    	    std::cin >> userInput;
    	    return userInput;
	}

private:

};
Last edited on
Oh! I see my mistake. Thank you guys very much.
Topic archived. No new replies allowed.