using std::string and string::npos... 
Author Message
 using std::string and string::npos...

We have code that typically does this:

#include <string>

using std::string;

namespace OurNamespace
{

// or here - either way
// using std::string;

void Func( const string& _r_String, size_t x =
string::npos );

Quote:
};

//-------------------

.NET VC++ 7.1.3088 will not compile this code, complaining
that:

error
C2653: 'basic_string<char,std::char_traits<char>,std::alloc
ator<char> >' : is not a class or namespace name

and then

error C2065: 'npos' : undeclared identifier

I can solve this by changing string::npos to
std::string::npos, but I want to know why the above syntax
*does not* work, or what I can do to make it work, without
changing my code.

Thanks

Kevin



Tue, 29 Nov 2005 08:47:58 GMT  
 using std::string and string::npos...
change either 'using' into 'using namespace std;'

nick


Quote:
> We have code that typically does this:

> #include <string>

> using std::string;

> namespace OurNamespace
> {

> // or here - either way
> // using std::string;

> void Func( const string& _r_String, size_t x =
> string::npos );

> };

> //-------------------

> .NET VC++ 7.1.3088 will not compile this code, complaining
> that:

> error
> C2653: 'basic_string<char,std::char_traits<char>,std::alloc
> ator<char> >' : is not a class or namespace name

> and then

> error C2065: 'npos' : undeclared identifier

> I can solve this by changing string::npos to
> std::string::npos, but I want to know why the above syntax
> *does not* work, or what I can do to make it work, without
> changing my code.

> Thanks

> Kevin



Tue, 29 Nov 2005 09:21:19 GMT  
 using std::string and string::npos...

Quote:
> change either 'using' into 'using namespace std;'

That's not a good solution.  You should try to minimize namespace pollution
whenever possible.

Ken



Tue, 29 Nov 2005 09:52:15 GMT  
 using std::string and string::npos...

Quote:
> We have code that typically does this:
<snip>
> error
> C2653: 'basic_string<char,std::char_traits<char>,std::alloc
> ator<char> >' : is not a class or namespace name

This is odd, because basic_string<> pretty clearly is a class name...

Quote:
> I can solve this by changing string::npos to
> std::string::npos, but I want to know why the above syntax
> *does not* work, or what I can do to make it work, without
> changing my code.

I don't have VC 7.1 handy to test right now, but your code (minus the spurious
semicolon after the namespace) compiles fine in VC 7.0 and Comeau.  If it
fails on VC 7.1, it sounds like a bug to me.

Ken



Tue, 29 Nov 2005 09:57:42 GMT  
 using std::string and string::npos...
Ah, yes, spurious semi-colon - I wrote this out of my head.

Yes, I figure it is a bug also.



Tue, 29 Nov 2005 10:18:25 GMT  
 using std::string and string::npos...

Quote:



> > change either 'using' into 'using namespace std;'

> That's not a good solution.  You should try to minimize namespace pollution
> whenever possible.

It's a bug in VC6 for sure.

Using directives don't bring forward the names.   The only other solution is to
FULLY qualify std::string::npos.



Tue, 29 Nov 2005 23:17:17 GMT  
 using std::string and string::npos...

Quote:



> > We have code that typically does this:
> <snip>
> > error
> > C2653: 'basic_string<char,std::char_traits<char>,std::alloc
> > ator<char> >' : is not a class or namespace name

> This is odd, because basic_string<> pretty clearly is a class name...

It's got nothing to do with string.

namespace nsname {
    struct foo {
        typedef int fooname;
    };

Quote:
}

using nsname::foo;

is broken in VC6.   It will not let you access things qualified off foo:

There are three solutions:

    using namespace nsname;        // ugly, dumps the whole namespace in.
    nsname::foo::fooname ;              // fully qualify everything
    using nsname::foo::fooname      // works, but isn't legal C++ otherwise.



Tue, 29 Nov 2005 23:19:48 GMT  
 using std::string and string::npos...

Quote:
> We have code that typically does this:

> #include <string>

> using std::string;

> namespace OurNamespace
> {

> // or here - either way
> // using std::string;

> void Func( const string& _r_String, size_t x =
> string::npos );

> };

> //-------------------

> .NET VC++ 7.1.3088 will not compile this code, complaining
> that:

This code compiles for me just fine, with no change, on VC++ 7.1.3088.
Can you show a small complete program that reproduces the problem?
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Wed, 30 Nov 2005 03:44:03 GMT  
 using std::string and string::npos...


Quote:


> > > change either 'using' into 'using namespace std;'

> > That's not a good solution.  You should try to minimize namespace
pollution
> > whenever possible.

> It's a bug in VC6 for sure.

> Using directives don't bring forward the names.   The only other solution is
to
> FULLY qualify std::string::npos.

Problem is, the OP was using VC 7.1, not VC 6.  If it's a bug, it's a
regression.

Ken



Wed, 30 Nov 2005 04:15:49 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. convert non std::string to std::string

2. int to std::string to System::String

3. static std::string exposed through System::String *

4. String to std:string

5. System::String to std::string

6. System.String to std::string conversion

7. null-terminated string to std::string

8. Assignind a NULL C string to an std::string

9. Calling an unmanaged DLL function that uses a std::string parameter

10. using std::string with windows classes gives error

11. convert between std::string and std::wstring

12. Warnings for std::vector<std::string>

 

 
Powered by phpBB® Forum Software