gpt4 book ai didi

c++ - 错误 C2228、错误 C2275

转载 作者:行者123 更新时间:2023-11-28 07:22:27 27 4
gpt4 key购买 nike

我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:

创建一个带有一个变量的 Seconds 类:totalSeconds(使用 long 类型)。该类应该有一种行为(方法):convert()。此行为应通过引用接收以下变量:天、小时、分钟和秒。该方法应将 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;
}

时间.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

error C2275: 'BRAVO' : illegal use of this type as an expression
error C2228: left of '.Totalseconds' must have 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

请注意,您之前拥有的不是您的作业所要求的,您应该提供一个代表总秒数的变量,并且转换函数应该将其分解为各种组件。更改您的类定义以匹配。

其他人已经评论过

return BRAVO; //this gets error c2275: 'BRAVO' : illegal use of this type as an expression

line 没有任何意义,因为它试图返回一个类型而不是一个变量。

关于c++ - 错误 C2228、错误 C2275,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19216579/

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