Ben
Ben

Reputation: 57318

C++ Win32 API include <string>

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.

Upvotes: 2

Views: 13691

Answers (4)

Edward Strange
Edward Strange

Reputation: 40895

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

Upvotes: 2

Hwansoo Kim
Hwansoo Kim

Reputation: 286

string.h has only methods to handle the string. For example, strcpy, strlen etc... (http://opengroup.org/onlinepubs/007908799/xsh/string.h.html)

If you want to use std::string, you should use . If there is no file, check that file.

Good luck :)

Upvotes: 0

Marcelo Cantos
Marcelo Cantos

Reputation: 186118

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.

Upvotes: 2

bgporter
bgporter

Reputation: 36574

<string.h> contains ANSI C string macros and function declarations (see here) , not the C++ string. To use std::string, you need to do

#include <string>

(no .h)

#include <windows.h>
#include <string>

std::string myString; 

Upvotes: 0

Related Questions