Class & Objects Problem

Can someone tell me what is wrong with this code? It seems pretty straight forward. I'm just accessing a private variable through public functions but on line '19' there is an error.

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
28
29
30
31
  #include <iostream>
#include <string>
using namespace std;

class MyClass
{
      public:
             void setName(string x)
             {
                  name = x;
                  
                  }
                  string getName()
                  {
                         return name;
                         }
      
      private:
              stirng name;
      };

int main()
{
 MyClass bo;
 bo.setName = ("Sir Wulf Wallace");
 cout << bo.getName << endl;
 
 cin.get();
 return 0;   
}
Last edited on
stirng name; should be string name;
Hi,

Try getting rid of line 3, it's a bad idea to bring an namespace into a header file, and it is a bad idea to bring in entire namspaces generally. Put std:: before each std thing. As in std::cout or std::endl

When yo type std::st into your ide it should give you a list of things to select from, thus avoiding typos like this one.

Cheers
TheIdeasMan is right.
but...
This should also work now.
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
28
29
30
31
#include <iostream>
#include <string>
using namespace std;

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

	}
	string getName()
	{
		return name;
	}

private:
	string name;
};

int main()
{
	MyClass bo;
	bo.setName("Sir Wulf Wallace");
	cout << bo.getName() << endl;

	cin.get();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.