Reputation: 2713
i have a c++ project on eclipse and i can to use the string class in main file, but when i add a new file to the project I can not use any class.
I add file how: New-> Source file and select Template: Defaul c++ template source.
This is error src/Common.cpp:8:17: error: ‘string’ was not declared in this scope
and in src/AC.cpp all ok
thanks very much
Thanks larsman my simple code is:
AC.cpp -> all ok
include <iostream>
using namespace std;
int main()
{
string j = !!!Hello World!!!;
cout << j << endl; // prints !!!Hello World!!!
return 0;
}
Common.cpp -> src/Common.cpp:8:17: error: ‘string’ was not declared in this scope
#include <string>
void PrintTrace(string message)
{
string j = !!!Hello World!!!;
cout << j << endl; // prints !!!Hello World!!!
}
both files are the same project.
Thanks
Upvotes: 0
Views: 2391
Reputation: 122001
In addition to adding the include directive #include <string>
(as stated by larsmans) you must refer to it using its fully qualified name std::string
.
If you want to use string
only, add using std::string;
to each .cpp
source file where it is used (you could also add using namespace std;
but this is not recommended).
Upvotes: 1
Reputation: 363727
#include <string>
You have to include this header in all modules that use std::string
.
Upvotes: 4