I write a program in c++ with two files.
main.cpp
#include "var.hpp"
#include <iostream>
using namespace std;
using namespace YU;
int main()
{
string god = "THL";
age = 10;
cout << age << endl;
cout << god << endl;
return 0;
}
var.hpp
#ifndef __VAR_H__
#define __VAR_H__
#include <string>
namespace YU
{
int age;
string name;
}
#endif
When I compilered it, It’get wrong.
the wrong infomation is:
In file included from main.cpp:1:0:
var.hpp:9:5: Error: ‘string’ is not a type name
I don’t know why,I had include <string> head file, but it still dosen’t work.
I write this code just for practice, not for work.
thank you!
The problem is the namespace of
stringinvar.hpp.stringis thestdnamespace, but you are not telling the compiler that. You could fix it by puttingusing namespace std;invar.hpp, but the following is a better solution as it doesn’t clutter the global namespace with other things fromstd: