繁体   English   中英

C ++ Win32 API包括<string>

[英]C++ Win32 API include <string>

我正在努力在我的C ++ Win32 API初学者项目中包含string s。 如果我定义一个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

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

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

无论是编译器安装还是使用它,都会严重破坏。 无论在此之后发生什么,不能包含std::string的标题将使得使用它变得非常困难。

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

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

<string.h>包含ANSI C字符串宏和函数声明(参见此处 ),而不是C ++ string 要使用std::string ,您需要这样做

#include <string>

(没有.h

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

std::string myString; 

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

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

祝好运 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM