-3

I am trying to do this:

#include <string>

class Medicine{

    string name;


};

but it's not working at all. I tried Right Click on the Project -> Index -> Searched for Unresolved Includes and it says: Unresolved includes in the Project(0 matches). It's not working with std::string either. What should I do?

Cucerzan Rares
  • 73
  • 3
  • 5
  • 10

3 Answers3

5

You should either fully qualify string with the namespace it belongs to (std):

#include <string>

class Medicine {
    std::string name;
//  ^^^^^
};

Or use a using declaration:

#include <string>

using std::string; // <== This will allow you to use "string" as an
                   //     unqualified name (resolving to "std::string")

class Medicine {
    string name;
//  ^^^^^^
//  No need to fully qualify the name thanks to the using declaration
};
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
1

The string class (of the header) is defined inside the std namespace. You are missing using std::string; or std:: before string in the object declaration.

If you still can't fix it then take a look at this answer.

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
0

try creating a new console project and keeping it to this simple code below. If this doesn't work then possibly you're eclipse isn't setup for c++ correctly. The default download for an eclipse c++ environment is here http://www.eclipse.org/cdt/

#include "stdafx.h"//optional depending if you have precompiled headers in VC++ project
#include <string>

using std::string;

class Medicine
{
    string name;        
};

// or use this alternative main if one below doesn't work
//int main(int argc, _TCHAR* argv[])
int _tmain(int argc, _TCHAR* argv[])
{
    Medicine test;

    return 0;
}
Rich
  • 4,572
  • 3
  • 25
  • 31
  • it's working. But when I am in a header file it doesn't recognise the string. – Cucerzan Rares Mar 31 '13 at 23:53
  • I don't see why this shouldn't work if you've #included and used "using std::string;" in the header. I think you need to post more code so we can see more of what you're doing. Specifically post the code from the header file that eclipse is complaining about, exactly as it appears in your project. – Rich Apr 01 '13 at 00:01