error C2039: 'string' : is not a member of 'std', header file problem

148,014

Solution 1

You need to have

#include <string>

in the header file too.The forward declaration on it's own doesn't do enough.

Also strongly consider header guards for your header files to avoid possible future problems as your project grows. So at the top do something like:

#ifndef THE_FILE_NAME_H
#define THE_FILE_NAME_H

/* header goes in here */

#endif

This will prevent the header file from being #included multiple times, if you don't have such a guard then you can have issues with multiple declarations.

Solution 2

Your FMAT.h requires a definition of std::string in order to complete the definition of class FMAT. In FMAT.cpp, you've done this by #include <string> before #include "FMAT.h". You haven't done that in your main file.

Your attempt to forward declare string was incorrect on two levels. First you need a fully qualified name, std::string. Second this works only for pointers and references, not for variables of the declared type; a forward declaration doesn't give the compiler enough information about what to embed in the class you're defining.

Solution 3

Take care not to include

#include <string.h> 

but only

#include <string>

It took me 1 hour to find this in my code.

Hope this can help

Share:
148,014

Related videos on Youtube

Calzone41
Author by

Calzone41

Updated on August 17, 2020

Comments

  • Calzone41
    Calzone41 almost 4 years

    I am having problems with a class I am writing. I have split the class into a .h file that defines the class and an .cpp file that implements the class.

    I receive this error in Visual Studio 2010 Express:

    error C2039: 'string' : is not a member of 'std'

    This is the header FMAT.h

    class string;
    
    class FMAT {
    public:
        FMAT(); 
    
        ~FMAT(); 
    
        int session();              
    
    private:
        int manualSession();    
        int autoSession();      
    
        int     mode;       
        std::string instructionFile;    
    
    };
    

    This is the implementation file FMAT.cpp

    #include <iostream>
    #include <string>
    #include "FMAT.h"
    
    FMAT::FMAT(){
    
        std::cout << "manually (1) or instruction file (2)\n\n";
        std::cin >> mode;
        if(mode == 2){
            std::cout << "Enter full path name of instruction file\n\n";
            std::cin >> instructionFile;
        }
    
    }
    
    int FMAT::session(){
    
        if(mode==1){
            manualSession();
        }else if(mode == 2){
            autoSession();
        }
    
        return 1;
    }
    
    int FMAT::manualSession(){
        //more code
        return 0;
    }
    

    this is the main file that uses this class

    #include "FMAT.h"
    
    int main(void)
    {
        FMAT fmat;      //create instance of FMAT class
    
        fmat.session();     //this will branch to auto session or manual session
    
    }
    

    My inability to fix this error is probably a result of me not understanding how to properly structure a class into separate files. Feel free to provide some tips on how to handle multiple files in a c++ program.

    • Roman Kruglov
      Roman Kruglov about 12 years
      Probably you meant "declares a class" instead of "defines".
  • Calzone41
    Calzone41 about 14 years
    Thank you Mark, putting #include <string> in the .h file solved the problem. As this program grows in size, will I run into problems by putting the #include in the header file? I had heard that the #includes should all go in the .cpp file. Is there a proper way to structure the program so that I will not run into multiple definition problems later. Is the program as it exists now (with the #include in the header file) structured correctly?
  • Mark Ransom
    Mark Ransom about 14 years
    One solution is to have a dependency on <string> and force every source that includes FMAT.h to include it as well. Every include file should include guards to prevent multiple definitions even if the include is done multiple times.
  • TheUndeadFish
    TheUndeadFish about 14 years
    @Cal It's a generally good practice for includes to go in the .cpp file if possible. However it's not a rule that can be applied everywhere. There are various situations where it is not possible. This is one of them.
  • UserX
    UserX almost 5 years
    "You need to have #include <string> in the header file too." Wow, this seems like a violation of the laws of the language! Including a header file is supposed to be semantically equivalent to having everything in the same file;.
  • shuttle87
    shuttle87 almost 5 years
    @userX it's because the forward declaration is for a class string. which is not the same as the std::string, so when it looks for string in the std namespace it can't find the symbol.
  • UserX
    UserX almost 5 years
    At the very least it's a violation of the Law of Minimum Astonishment. The user expects #include <string> to include the definition of a string. You never see this kind of nonsense in C#.