0

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

Jjreina
  • 2,633
  • 6
  • 33
  • 54

2 Answers2

4
#include <string>

You have to include this header in all modules that use std::string.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
1

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).

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252