Re: string
From: Hendrik Schober (SpamTrap_at_gmx.de)
Date: 02/22/05
- Next message: hangaround: "Re: string"
- Previous message: Ulrich Eckhardt: "Re: string"
- In reply to: hangaround: "string"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 22 Feb 2005 13:11:18 +0100
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 seeking the truth is infinitely to be prefered to those thinking they've found it." Terry Pratchett
- Next message: hangaround: "Re: string"
- Previous message: Ulrich Eckhardt: "Re: string"
- In reply to: hangaround: "string"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|