[英]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.