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.