提问人:slickster 提问时间:10/29/2012 最后编辑:slickster 更新时间:10/29/2012 访问量:987
为什么我的计算关闭了?
Why are my calculations off?
问:
我几乎完成了这个程序。我唯一的问题是我的计算,我猜。我的数字完全不对劲。我太新了,无法发布图像,所以这里是我输出的链接。示例:http://imageshack.us/photo/my-images/62/16902078.jpg/
NumDays.h
#ifndef NUMDAYS_H
#define NUMDAYS_H
#include <iostream>
#include <math.h>
using namespace std;
class NumDays
{
private:
double hours, days;
void calcDays();
public:
// Constructors
NumDays();
NumDays(double);
// Mutator Functions
void setHours(double);
// Accessor Functions
double getHours();
double getDays();
// Overloaded operator functions
NumDays operator + (const NumDays &); // Overloaded +
NumDays operator - (const NumDays &); // Overloaded -
NumDays operator ++ (); // Overloaded Prefix ++
NumDays operator ++ (int); // Overloaded Postfix ++
NumDays operator -- (); // Overloaded Prefix --
NumDays operator -- (int); // Overloaded Postfix --
};
#endif
NumDays.cpp
// Implementation file for the NumDays class
#include <iostream>
#include <math.h>
#include "NumDays.h"
using namespace std;
// Recalculation function
void NumDays::calcDays()
{
days = hours / 8;
}
// Default Constructor
NumDays::NumDays()
{
hours = 0;
days = 0;
}
// Constructor 1
NumDays::NumDays(double h)
{
hours = h;
days = hours * 1 / 8;
}
// Mutatory Functions
void NumDays::setHours(double h)
{
hours = h;
}
// Accessor Functions
double NumDays::getHours()
{
return hours;
}
double NumDays::getDays()
{
NumDays::calcDays();
return days;
}
// Overloaded operator functions
NumDays NumDays::operator + (const NumDays &a)
{
NumDays temp;
temp.hours = this->hours + a.hours;
return temp;
}
NumDays NumDays::operator - (const NumDays &a)
{
NumDays temp;
temp.hours = this->hours + a.hours;
return temp;
}
NumDays NumDays::operator ++ ()
{
++hours;
calcDays();
return *this;
}
NumDays NumDays::operator -- ()
{
--hours;
calcDays();
return *this;
}
NumDays NumDays::operator ++ (int)
{
NumDays temp(*this);
++hours;
return temp;
}
NumDays NumDays::operator -- (int)
{
hours--;
calcDays();
return *this;
}
main.cpp
#include <iostream>
#include "NumDays.h"
using namespace std;
int main()
{
double hours1, hours2;
//Prompt for the data for the first 2 objects
cout << "Enter the number of hours for the the object called One: ";
cin >> hours1;
cout << "Enter the number of hours for the the object called Two: ";
cin >> hours2;
// Define two objects of WorkHours
NumDays one(hours1), two(hours2);
cout << "One: " << one.getDays() << " day(s)" << endl;
cout << "Two: " << two.getDays() << " day(s)" << endl << endl;
// Demonstrate addition and subtraction operators
cout << "Three = One + Two: " << (one - two).getDays() << " day(s)" << endl;
cout << "One - Two: " << (one - two).getDays() << " day(s)" << endl << endl;
// Define a third and fourth object to be used for further operator demonstrations
NumDays three(one + two), four;
// Demonstrate increment and decrement operators
four = three++;
cout << "Four = Three++ " << endl;
cout << " Three: " << three.getDays() << " day(s)" << endl;
cout << " Four: " << four.getDays() << " day(s)" << endl << endl;
four = ++three;
cout << "Four = ++Three: " << endl;
cout << " Three: " << three.getDays() << " day(s)" << endl;
cout << " Four: " << four.getDays() << " day(s)" << endl << endl;
four = three--;
cout << "Four = Three--: " << endl;
cout << " Three: " << three.getDays() << " day(s)" << endl;
cout << " Four: " << four.getDays() << " day(s)" << endl << endl;
four = --three;
cout << "Four = --Three: " << endl;
cout << " Three: " << three.getDays() << " day(s)" << endl;
cout << " Four: " << four.getDays() << " day(s)" << endl;
system("pause");
return 0;
}
答:
2赞
AnT stands with Russia
10/29/2012
#1
那么,您的函数在哪里实现呢?operator <<
您发布的代码不包含任何内容。您将其声明为好友NumDays.h
friend ostream &operator << (ostream &, NumDays &);
但是无论是在国内还是在其他任何地方都没有定义。NumDays.cpp
显然,链接器告诉你它找不到它。我也不能。
你必须去你的并实施你的操作员,然后在那里NumDays.cpp
<<
>>
ostream &operator << (ostream &s, NumDays &n)
{
// Whatever
return s;
}
istream &operator >> (istream &s, NumDays &n);
{
// Whatever
return s;
}
评论
0赞
slickster
10/29/2012
好的,所以我在 NumDays.h 中添加了这些行:类 NumDays;forward 声明 //重载流算子的函数原型 ostream & 运算符 << (ostream &, const NumDays &);istream & 运算符 >> (istream &, NumDays &);它仍然给出相同的错误。不知道还能做什么。我也不确定如何在这个网站上格式化回复,所以如果可能的话,我深表歉意。
0赞
dusan
10/29/2012
@slickster,您始终可以编辑您的问题以添加更多详细信息
0赞
slickster
10/29/2012
我做了。我编辑了我的原始帖子以反映我的更改。谢谢。
1赞
AnT stands with Russia
10/29/2012
@slickster:我不明白你为什么要这样做。您添加了额外的声明。您不需要更多声明。你已经受够了。你需要的是定义。你必须定义这些运算符,即你必须实现它们。作为函数。你必须给他们身体,让他们真正做你想让他们做的事情。您已经为 中的所有操作员完成了此操作。只需为和操作员做同样的事情,仅此而已。NumDays.cpp
<<
>>
0赞
AnT stands with Russia
10/29/2012
@slickster:您最新添加的内容完全没有必要。你的原始形式完全没问题。您只需要定义 和 .它可能应该在NumDays.h
NumDays.h
<<
>>
NumDays.cpp
评论