gpt4 book ai didi

c++ - 为什么我的计算出错了?

转载 作者:行者123 更新时间:2023-11-28 03:29:04 24 4
gpt4 key购买 nike

我几乎完成了这个程序。我想我唯一的问题是我的计算。我的号码完全关闭。我太新了,无法发布图片,所以这里是我的输出链接。示例: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;
}

主要.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实现您的运营商<<>>那里

ostream &operator << (ostream &s, NumDays &n)
{
// Whatever
return s;
}

istream &operator >> (istream &s, NumDays &n);
{
// Whatever
return s;
}

关于c++ - 为什么我的计算出错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13114854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com