簡體   English   中英

錯誤C2228,錯誤C2275

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM