problems with standard header

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan D. Lucey

    problems with standard header

    ok I'm trying out a small program using operator overloading from the book
    C++ How To Program by Deitel Deitel. In their code they use the old style
    headers
    and the program compiles and runs as it should, but if I switch to the new
    style headers and add 'using namespace std' and change nothing else
    I get errors saying that the class object cannot access private data in the
    class although the overloaded function is declared as a friend. It compiles
    fine however if I add the .h extension to the headers <iostream> and
    <iomanip>

    What gives ?


    Ryan


  • John Harrison

    #2
    Re: problems with standard header


    "Ryan D. Lucey" <mojokid@comcas t.net> wrote in message
    news:3f4842f7$0 $10462$9a6e19ea @news.newshosti ng.com...[color=blue]
    > ok I'm trying out a small program using operator overloading from the book
    > C++ How To Program by Deitel Deitel. In their code they use the old style
    > headers
    > and the program compiles and runs as it should, but if I switch to the new
    > style headers and add 'using namespace std' and change nothing else
    > I get errors saying that the class object cannot access private data in[/color]
    the[color=blue]
    > class although the overloaded function is declared as a friend. It[/color]
    compiles[color=blue]
    > fine however if I add the .h extension to the headers <iostream> and
    > <iomanip>
    >
    > What gives ?
    >[/color]

    Hard to say without seeing the code. My guess would be that you've declared
    the friendship wrongly, remember operator<< is now in the std namespace. Or
    that you've a non-compliant compiler. Post the code.

    john


    Comment

    • Ryan D. Lucey

      #3
      Re: problems with standard header

      I kind of solved the problem, if I specify the namespace
      std for all of the objects i'm using that belong to namespace std it
      compiles without problem (i.e. std::cout << myObject as opposed to cout <<
      myObject in which I get an ambiguous call error ). Why isn't this resolved
      when I do using namespace std; Do I have
      to use std:: before anything I want to use within that namespace ?
      "Ryan D. Lucey" <mojokid@comcas t.net> wrote in message
      news:3f4842f7$0 $10462$9a6e19ea @news.newshosti ng.com...[color=blue]
      > ok I'm trying out a small program using operator overloading from the book
      > C++ How To Program by Deitel Deitel. In their code they use the old style
      > headers
      > and the program compiles and runs as it should, but if I switch to the new
      > style headers and add 'using namespace std' and change nothing else
      > I get errors saying that the class object cannot access private data in[/color]
      the[color=blue]
      > class although the overloaded function is declared as a friend. It[/color]
      compiles[color=blue]
      > fine however if I add the .h extension to the headers <iostream> and
      > <iomanip>
      >
      > What gives ?
      >
      >
      > Ryan
      >
      >[/color]


      Comment

      • Ryan D. Lucey

        #4
        Re: problems with standard header

        here's the code .. compiled using visual c++ 6.0

        / stomping.cpp : Defines the entry point for the console application.
        //

        #include "stdafx.h"

        #include <iostream>
        #include <string>
        #include <fstream>

        using namespace std;


        class testClass
        {


        int a;

        public:

        testClass(int = 1);
        virtual ~testClass();

        void setData(int);
        int getData() { return(a); }
        void writeData( string fileName );

        friend ostream& operator<<(ostr eam& , const testClass& );
        };

        ostream& operator<<( ostream& outStream, const testClass& object)
        {
        outStream << "Value of object: " << object.a << std::endl;

        return outStream;
        }

        testClass::test Class(int a_)
        {
        setData(a_);
        }

        testClass::~tes tClass()
        {

        }

        void testClass::setD ata(int a_)
        {
        a = a_;
        }

        void testClass::writ eData( std::string fileName )
        {
        ofstream outPut;



        outPut.open( fileName.c_str( ));


        outPut.close();
        }


        int main(int argc, char* argv[])
        {
        testClass object1(30);
        testClass object2(25);

        object1.writeDa ta("c:\\object1 .txt");
        object2.writeDa ta("c:\\object2 .txt");

        cout << object1;




        return 0;
        }


        errors:

        --------------------Configuration: stomping - Win32
        Debug--------------------
        Compiling...
        stomping.cpp
        E:\Projects\sto mping\stomping. cpp(33) : error C2248: 'a' : cannot access
        private member declared in class 'testClass'
        E:\Projects\sto mping\stomping. cpp(17) : see declaration of 'a'
        E:\Projects\sto mping\stomping. cpp(74) : error C2593: 'operator <<' is
        ambiguous
        Error executing cl.exe.

        stomping.exe - 2 error(s), 0 warning(s)
        "John Harrison" <john_andronicu s@hotmail.com> wrote in message
        news:bi9gr9$6ri 58$1@ID-196037.news.uni-berlin.de...[color=blue]
        >
        > "Ryan D. Lucey" <mojokid@comcas t.net> wrote in message
        > news:3f4842f7$0 $10462$9a6e19ea @news.newshosti ng.com...[color=green]
        > > ok I'm trying out a small program using operator overloading from the[/color][/color]
        book[color=blue][color=green]
        > > C++ How To Program by Deitel Deitel. In their code they use the old[/color][/color]
        style[color=blue][color=green]
        > > headers
        > > and the program compiles and runs as it should, but if I switch to the[/color][/color]
        new[color=blue][color=green]
        > > style headers and add 'using namespace std' and change nothing else
        > > I get errors saying that the class object cannot access private data in[/color]
        > the[color=green]
        > > class although the overloaded function is declared as a friend. It[/color]
        > compiles[color=green]
        > > fine however if I add the .h extension to the headers <iostream> and
        > > <iomanip>
        > >
        > > What gives ?
        > >[/color]
        >
        > Hard to say without seeing the code. My guess would be that you've[/color]
        declared[color=blue]
        > the friendship wrongly, remember operator<< is now in the std namespace.[/color]
        Or[color=blue]
        > that you've a non-compliant compiler. Post the code.
        >
        > john
        >
        >[/color]


        Comment

        • John Harrison

          #5
          Re: problems with standard header


          "Ryan D. Lucey" <mojokid@comcas t.net> wrote in message
          news:3f484c32$0 $10454$9a6e19ea @news.newshosti ng.com...[color=blue]
          > here's the code .. compiled using visual c++ 6.0
          >
          > / stomping.cpp : Defines the entry point for the console application.
          > //
          >
          > #include "stdafx.h"
          >
          > #include <iostream>
          > #include <string>
          > #include <fstream>
          >
          > using namespace std;
          >
          >
          > class testClass
          > {
          >
          >
          > int a;
          >
          > public:
          >
          > testClass(int = 1);
          > virtual ~testClass();
          >
          > void setData(int);
          > int getData() { return(a); }
          > void writeData( string fileName );
          >
          > friend ostream& operator<<(ostr eam& , const testClass& );
          > };
          >
          > ostream& operator<<( ostream& outStream, const testClass& object)
          > {
          > outStream << "Value of object: " << object.a << std::endl;
          >
          > return outStream;
          > }
          >
          > testClass::test Class(int a_)
          > {
          > setData(a_);
          > }
          >
          > testClass::~tes tClass()
          > {
          >
          > }
          >
          > void testClass::setD ata(int a_)
          > {
          > a = a_;
          > }
          >
          > void testClass::writ eData( std::string fileName )
          > {
          > ofstream outPut;
          >
          >
          >
          > outPut.open( fileName.c_str( ));
          >
          >
          > outPut.close();
          > }
          >
          >
          > int main(int argc, char* argv[])
          > {
          > testClass object1(30);
          > testClass object2(25);
          >
          > object1.writeDa ta("c:\\object1 .txt");
          > object2.writeDa ta("c:\\object2 .txt");
          >
          > cout << object1;
          >
          >
          >
          >
          > return 0;
          > }
          >
          >[/color]

          Compile successfully with VC++ 7.1. VC++ 6 is bugged. This might be a
          service pack issue, if you haven't already, upgrade to SP5.

          I don't have a copy of VC++ 6 to test but try the following as workarounds.

          1) declare the function inline

          class testClass
          {
          ...
          friend ostream& operator<<(ostr eam& outStream, const testClass& object)
          {
          outStream << "Value of object: " << object.a << std::endl;
          }
          };


          2) declare the function in the std namespace

          namespace std
          {
          ostream& operator<<(ostr eam& , const testClass& );
          }

          class testClass
          {
          ...
          friend ostream& std::operator<< (ostream& , const testClass& );
          };

          namespace std
          {
          ostream& operator<<(ostr eam& outStream , const testClass& object)
          {
          outStream << "Value of object: " << object.a << std::endl;
          }
          }

          I pretty sure one of those works, because it definitely is possible to do
          this in VC++ 6. But it a while since I've had to do this in VC++ 6 so I
          can't quite recall.

          john


          Comment

          • Michael Fawcett

            #6
            Re: problems with standard header

            "John Harrison" <john_andronicu s@hotmail.com> wrote in message
            news:bi9kvg$731 0m$1@ID-196037.news.uni-berlin.de...[color=blue]
            >
            > "Ryan D. Lucey" <mojokid@comcas t.net> wrote in message
            > news:3f484c32$0 $10454$9a6e19ea @news.newshosti ng.com...[color=green]
            > > here's the code .. compiled using visual c++ 6.0
            > >[/color][/color]


            // lots of code
            *snip*

            [color=blue]
            > Compile successfully with VC++ 7.1. VC++ 6 is bugged. This might be a
            > service pack issue, if you haven't already, upgrade to SP5.
            >
            > I don't have a copy of VC++ 6 to test but try the following as[/color]
            workarounds.[color=blue]
            >
            > 1) declare the function inline
            >
            > class testClass
            > {
            > ...
            > friend ostream& operator<<(ostr eam& outStream, const testClass&[/color]
            object)[color=blue]
            > {
            > outStream << "Value of object: " << object.a << std::endl;
            > }
            > };
            >
            >
            > 2) declare the function in the std namespace
            >
            > namespace std
            > {
            > ostream& operator<<(ostr eam& , const testClass& );
            > }
            >
            > class testClass
            > {
            > ...
            > friend ostream& std::operator<< (ostream& , const testClass& );
            > };
            >
            > namespace std
            > {
            > ostream& operator<<(ostr eam& outStream , const testClass& object)
            > {
            > outStream << "Value of object: " << object.a << std::endl;
            > }
            > }
            >
            > I pretty sure one of those works, because it definitely is possible to do
            > this in VC++ 6. But it a while since I've had to do this in VC++ 6 so I
            > can't quite recall.
            >
            > john
            >[/color]


            Visual C++ 6.0 didn't handle friends correctly until SP3. Download SP5 as
            soon as you can.

            -Michael


            Comment

            • Ryan D. Lucey

              #7
              Re: problems with standard header

              Got the service pack, all is well!
              Thanks guys.

              Ryan
              "Michael Fawcett" <xmikefx@xintel gamesx.xcomx> wrote in message
              news:b8_1b.7941 9$K4.3560605@tw ister.tampabay. rr.com...[color=blue]
              > "John Harrison" <john_andronicu s@hotmail.com> wrote in message
              > news:bi9kvg$731 0m$1@ID-196037.news.uni-berlin.de...[color=green]
              > >
              > > "Ryan D. Lucey" <mojokid@comcas t.net> wrote in message
              > > news:3f484c32$0 $10454$9a6e19ea @news.newshosti ng.com...[color=darkred]
              > > > here's the code .. compiled using visual c++ 6.0
              > > >[/color][/color]
              >
              >
              > // lots of code
              > *snip*
              >
              >[color=green]
              > > Compile successfully with VC++ 7.1. VC++ 6 is bugged. This might be a
              > > service pack issue, if you haven't already, upgrade to SP5.
              > >
              > > I don't have a copy of VC++ 6 to test but try the following as[/color]
              > workarounds.[color=green]
              > >
              > > 1) declare the function inline
              > >
              > > class testClass
              > > {
              > > ...
              > > friend ostream& operator<<(ostr eam& outStream, const testClass&[/color]
              > object)[color=green]
              > > {
              > > outStream << "Value of object: " << object.a << std::endl;
              > > }
              > > };
              > >
              > >
              > > 2) declare the function in the std namespace
              > >
              > > namespace std
              > > {
              > > ostream& operator<<(ostr eam& , const testClass& );
              > > }
              > >
              > > class testClass
              > > {
              > > ...
              > > friend ostream& std::operator<< (ostream& , const testClass& );
              > > };
              > >
              > > namespace std
              > > {
              > > ostream& operator<<(ostr eam& outStream , const testClass& object)
              > > {
              > > outStream << "Value of object: " << object.a << std::endl;
              > > }
              > > }
              > >
              > > I pretty sure one of those works, because it definitely is possible to[/color][/color]
              do[color=blue][color=green]
              > > this in VC++ 6. But it a while since I've had to do this in VC++ 6 so I
              > > can't quite recall.
              > >
              > > john
              > >[/color]
              >
              >
              > Visual C++ 6.0 didn't handle friends correctly until SP3. Download SP5 as
              > soon as you can.
              >
              > -Michael
              >
              >[/color]


              Comment

              Working...