C++ Win32 API include <string>

11,627

Solution 1

Does the source file have a .cpp extension? If it's .c, it will compile as C code, which probably excludes the directories containing the standard C++ headers.

Solution 2

#include <string.h> //<string> throws "no such file or directory"

Something is seriously broken with either your compiler installation or your use of it. Whatever comes after this, not being able to include the header for std::string is going to make it very difficult to use one.

You can install the GCC suite without C++ support, maybe that's your problem.

Share:
11,627
Ben
Author by

Ben

javascript 1167th user (yes!) php 1312th user (wow!) html 476th user (he's good!) css 360th user (quick, hire this guy!) Proud owner of the Tumbleweed badge (his weaknesses are actually strengths!)

Updated on June 04, 2022

Comments

  • Ben
    Ben almost 2 years

    I'm trying without any luck to include strings in my C++ Win32 API beginner project. The code won't compile if I define a string. What's going on?


    Details:

    I was working in Dev C++, but now have switched to Code::Blocks using the (default?) "Gnu GCC Compiler".

    Here are the code cases I have tried, all similar, with their results:

    Compiles successfully:

    #include <windows.h>
    #include <string.h>  //<string> throws "no such file or directory"
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    //...the rest works perfectly, omitted in following examples
    

    Fails:

    #include <windows.h>
    #include <string.h>
    
    // Error: "string" does not name a type
    string myString;  
    
    // ...WndProc
    

    Compiles successfully:

    #include <windows.h>
    #include <string.h>
    using namespace std;
    
    // ...WndProc
    

    Fails:

    #include <windows.h>
    #include <string.h>
    using namespace std;
    
    // Error: "string" does not name a type
    string myString; 
    
    // ...WndProc
    

    Fails:

    #include <windows.h>
    #include <string.h>
    
    // Error: expected constructor, destructor, or type conversion before "myString"
    // Error: expected ',' or ';' before "myString"
    std::string myString; 
    
    // ...WndProc
    

    I asked this question a few days ago but deleted it because it seemed like a dumb question. However, it wasn't solved and now has come back to haunt me. Thanks in advance.