读书人

各位好心人能否帮小弟我修改一下

发布时间: 2012-05-20 16:03:12 作者: rapoo

各位好心人能否帮我修改一下?
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

#include "decimal.h"

int main()
{
Decimal test1;
Decimal test2;
Decimal test3( 1.234 );

cout << "Initial values:\n"
<< test1 << endl << test2 << endl << test3
<< endl << endl;

cin >> test1 >> test2;

cout << "The sum of test1 and test2 is: "
<< test1 + test2 << endl;
test3 += ++test2;

cout << "\nfinal values:\n"
<< "test1 = " << test1 << endl
<< "test2 = " << test2 << endl
<< "test3 = " << test3 << endl;

if ( test1 != test3 )
cout << "test1 and test3 are not equal to each other\n";

return 0;
#include <iostream>

using std::cout;
using std::cin;

#include <cmath>

#include "decimal.h"

// constructor
Decimal::Decimal( double n )
{
decimal = modf( n, &integer );

} // end class Decimal constructor

// function operator<< definition
friend ostream & operator<<( const Decimal &d )
{
double n = 0;

n = floor( d.decimal * 100 );

if ( n < 0 )
n = 0 - dec;

if ( d.decimal != 0 ) {
output << floor( d.integer ) << ".";

if ( n > 10 )
output << n;

else
output << "0" << n;

#ifndef DECIMAL_H
#define DECIMAL_H

#include <iostream>

using std::ostream;
using std::istream;

// class Decimal definition
class Decimal {

public:
friend istream operator>>( istream &, const Decimal & );
Decimal( double = 0.0 );

void setInteger( double );
void setDecimal( double );

Decimal &operator=( const Decimal );
Decimal +( Decimal );
Decimal +=( Decimal ) const;
Decimal &operator++();
Decimal operator++( double );
bool operator==( const Decimal );

private:
friend ostream &operator<<( const Decimal & );
double integer;
double decimal;

}; // end class Decimal

#endif // DECIMAL_H



[解决办法]
你的代码,可以使用 code 标签,
这样子,你的代码看起来,舒服很多,会有更多人帮忙解决问题

C/C++ code
#include <iostream>using std::cout;using std::endl;using std::cin;#include "decimal.h"int main(){  Decimal test1;    Decimal test2;  Decimal test3( 1.234 );  cout << "Initial values:\n"  << test1 << endl << test2 << endl << test3   << endl << endl;  cin >> test1 >> test2;  cout << "The sum of test1 and test2 is: "  << test1 + test2 << endl;  test3 += ++test2;    cout << "\nfinal values:\n"  << "test1 = " << test1 << endl  << "test2 = " << test2 << endl  << "test3 = " << test3 << endl;  if ( test1 != test3 )  cout << "test1 and test3 are not equal to each other\n";  return 0;#include <iostream>using std::cout;using std::cin;#include <cmath>#include "decimal.h"// constructorDecimal::Decimal( double n ){  decimal = modf( n, &integer );} // end class Decimal constructor// function operator<< definitionfriend ostream & operator<<( const Decimal &d ){  double n = 0;  n = floor( d.decimal * 100 );  if ( n < 0 )  n = 0 - dec;  if ( d.decimal != 0 ) {  output << floor( d.integer ) << ".";  if ( n > 10 )  output << n;  else  output << "0" << n;#ifndef DECIMAL_H#define DECIMAL_H#include <iostream>using std::ostream;using std::istream;// class Decimal definitionclass Decimal {public:  friend istream operator>>( istream &, const Decimal & );  Decimal( double = 0.0 );  void setInteger( double );  void setDecimal( double );  Decimal &operator=( const Decimal );  Decimal +( Decimal );  Decimal +=( Decimal ) const;  Decimal &operator++();  Decimal operator++( double );  bool operator==( const Decimal );private:  friend ostream &operator<<( const Decimal & );  double integer;  double decimal;}; // end class Decimal#endif // DECIMAL_H 


[解决办法]
1.函数申明后没有定义!
2.Decimal operator++( double );函数申明形式错误,++原则上只能用于int型,所以()内的形参应该是int
[解决办法]
额,楼上我咋感觉他写的是类的++运算符重载呢。第二个应该是没有问题的!~
令:我不是要抬杠的啊……

读书人网 >C++

热点推荐