2011-02-24 56 views
3

我有嘗試的typedef自己一個很好的方便tstring問題(見下文)問題與tstring的typedef

#ifndef _NISAMPLECLIENT_H_ 
#define _NISAMPLECLIENT_H_ 

#include <windows.h> 
#include <stdlib.h> 
using namespace std; // ERROR here (1) 

#ifdef _UNICODE 
#define CommandLineToArgv CommandLineToArgvW 
#else 
#define CommandLineToArgv CommandLineToArgvA 
#endif 

typedef basic_string<TCHAR> tstring; // ERROR HERE (2) 

試圖編譯這個時候我得到一個編譯錯誤。在Error 「錯誤在這裏(1)」 是:

Error 3 error C2871: 'std' : a namespace with this name does not exist \nisampleclient\nisampleclientdefs.h 16

如果我刪除using namespace std;聲明,並更改錯誤HERE(2)說typedef std::basic_string<TCHAR> tstring;然後我得到一個錯誤:

Error 3 error C2653: 'std' : is not a class or namespace name \nisampleclient\nisampleclientdefs.h 23

在那改爲點。

在此先感謝。 :)

回答

7

包括string標題(#include <string>,而不是string.h;))。

另外,不要用不完:

using namespace ... 

...在頭,除非你要調用下來的其他開發怒。)

側面說明:在C++中的大多數傳統的C標準標頭的零件不帶.h延伸件,但帶有領先的c。在你的情況下,#include <cstdlib>將是更好的選擇,儘管它取決於你使用的編譯器是否存在實際差異。

+0

我其實已經嘗試了另一種方式。 「使用命名空間......」是我最後希望的堡壘。 包含string.h沒有區別。不過謝謝。 – Dennis 2011-02-24 15:19:07

+0

我沒有編寫'string.h',我寫了'string';)... C++項目中的'string.h'通常會變成'cstring'。 – 0xC0000022L 2011-02-24 15:21:12

+1

你是在現場。問題其實是我使用string.h而不是字符串。耶布斯! – Dennis 2011-02-24 15:26:51

5

std::basic_string類模板有三個參數。所以你必須這樣做:

#include <string> //include this 

typedef std::basic_string<TCHAR, std::char_traits<TCHAR>, std::allocator<TCHAR> > tstring; 
+1

其實據我所知,第二個模板參數是可選的。 – 0xC0000022L 2011-02-24 15:16:50

+0

@STATUS_ACCESS_DENIED:這看起來不是來自類定義。你怎麼能這麼說? – Nawaz 2011-02-24 15:18:09

+1

很難在評論中包含代碼,但我會嘗試。由於STL是標準化的我想你有相同的代碼:'模板< 類CharType, 類性狀= char_traits , 類分配器=分配器 > 類basic_string' 的兩個參數與'='分配默認值到那些模板參數。所以除非你想重寫這些,否則給第一個就好了。 – 0xC0000022L 2011-02-24 16:03:36