problems with standard header

Discussion in 'C++' started by Ryan D. Lucey, Aug 24, 2003.

  1. 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
     
    Ryan D. Lucey, Aug 24, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:3f4842f7$0$10462$...
    > 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
    >
    >
     
    Ryan D. Lucey, Aug 24, 2003
    #2
    1. Advertising

  3. "Ryan D. Lucey" <> wrote in message
    news:3f4842f7$0$10462$...
    > 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 ?
    >


    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
     
    John Harrison, Aug 24, 2003
    #3
  4. 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<<(ostream& , const testClass& );
    };

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

    return outStream;
    }

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

    testClass::~testClass()
    {

    }

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

    void testClass::writeData( 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.writeData("c:\\object1.txt");
    object2.writeData("c:\\object2.txt");

    cout << object1;




    return 0;
    }


    errors:

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

    stomping.exe - 2 error(s), 0 warning(s)
    "John Harrison" <> wrote in message
    news:bi9gr9$6ri58$-berlin.de...
    >
    > "Ryan D. Lucey" <> wrote in message
    > news:3f4842f7$0$10462$...
    > > 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 ?
    > >

    >
    > 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
    >
    >
     
    Ryan D. Lucey, Aug 24, 2003
    #4
  5. "Ryan D. Lucey" <> wrote in message
    news:3f484c32$0$10454$...
    > 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<<(ostream& , const testClass& );
    > };
    >
    > ostream& operator<<( ostream& outStream, const testClass& object)
    > {
    > outStream << "Value of object: " << object.a << std::endl;
    >
    > return outStream;
    > }
    >
    > testClass::testClass(int a_)
    > {
    > setData(a_);
    > }
    >
    > testClass::~testClass()
    > {
    >
    > }
    >
    > void testClass::setData(int a_)
    > {
    > a = a_;
    > }
    >
    > void testClass::writeData( 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.writeData("c:\\object1.txt");
    > object2.writeData("c:\\object2.txt");
    >
    > cout << object1;
    >
    >
    >
    >
    > return 0;
    > }
    >
    >


    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<<(ostream& outStream, const testClass& object)
    {
    outStream << "Value of object: " << object.a << std::endl;
    }
    };


    2) declare the function in the std namespace

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

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

    namespace std
    {
    ostream& operator<<(ostream& 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
     
    John Harrison, Aug 24, 2003
    #5
  6. "John Harrison" <> wrote in message
    news:bi9kvg$7310m$-berlin.de...
    >
    > "Ryan D. Lucey" <> wrote in message
    > news:3f484c32$0$10454$...
    > > here's the code .. compiled using visual c++ 6.0
    > >



    // lots of code
    *snip*


    > 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<<(ostream& outStream, const testClass&

    object)
    > {
    > outStream << "Value of object: " << object.a << std::endl;
    > }
    > };
    >
    >
    > 2) declare the function in the std namespace
    >
    > namespace std
    > {
    > ostream& operator<<(ostream& , const testClass& );
    > }
    >
    > class testClass
    > {
    > ...
    > friend ostream& std::eek:perator<<(ostream& , const testClass& );
    > };
    >
    > namespace std
    > {
    > ostream& operator<<(ostream& 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
    >



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

    -Michael
     
    Michael Fawcett, Aug 24, 2003
    #6
  7. Got the service pack, all is well!
    Thanks guys.

    Ryan
    "Michael Fawcett" <> wrote in message
    news:b8_1b.79419$...
    > "John Harrison" <> wrote in message
    > news:bi9kvg$7310m$-berlin.de...
    > >
    > > "Ryan D. Lucey" <> wrote in message
    > > news:3f484c32$0$10454$...
    > > > here's the code .. compiled using visual c++ 6.0
    > > >

    >
    >
    > // lots of code
    > *snip*
    >
    >
    > > 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<<(ostream& outStream, const testClass&

    > object)
    > > {
    > > outStream << "Value of object: " << object.a << std::endl;
    > > }
    > > };
    > >
    > >
    > > 2) declare the function in the std namespace
    > >
    > > namespace std
    > > {
    > > ostream& operator<<(ostream& , const testClass& );
    > > }
    > >
    > > class testClass
    > > {
    > > ...
    > > friend ostream& std::eek:perator<<(ostream& , const testClass& );
    > > };
    > >
    > > namespace std
    > > {
    > > ostream& operator<<(ostream& 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
    > >

    >
    >
    > Visual C++ 6.0 didn't handle friends correctly until SP3. Download SP5 as
    > soon as you can.
    >
    > -Michael
    >
    >
     
    Ryan D. Lucey, Aug 24, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. steve.leach

    How standard is the standard library?

    steve.leach, Apr 18, 2005, in forum: Python
    Replies:
    1
    Views:
    394
    Christos TZOTZIOY Georgiou
    Apr 18, 2005
  2. funkyj
    Replies:
    5
    Views:
    1,134
    funkyj
    Jan 20, 2006
  3. Replies:
    5
    Views:
    869
  4. mlt
    Replies:
    2
    Views:
    852
    Jean-Marc Bourguet
    Jan 31, 2009
  5. Sriram Srinivasan
    Replies:
    13
    Views:
    569
    Benjamin Kaplan
    Nov 12, 2009
Loading...

Share This Page