Home > Archive > VC STL > February 2005 > string
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| hangaround 2005-02-22, 9:00 am |
| #include <bitset>
#include <iostream>
#include <string>
int main(void)
{
const char *a = "11";
std::bitset<8> header(std::string(a));
for (std::string::size_type i = 0; i < header.size();
++i)
{
std::cout << header[i];
}
return 0;
}
Enviroment: Win2k + VC6
Why the code above couldnot be compiled?
| |
| Ulrich Eckhardt 2005-02-22, 9:00 am |
| hangaround wrote:
> const char *a = "11";
> std::bitset<8> header(std::string(a));
Other than what you (and many others) might expect, this does _not_ create a
bitset but declares a function called 'header' that takes a std::string and
returns a std::bitset<8>. Yes, local functions are not part of C or C++,
but local function declarations are.
This is a popular mistake, workarounds are e.g.
std::bitset<8> header((std::string(a)));
std::bitset<8> header(static_cast<std::string>(a));
std::bitset<8> header = std::bitset<8>(std::string(a));
> Why the code above couldnot be compiled?
What error message do you get? Next time, try to give more info!
Uli
| |
| Hendrik Schober 2005-02-22, 9:00 am |
| hangaround <anonymous@discussions.microsoft.com> wrote:
> #include <bitset>
> #include <iostream>
> #include <string>
>
> int main(void)
> {
> const char *a = "11";
> std::bitset<8> header(std::string(a));
> for (std::string::size_type i = 0; i < header.size();
> ++i)
> {
> std::cout << header[i];
> }
> return 0;
> }
>
> Enviroment: Win2k + VC6
> Why the code above couldnot be compiled?
Because this
std::bitset<8> header(std::string(a));
declares a function named 'header', which
takes a 'std::string' (named 'a', the
parans are ignored) and returnes a
'std::bitset<8>'.
Yes, I am serious. :)
The line can be read as both a function
declaration and an object definition.
The C++ std says that, whenever there is
the choice between a declaration and a
definition, the compiler should read the
statement as a declaration.
I see a few ways to get around this:
1. std::bitset<8> header( (std::string(a)) ); // note extra ()
2. std::bitset<8> header = std::bitset<8>( (std::string(a)) );
3. const std::string b(a);
std::bitset<8> header(b);
HTH,
Schobi
--
SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org
"The presence of those s ing the truth is infinitely
to be prefered to those thinking they've found it."
Terry Pratchett
| |
| hangaround 2005-02-23, 4:01 am |
|
>-----Original Message-----
>hangaround wrote:
>
>Other than what you (and many others) might expect, this
does _not_ create a
>bitset but declares a function called 'header' that takes
a std::string and
>returns a std::bitset<8>. Yes, local functions are not
part of C or C++,
>but local function declarations are.
>
>This is a popular mistake, workarounds are e.g.
>
> std::bitset<8> header((std::string(a)));
> std::bitset<8> header(static_cast<std::string>(a));
> std::bitset<8> header = std::bitset<8>(std::string(a));
>
>
>What error message do you get? Next time, try to give
more info!
>
>Uli
>
>.
>
std::bitset<8> header((std::string(a)));
This workaround doesn't work!
VC complains as following:
Compiling...
main.cpp
E:\VcProject\CodeForRead\Experience\Try\
main.cpp(8) :
error C2061: syntax
error : identifier 'a'
E:\VcProject\CodeForRead\Experience\Try\
main.cpp(8) :
error C2066: cast to
function type is illegal
E:\VcProject\CodeForRead\Experience\Try\
main.cpp(8) :
error C2059: syntax
error : ')'
E:\VcProject\CodeForRead\Experience\Try\
main.cpp(9) :
error C2065: 'header'
: undeclared identifier
E:\VcProject\CodeForRead\Experience\Try\
main.cpp(9) :
error C2228: left of
'.size' must have class/struct/union type
E:\VcProject\CodeForRead\Experience\Try\
main.cpp(11) :
error C2109:
subscript requires array or pointer type
Error executing cl.exe.
main.obj - 6 error(s), 0 warning(s)
| |
| Tom Widmer 2005-02-23, 9:00 am |
| hangaround wrote:
> std::bitset<8> header((std::string(a)));
> This workaround doesn't work!
Compiler bug - it does work on VC++7.1. What version are you using?
Tom
| |
|
|
>-----Original Message-----
>hangaround wrote:
>
>Compiler bug - it does work on VC++7.1. What version are
you using?
>
>Tom
>.
>
I used VC6 before.
Now I have check it in VS2003 and compile it successfully!
Thank you all!
|
|
|
|
|