我刚来这地方。我目前在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我做得还不错。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
创建一个具有一个变量的 Seconds 类:totalSeconds(使用 long 类型)。该类应该有一种行为(方法):convert()。此行为应通过引用接收以下变量:天、小时、分钟和秒。该方法应将 totalSeconds 转换为以天、小时、分钟和秒为单位的等效时间。在类中使用符号常量来表示一天的小时数、一小时的分钟数和一分钟的秒数。
编写一个简短的主程序来获取 totalSeconds(使用 long 类型)。然后,创建一个 Seconds 对象。使用构造函数或修改器将 totalSeconds 传递给 Seconds 对象。调用 convert() 方法,将天、小时、分钟和秒作为参数和引用发送。在 main 方法中显示天、小时、分钟和秒。任何道路,这是我的代码:
主文件
/*GSP 125 Intmed Prgrmg C++/OOP main.cpp*/
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include "sec.h"
using namespace std;
int input = 0;
int main()
{
system("TITLE Tick Tack");
char choice1;
ofstream fout;
char filename[50];
cout << "Save results to file ? (Y/N) : ";
(cin >> choice1).get();//make choice, Y or N
if (toupper(choice1) == 'Y')
{
cout << "Enter filename(max 50 characters): ";
cin.getline(filename,50);//string for file name
fout.open(filename);//makes text file with chosen name
}
else
cout << "Results will not be saved!\n\n";
// ToDo: add your code here
cout << "Enter the number of seconds:";
long input;
cin >> input;
BRAVO alpha(long seconds,long minutes,long hours,long days,long years);
long breakdown = alpha.Totalseconds(); //this gets error C2228: left of '.Totalseconds' must have class/struct/union
cout << "\n\n" << input << " seconds = \n"<< breakdown << endl;
cout << year << day_remain << hour_remain << min_remain << seconds << endl;
// pause
cout << "\nPress any key to continue...";
cin.sync();//clearscreen
_getch();//waitkey
// return environment variable
return 0;
}
时间.cpp
// time.cpp
#include "time.h"
// constructors
BRAVO::BRAVO(void)
{
seconds = 0;
minutes = 0;
hours = 0;
days = 0;
years = 0;
}
BRAVO::BRAVO( long seconds, long minutes, long hours, long days, long years)
{
seconds = seconds;
minutes = minutes;
hours = hours;
days = days;
years = years;
}
// destructor
BRAVO::~BRAVO(void)
{}
// behaviors
double BRAVO::Totalseconds(void)
{
long input = 0;
//convert to minutes
long min = input / SecPerMin;
int sec = input % SecPerMin;
//convert to hours
long hour = min / MinPerHour;
int min_remain = min % MinPerHour;
//convert to days
long day = hour / HourPerDay;
int hour_remain = hour % HourPerDay;
//convert to years
long year = day / DaysPerYear;
int day_remain = day % DaysPerYear;
return BRAVO; //this gets error c2275: 'BRAVO' : illegal use of this type as an expression
}
// accessors and mutators
short BRAVO::getseconds(void)
{return seconds;}
void BRAVO::setseconds( long seconds )
{seconds = seconds;}
short BRAVO::getminutes(void)
{return minutes;}
void BRAVO::setminutes( long minutes )
{minutes = minutes;}
short BRAVO::gethours(void)
{return hours;}
void BRAVO::sethours( long hours )
{hours = hours;}
short BRAVO::getdays(void)
{return days;}
void BRAVO::setdays( long days )
{days = days;}
short BRAVO::getyears(void)
{return years;}
void BRAVO::setyears( long years )
{years = years;}
时间.h
// sec.h
#ifndef BRAVO_H_
#define BRAVO_H_
#include <iostream>
// global constants
const int DaysPerYear = 365;
const int HourPerDay = 24;
const int MinPerHour = 60;
const int SecPerMin = 60;
long int seconds = 0;
long int minutes=0;
long int hours=0;
long int days=0;
long int years=0;
int min_remain=0;
int hour_remain=0;
int day_remain=0;
int year=0;
// Class definition
class BRAVO
{
private:
// accessors
short seconds;
short minutes;
short hours;
short days;
short years;
public:
// constructors
BRAVO(void);
BRAVO(long seconds, long minutes, long hours, long days, long years);
// destructor
~BRAVO(void);
// behaviors
double Totalseconds();
// accessors and mutators
short getseconds(void);
void setseconds( long seconds );
short getminutes(void);
void setminutes( long minutes );
short gethours(void);
void sethours( long hours );
short getdays(void);
void setdays( long days );
short getyears(void);
void setyears( long years );
short getmin_remain(void);
void setmin_remain( long min_remain );
short gethour_remain(void);
void sethour_remain( long hour_remain );
short getday_remain(void);
void setday_remain( long day_remain );
};
#endif
错误 C2275:“BRAVO”:非法使用此类型作为表达式
错误 C2228:“.Totalseconds”左侧必须有类/结构/联合
我只记录了两个错误,但是我似乎无法找到指定我确切问题的答案。我相信我在 .h 文件中有点过火,试图添加访问器和常量,但无济于事。其中大部分将被删除。
更新:按照提供的第一个答案后,我设法解决了以前的错误。然而,我遇到了一个新的。下面显示的 convertToDecimal 行现在有不同的错误。
long seconds = 0, minutes = 0, hours = 0, days = 0;
BRAVO alpha(input);
float breakdown = alpha.convertToDecimal(seconds, minutes, hours, days); // pass variables by reference
//gets error C2660: 'BRAVO::convertToDecimal' : function does not take 4 arguments
cout << "\n\n" << input << " seconds = \n" << breakdown << endl;
cout << year << day_remain << hour_remain << min_remain << seconds << endl;
我试过改变那里的东西,但我没有运气。我对 convertToDecimal 所做的唯一更改是我以
return convertToDecimal; //this closed the error I was having with it