Re: string

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Hendrik Schober (SpamTrap_at_gmx.de)
Date: 02/22/05


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


Relevant Pages

  • Re: Question about multiple files
    ... No. extern just means: The actual definition is somewhere else. ... the compiler can differentiate between declaration and definition just ... > declaration it finds and in this way can I write code expecting it to be ... When the compiler reaches the line foo(); ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Question about multiple files
    ... but say I declare with extern. ... > the compiler can differentiate between declaration and definition just ... >> declaration it finds and in this way can I write code expecting it to be ... > When the compiler reaches the line foo(); ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Cpp Considered Harmful
    ... >> programming language, the compiler, the IDE, the libraries, etc. ... source file, the implementation shall locate the declaration, (and ... to name one of the defining source files on the command line that initiates ... The counterpart to this in Java is accomplished using the following: ...
    (comp.lang.cpp)
  • Re: Program Fails When Parameter Fixed Constants are Changed (F77) ??
    ... ZBRENT is a function as opposed to a subroutine. ... I don't know how to tell the compiler DCPVAL is a fun and not a sub. ... That is not the same thing as being valid f77. ... declared the name of the External as real throughout, one declaration ...
    (comp.lang.fortran)
  • Re: Internal compiler error C1001 after SP6 installation
    ... Declaring variables like ... fixed it is not particularly productive to worry about why the compiler is choking. ... >The stdafx.h contains a class declaration at line 30. ... the optimizations should caused these error. ...
    (microsoft.public.vc.mfc)