为什么我的计算崩溃了
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;
}
那么,您的operator <<
函数的实现在哪里?
您发布的代码不包含。您将其声明为NumDays.h
friend ostream &operator << (ostream &, NumDays &);
但是在NumDays.cpp
或其他任何地方都没有定义。
显然,链接器告诉您找不到它。我也不能。
您必须转到您的NumDays.cpp
和 Impartiment 您的操作员<<
和>>
那里
ostream &operator << (ostream &s, NumDays &n)
{
// Whatever
return s;
}
istream &operator >> (istream &s, NumDays &n);
{
// Whatever
return s;
}
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 当回溯以零开始时,如何调试崩溃
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- 在目标计算机上访问 POSIX 信号灯时出现可执行文件崩溃(SEGV_MAPERR)
- std::在循环中计算重复字符时擦除崩溃
- OpenGL因大量计算而崩溃
- 我的C 代码在尝试计算表中元素的总和时崩溃
- 创建 COM 接口指针在开发计算机上有效,但会导致其他计算机上的应用程序崩溃
- 计算向量::empty()正在使程序崩溃
- 带有Qt的程序在其他计算机上执行时崩溃
- 为什么我的计算崩溃了
- OBB在第二次通过"Assertion Failed"时崩溃的特征向量计算
- While循环在计算着色器是崩溃我的视频卡驱动程序
- C++猜数字游戏在其他计算机上崩溃并无限循环修复
- OpenGL应用程序在其他计算机上崩溃