3

I am having a problem with trying to typedef myself a nice handy tstring (see below)

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

I get a compiler error when trying to compile this. The error at "ERROR here (1)" is :

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

If I remove the using namespace std;declaration and change ERROR HERE (2) to say typedef std::basic_string<TCHAR> tstring;then I get an error:

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

at that point instead.

Thanks in advance. :)

Dennis
  • 3,683
  • 1
  • 21
  • 43

2 Answers2

7

Include the string header (#include <string>, not string.h ;)).

Also, don't ever use:

using namespace ...

... in headers unless you want to call down the wrath of your fellow developers ;)

Side-note: in C++ most of the traditional C standard headers have counter-parts without .h extension but with a leading c. In your case #include <cstdlib> would be the better choice, although it depends on the compilers you use whether there is an actual difference.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
  • I actually did try it the other way first. "using namespace ... " is my bastion of last hope. Including string.h makes no difference. Thanks though. – Dennis Feb 24 '11 at 15:19
  • I did not write `string.h`, I wrote `string` ;) ... the `string.h` in a C++ project would normally become `cstring`. – 0xC0000022L Feb 24 '11 at 15:21
  • 1
    You are spot on. The problem was in fact that I was using string.h not string. Jebus! – Dennis Feb 24 '11 at 15:26
5

std::basic_string class template takes three arguments. So you've to do this:

 #include <string> //include this

 typedef std::basic_string<TCHAR, std::char_traits<TCHAR>, std::allocator<TCHAR> > tstring;
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    Actually to my knowledge the second template parameter is optional. – 0xC0000022L Feb 24 '11 at 15:16
  • @STATUS_ACCESS_DENIED: That doesn't seem from the class definition. How can you say that? – Nawaz Feb 24 '11 at 15:18
  • 1
    Hard to include code in a comment, but I'll try. Since the STL is standardized I presume you have the same code: `template < class CharType, class Traits=char_traits, class Allocator=allocator > class basic_string` The two parameters with the `=` assign defaults to those template parameters. So unless you want to override these, you're just fine with giving the first one. – 0xC0000022L Feb 24 '11 at 16:03
  • @STATUS_ACCESS_DENIED: Thanks. I saw that in the forward declaration of basic_string. Didn't see this before. – Nawaz Feb 24 '11 at 16:05