[英]error C2228, error C2275
我刚来这地方。 我目前正在DeVry在线上进行中级C ++编程。 我们正在使用《 C ++ Primer Plus》这本书,到目前为止我做得还不错。 我的老师最近向我们扔了一个弯弯的球。 我目前的工作是这样的:
创建一个具有一个变量的Seconds类:totalSeconds(使用long类型)。 该类应具有一种行为(方法):convert()。 此行为应通过引用接收以下变量:天,小时,分钟和秒。 该方法应将totalSeconds转换为以天,小时,分钟和秒为单位的等效时间。 在类中使用符号常量表示一天中的小时数,一小时中的分钟数和一分钟中的秒数。
编写一个简短的主程序,以获取totalSeconds(使用long类型)。 然后,创建一个Seconds对象。 使用构造函数或修改器将totalSeconds传递给Seconds对象。 调用convert()方法,将天,小时,分钟和秒作为参数并通过引用发送。 在主要方法中显示天,小时,分钟和秒。 任何道路,这是我的代码:
main.cpp
/*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;
}
time.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;}
时间
// 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”的左侧必须具有class / struct / union
我只注意到了两个错误,但是我似乎找不到指定我确切问题的答案。 我相信我在.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
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
第一行没有意义,您不应在此处的参数前面指定类型“ long”。 您也没有任何这些变量的实例传递给它。 调用TotalSeconds的错误失败,因为此问题导致未使用有效类型创建“ alpha”。 要使其编译,请尝试:
long seconds = 0, minutes = 0, hours = 0, days = 0;
BRAVO alpha(input);
alpha.convert(seconds, minutes, hours, days); // pass variables by reference
请注意,您以前拥有的不是您的分配要求的,您应该给一个代表总秒数的变量,convert函数应将其分解为各个组件。 更改您的类定义以匹配。
有人已经评论说
return BRAVO; //this gets error c2275: 'BRAVO' : illegal use of this type as an expression
line没有任何意义,因为它试图返回类型而不是变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.