What is wrong with this simple code!?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • IWP506@gmail.com

    What is wrong with this simple code!?

    10 points (and a cookie) to whoever tells me why this code generates
    these errors:

    C:\Programming\ Microsoft Visual Studio\MyProjec ts\HelloWorld\m ain.c(5)
    : error C2065: 'cout' : undeclared identifier
    C:\Programming\ Microsoft Visual Studio\MyProjec ts\HelloWorld\m ain.c(5)
    : error C2297: '<<' : illegal, right operand has type 'char [2]'

    #include <iostream.h>

    int main()
    {
    cout << "h";
    return 0;
    }

    Thanks
    iwp506@gmail.co m

  • Victor Bazarov

    #2
    Re: What is wrong with this simple code!?

    IWP506@gmail.co m wrote:[color=blue]
    > 10 points (and a cookie) to whoever tells me why this code generates
    > these errors:
    >
    > C:\Programming\ Microsoft Visual Studio\MyProjec ts\HelloWorld\m ain.c(5)[color=green]
    >> error C2065: 'cout' : undeclared identifier[/color]
    > C:\Programming\ Microsoft Visual Studio\MyProjec ts\HelloWorld\m ain.c(5)[color=green]
    >> error C2297: '<<' : illegal, right operand has type 'char [2]'[/color]
    >
    > #include <iostream.h>[/color]

    Should be

    #include <iostream>
    using std::cout;
    [color=blue]
    >
    > int main()
    > {
    > cout << "h";
    > return 0;
    > }
    >
    > Thanks
    > iwp506@gmail.co m[/color]

    Rename the file to .cpp or .C or .cxx or .c++ or anything that would
    make it a C++ source file. Your compiler apparently attempts to treat
    it as a C source file.

    V


    Comment

    • BigBrian

      #3
      Re: What is wrong with this simple code!?


      IWP506@gmail.co m wrote:[color=blue]
      > 10 points (and a cookie) to whoever tells me why this code generates
      > these errors:
      >
      > C:\Programming\ Microsoft Visual Studio\MyProjec ts\HelloWorld\m ain.c(5)
      > : error C2065: 'cout' : undeclared identifier
      > C:\Programming\ Microsoft Visual Studio\MyProjec ts\HelloWorld\m ain.c(5)
      > : error C2297: '<<' : illegal, right operand has type 'char [2]'
      >
      > #include <iostream.h>[/color]

      It should be
      #include <iostream>
      [color=blue]
      > int main()
      > {
      > cout << "h";[/color]

      std::cout << "h" << std::endl;

      Comment

      • IWP506@gmail.com

        #4
        Re: What is wrong with this simple code!?

        I see... so I could using "using namespace std;" then if I wanted to
        just make it "cout" and not "std::cout" ?

        BTW, thanks for the uberly quick responses.

        Comment

        • j

          #5
          Re: What is wrong with this simple code!?


          <IWP506@gmail.c om> wrote in message
          news:1125113242 .313248.45270@g 47g2000cwa.goog legroups.com...[color=blue]
          > I see... so I could using "using namespace std;" then if I wanted to
          > just make it "cout" and not "std::cout" ?
          >
          > BTW, thanks for the uberly quick responses.
          >[/color]

          The namespaces are there for a reason, so your code does not trip-over some
          other library code, so it is not a good idea just to discard the namespaces.
          Try getting use to writing std:: and use namespaces in your own code too..
          with code completion and copy & paste it really is not that hard to get used
          to..

          Jesper


          Comment

          • Karl Heinz Buchegger

            #6
            Re: What is wrong with this simple code!?

            IWP506@gmail.co m wrote:[color=blue]
            >
            > 10 points (and a cookie) to whoever tells me why this code generates
            > these errors:
            >
            > C:\Programming\ Microsoft Visual Studio\MyProjec ts\HelloWorld\m ain.c(5)
            > : error C2065: 'cout' : undeclared identifier
            > C:\Programming\ Microsoft Visual Studio\MyProjec ts\HelloWorld\m ain.c(5)
            > : error C2297: '<<' : illegal, right operand has type 'char [2]'
            >[/color]

            In addition:

            C:\Programming\ Microsoft Visual Studio\MyProjec ts\HelloWorld\m ain.c
            ***

            If you haven't changed anything else, than VC++ assumes this is C code not C++.
            For C++ use a file extension of *.cpp

            --
            Karl Heinz Buchegger
            kbuchegg@gascad .at

            Comment

            Working...