VS2012 Ultimate doesn't recognize saved word

Hi guys,
I'm taking a OOP course in the university and we're studying CPP, so I'm quite new at OOP.

I've tried to solve a small problem which was given to students in 2007 and I've came across a problem:
The Visual Studio doesn't recognize the word "string". At the lecture we were told this is a saved word(and in order to use it I have to include the "string" library, so I did). I've added to code so you could look at it.

If it wasn't clear so far, I'm using Visual Studio 2012 Ultimate.

Thanks in advance!

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

  class Person{ 
private: 
	int age; 
	string _pname; 
	string _last_name;
}


1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

  class Person{ 
private: 
	int age; 
	std::string _pname; 
	std::string _last_name;
}


Why do I have to use "std::"?
Because standard C++ classes are declared in the standard namespace std.

Consider an example with your own namespace

1
2
3
4
5
6
7
8
9
10
namespace MyNamespace
{
   class Person {};
}

int main()
{
   Person p1;  // Error. There is no such name as Person
   MyNamespace::Person p2; // O'k there is such name in your namespace 
}
Last edited on
If you do not want to type every time std:: when you can announce that you will use all names of the standard namespace by means of directive.

using namespace std;

Thanks Vlad, so generally namespace is an actual "space" which contains name and declarations(such as input-output->cout, cin and classes)?

It would nice of you to give me an precise definition of namespace.

From the C++ Standard

7.3 Namespaces [basic.namespace]
1 A namespace is an optionally-named declarative region. The name of a namespace can be used to access entities declared in that namespace; that is, the members of the namespace. Unlike other declarative regions, the definition of a namespace can be split over several parts of one or more translation units.
2 The outermost declarative region of a translation unit is a namespace; see 3.3.6.

3.3.6 Namespace scope
2 A namespace member can also be referred to after the :: scope resolution operator (5.1) applied to the name of its namespace or the name of a namespace which nominates the member’s namespace in a using-directive;
see 3.4.3.2.
3 The outermost declarative region of a translation unit is also a namespace, called the global namespace. A name declared in the global namespace has global namespace scope (also called global scope). The potential scope of such a name begins at its point of declaration (3.3.2) and ends at the end of the translation unit that is its declarative region. Names with global namespace scope are said to be global name.
Here is an easier way of understanding it. The libraries string and iostream and some more are located within the standard c++ namespace.
Cheers
Topic archived. No new replies allowed.