error with using namespace std

the program runs well for this header file.
#pragma once
#include"birthday.h"
#include<string>
using namespace std;
class people
{
public:
people(string yourName, birthday DOB);
void printInfo();
private:
string name;
birthday dobObject;
};

but
by removing using namespace std; the programs fails with error. can you please give me the reason and elaborate?
#pragma once
#include"birthday.h"
#include<string>

class people
{
public:
people(string yourName, birthday DOB);
void printInfo();
private:
string name;
birthday dobObject;
};


Last edited on
1. Avoid using namespace std; at global scope in a header file.
2. In the header, use qualified names for entities from the standard library

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// #include guard
#include"birthday.h"
#include<string>

class people
{
    public:
        people( std::string yourName, birthday DOB );

        void printInfo() const ; // non-modifier, so const
    
    private:
        std::string name;
        birthday date_of_birth;	
};
#pragma once

class birthday
{
public:
birthday(int m, int d , int );
void printOutDOB();
private:
int day;
int month;
int year;

};

excuse me sir, but for above header file "bithday.h" i didn't have to use qualified names for int. is there some rules for that in documentation or what? can you elaborate it?
> i didn't have to use qualified names for int

int is a keyword http://en.cppreference.com/w/cpp/keyword
Keywords are built into the language; there is no name look up to determine what a keyword stands for.

Identifiers (like string), which are not keywords are subject to name look up. http://en.cppreference.com/w/cpp/language/lookup
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) There's nothing in the header file that needs a std:: qualification, but there are things in your source file. Since there's no longer a using namespace std; statement anywhere in the translation unit, you'll need to qualify those names in the source file.
Last edited on
by removing using namespace std; the programs fails with error.

You do mean that the compiler aborts and gives error messages.

It is not enough to merely note that there is "an error". You have to read the error messages carefully, because they tell what offends the compiler.

For example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<string>

class people
{
    public:
        people( string yourName, int DOB );

        void printInfo() const ;
    
    private:
        string name;
        int date_of_birth;	
};

int main()
{
    return 0;
}

Produces on one compiler:
6:24: error: expected ')' before 'yourName'
11:9: error: 'string' does not name a type

and in another:
main.cpp:6:24: error: expected ')' before 'yourName'
         people( string yourName, int DOB );
                        ^
main.cpp:11:9: error: 'string' does not name a type
         string name;
         ^

Both clearly point to lines 6 and 11.

Line 11 is quite clear; the compiler understands that 'string' should probably be a name of a type, but compiler has not seen definition of such type. You do include <string> and it does contain definition of 'string', but that definition is within namespace 'std' and the compiler does not see inside namespaces unless it is told to look there.

Both using namespace std; and using std::string; essentially state that when looking for 'string', a 'string' inside 'std' is a match too.

std::string in code is more explicit: only the 'string' in 'std' is a match.


What is before 'yourName' on line 6? people( string
This message is harder to explain.
We can try to humor the compiler and test what happens if we write line 6 as:
people( string );
Alas, that gives a different error:
6:24: error: field 'string' has incomplete type 'people'



The important thing is to read those error messages and use the info the best you can.
For example, when asking for help do show the exact messages. Someone might be able to help you read them.
clang++ emits a very clear diagnostic.

MinGW64 6:38am /r/code/test >CC --version
clang version 5.0.1 (tags/RELEASE_501/final)
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:\msys64\mingw64\bin
MinGW64 6:38am /r/code/test >CC -c test.cpp
test.cpp:6:17: error: unknown type name 'string'; did you mean 'std::string'?
        people( string yourName, int DOB );
                ^~~~~~
                std::string
C:\msys64\mingw64\include\c++\7.3.0\bits/stringfwd.h:74:33: note: 'std::string' declared here
  typedef basic_string<char>    string;
                                ^
test.cpp:11:9: error: unknown type name 'string'; did you mean 'std::string'?
        string name;
        ^~~~~~
        std::string
C:\msys64\mingw64\include\c++\7.3.0\bits/stringfwd.h:74:33: note: 'std::string' declared here
  typedef basic_string<char>    string;
                                ^
2 errors generated.
Topic archived. No new replies allowed.