C ++ Win32 API包含<string> </string>

时间:2010-11-25 01:09:09

标签: c++ string winapi

我正在努力将string包含在我的C ++ Win32 API初学者项目中。如果我定义string,代码将无法编译。发生了什么事?


详细说明:

我在Dev C ++工作,但现在使用(默认?)“Gnu GCC编译器”切换到Code :: Blocks。

以下是我尝试过的代码案例,它们的结果都很相似:

成功编译:

#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

失败:

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

// Error: "string" does not name a type
string myString;  

// ...WndProc

成功编译:

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

// ...WndProc

失败:

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

// Error: "string" does not name a type
string myString; 

// ...WndProc

失败:

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

// Error: expected constructor, destructor, or type conversion before "myString"
// Error: expected ',' or ';' before "myString"
std::string myString; 

// ...WndProc

我几天前问了这个问题,但删除了它,因为它似乎是一个愚蠢的问题。然而,它没有解决,现在又回来困扰我。提前谢谢。

4 个答案:

答案 0 :(得分:2)

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

无论是编译器安装还是使用它,都会严重破坏。无论在此之后发生什么,都无法包含std::string的标题会使其难以使用。

您可以在没有C ++支持的情况下安装GCC套件,这可能就是您的问题。

答案 1 :(得分:2)

源文件的扩展名是否为.cpp?如果它是.c,它将编译为C代码,这可能会排除包含标准C ++标题的目录。

答案 2 :(得分:0)

<string.h>包含ANSI C字符串宏和函数声明(请参阅here),而不是C ++ string。要使用std::string,您需要执行

#include <string>

(没有.h

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

std::string myString; 

答案 3 :(得分:0)

string.h只有处理字符串的方法。例如,strcpy,strlen等...(http://opengroup.org/onlinepubs/007908799/xsh/string.h.html)

如果你想使用std :: string,你应该使用。 如果没有文件,请检查该文件。

祝你好运:)