Accessing private class to public
Why does line 23 errors?
I want to print the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include<iostream>
using namespace std;
class Shit
{
public:
void setName(string x)
{
name = x;
}
string getName()
{
return name;
}
private:
string name;
};
int main()
{
Shit s;
s.setName("shits");
cout << s.getName;
system("pause>0");
return 0;
}
| |
getName is not a variable.
When you are calling a function, even if it doesn't have any parameters, you need to include parentheses.
You have this:
std::cout << s.getName;
When you should have this:
std::cout << s.getName();
Also, there is a reason why the "Console Closing Down" thread is locked. It brings up a good point that you should never really use system("PAUSE");
http://www.cplusplus.com/forum/beginner/1988/
Thank you, guys. I fixed it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include<iostream>
#include<string>
using namespace std;
class Shit
{
public:
void setName(string x)
{
name = x;
}
string getName()
{
return name;
}
private:
string name;
};
int main()
{
Shit s;
s.setName("shits");
cout << s.getName();
system("pause>0");
return 0;
}
| |
Topic archived. No new replies allowed.