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);
}