streaming errors

This is a discussion on streaming errors within the C++ Programming forums, part of the General Programming Boards category; can someone tell me what's wrong with my code? i'm using msvc++. the errors i get are that i can't ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    83

    streaming errors

    can someone tell me what's wrong with my code? i'm using msvc++. the errors i get are that i can't access private data types (i think the code's right) and that the << operator is ambiguous (?).

    i can't even get it to compile but i've gone through my code a dozen+ times.

    thanks,
    barneygumble742

    main.cpp
    Code:
    #include <iostream>
    #include "file.h"
    using namespace std;
    
    int main()
    {
    	BigFloat x;
    	cout <<	x	<< endl;
    	x = "-123456.9832155";
    
    return 0;
    }
    file.h
    Code:
    #ifndef BigFloat_h
    #define BigFloat_h
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    class BigFloat
    {
    public:	BigFloat();		// default constructor
    	~BigFloat();		// destructor
    	friend ostream& operator << (ostream &ostr, const BigFloat &BF);
    	void operator = (const string &s);
    
    private:string digit;
    	int DecimalPlace;
    	bool sign;
    };
    
    	ostream& operator << (ostream &ostr, const BigFloat &BF);
    
    #endif
    file.cpp
    Code:
    #include "file.h"
    
    BigFloat::BigFloat()		// initialize data members
    {
    	digit="00";
    	DecimalPlace = 1;
    	sign = true;		// sign initially set to positive
    }
    
    // need destructor
    
    ostream& operator << (ostream &ostr, const BigFloat &BF)
    {
    	ostr	<<	"digit   = "	<< BF.digit		<< endl;
    	ostr	<<	"decimal = "	<< BF.DecimalPlace	<< endl;
    	ostr	<<	"sign    = "	<< BF.sign		<< endl;
    	return ostr;
    }
    
    void BigFloat::operator =(const string &s)
    {
    	sign = true;
    	string ss = s;
    	if(ss[0] == '-')
    	{
    		sign = false;
    		ss = ss.substr(1, ss.length());
    	}
    	else
    	{
    		if(ss[0] == '+')
    		{
    			ss = ss.substr(1,ss.length());
    		}
    	}
    	int dp = ss.find('.');
    	dp = DecimalPlace;
    	DecimalPlace = ss.length() - dp;
    	digit = digit + ss.substr(dp+1, DecimalPlace);
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You should tell us the exact error message that you get.
    I have tried your code (with g++ ) and it compiles and runs ok. It is propably not doing what you expect but that is another issue.
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    here's the error message i get. i've seen this same exact code run on msvc++ and that ran ok. i have a feeling that i'm missing something very small somewhere.

    thanks,
    barneygumble742

    --------------------Configuration: main - Win32 Debug--------------------
    Compiling...
    BigFloat.cpp
    c:\file.cpp(14) : error C2248: 'digit' : cannot access private member declared in class 'BigFloat'
    c:\file.h(15) : see declaration of 'digit'
    c:\file.cpp(15) : error C2248: 'DecimalPlace' : cannot access private member declared in class 'BigFloat'
    c:\file.h(16) : see declaration of 'DecimalPlace'
    c:\file.cpp(16) : error C2248: 'sign' : cannot access private member declared in class 'BigFloat'
    c:\file.h(17) : see declaration of 'sign'
    main.cpp
    c:\main.cpp(8) : error C2593: 'operator <<' is ambiguous
    Error executing cl.exe.

    main.exe - 4 error(s), 0 warning(s)

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Ok. I pasted all your code into one file and tested that.
    You could try to put the implementation of the << operator into file.h.
    there seems to be an issue with friend. maybe that helps.
    Code:
    inline ostream& operator << (ostream &ostr, const BigFloat &BF)
    {
    	ostr	<<	"digit   = "	<< BF.digit		<< endl;
    	ostr	<<	"decimal = "	<< BF.DecimalPlace	<< endl;
    	ostr	<<	"sign    = "	<< BF.sign		<< endl;
    	return ostr;
    }
    Kurt

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    i pasted all the code into one file and tested it and i got the same error messages as before.

    i don't think any of the header files should have any implementation code; only the class.

    i think the problem has to be somewhere in th << operator implementation. once i fix the ambiguity error, then i think the other "private data" errors will be fixed.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I have split your code the same way as you did ( changed a few things ).
    compiles and runs.
    BTW the destructor was not implemented.
    main.cpp
    Code:
    #include <iostream>
    #include "bf.h"
    using namespace std;
    
    int main() {
       BigFloat x;
       cout <<	x	<< endl;
       x = "-123456.9832155";
       cout <<	x	<< endl;
       return 0;
    }
    bf.h
    Code:
    #ifndef bigfloat_included
    #define bigfloat_included
    
    #include <string>
    #include <iostream>
    
    //using namespace std; // never do that in a header
    
    class BigFloat
    {
    public:	BigFloat();	// default constructor
    	~BigFloat(){}	// destructor not doing anything
    	void operator = (const std::string &s);
    	
    	friend std::ostream& operator << (std::ostream &ostr, const BigFloat &BF);
    
    private:
            std::string digit;
    	int DecimalPlace;
    	bool sign;
    };
    
    #endif
    bf.cpp

    Code:
    #include <iostream>
    #include "bf.h"
    using namespace std;
    
    void BigFloat::operator =(const string &s)
    {
    	sign = true;
    	string ss = s;
    	if(ss[0] == '-')
    	{
    		sign = false;
    		ss = ss.substr(1, ss.length());
    	}
    	else
    	{
    		if(ss[0] == '+')
    		{
    			ss = ss.substr(1,ss.length());
    		}
    	}
    	int dp = ss.find('.');
    	//DecimalPlace=dp; // don't think this is intended
    	DecimalPlace = ss.length() - dp;
    	digit = digit + ss.substr(dp+1, DecimalPlace);
    }
    
    BigFloat::BigFloat()		// initialize data members
    {
    	digit="00";
    	DecimalPlace = 1;
    	sign = true;		// sign initially set to positive
    }
    
    std::ostream& operator << (std::ostream &ostr, const BigFloat &BF) {
    	ostr	<<	"digit   = "	<< BF.digit		<< std::endl;
    	ostr	<<	"decimal = "	<< BF.DecimalPlace	<< std::endl;
    	ostr	<<	"sign    = "	<< BF.sign		<< std::endl;
    	return ostr;
    }
    Kurt

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    hi kurt...thanks for all the help. but wouldn't
    Code:
    using namespace std;
    remove the necessity for using
    Code:
    std::
    all over the place?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,824
    You should not have "using namespace std" in your header file, but should use the std:: prefix for names in the standard namespace.

    Could you post your current version of your code and the error messages?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    the way kurt fixed it works however i've seen it done without all the extra std::

    here's my current code:

    main.cpp:
    Code:
    #include <iostream>
    #include "BigFloat.h"
    using namespace std;
    
    int main()
    {
    	BigFloat x;
    	x = "-12345.67890";
    	cout <<	x	<< endl;
    
    return 0;
    }
    bigfloat.cpp:
    Code:
    #include "BigFloat.h"
    
    BigFloat::BigFloat()		// initialize data members
    {
    	digit="00";
    	DecimalPlace = 1;
    	sign = true;		// sign initially set to positive
    }
    
    ostream& operator << (ostream &ostr, const BigFloat &BF)
    {
    	ostr	<<	"digit   = "	<< BF.digit			<< endl;
    	ostr	<<	"decimal = "	<< BF.DecimalPlace	<< endl;
    	ostr	<<	"sign    = "	<< BF.sign			<< endl;
    	return ostr;
    }
    
    void BigFloat::operator = (const string &s)
    {
    	sign = true;
    	string ss = s;
    	if(ss[0] == '-')
    	{
    		sign = false;
    		ss = ss.substr(1, ss.length());
    	}
    	else
    	{
    		if(ss[0] == '+')
    		{
    			ss = ss.substr(1,ss.length());
    		}
    	}
    	int dp = ss.find('.');
    	dp = DecimalPlace;
    	DecimalPlace = ss.length() - dp;
    	digit = digit + ss.substr(dp+1, DecimalPlace);
    }
    bigfloat.h:
    Code:
    #ifndef BigFloat_h
    #define BigFloat_h
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    class BigFloat
    {
    public:	BigFloat();		// default constructor
    		~BigFloat(){};		// destructor
    		friend ostream& operator << (ostream &ostr, const BigFloat &BF);
    		void operator = (const string &s);
    
    private:string digit;
    		int DecimalPlace;
    		bool sign;
    };
    
    
    #endif
    error messages:
    bigfloat.cpp(12) : error C2248: 'digit' : cannot access private member declared in class 'BigFloat'
    bigfloat.h(15) : see declaration of 'digit'
    bigfloat.cpp(13) : error C2248: 'DecimalPlace' : cannot access private member declared in class 'BigFloat'
    bigfloat.h(16) : see declaration of 'DecimalPlace'
    bigfloat.cpp(14) : error C2248: 'sign' : cannot access private member declared in class 'BigFloat'
    bigfloat.h(17) : see declaration of 'sign'
    main.cpp
    Error executing cl.exe.

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The code should work. Change the compiler.
    Kurt

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    yes of course. i don't know why but g++ does the trick.

    thanks kurt and to everyone else. i though sp6 for msvc++ covered all the bugs.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    i just installed sp6 for msvs and that took care of the problem. the last set of code that i posted runs perfectly on msvc++.

    thanks again everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  2. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 02:57 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  4. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 08:18 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 05:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21