Carnack Ketral
Carnack Ketral

Reputation: 3

error C2228, error C2275

I am new here. I am currently taking intermediate C++ programming at DeVry online. We are using the book C++ Primer Plus, and I have been doing okay so far. My teacher has recently thrown a bit of a curve ball at us. My current assignment is as such:

Create a Seconds class with one variable: totalSeconds (use type long). The class should have one behavior (method): convert( ). This behavior should receive the following variables by reference: days, hours, minutes, and seconds. The method should convert the totalSeconds to the equivalent time in days, hours, minutes, and seconds. Use symbolic constants in the class to represent the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.

Write a short main program that gets the totalSeconds (use type long). Then, create a Seconds object. Pass the totalSeconds to the Seconds object using the constructors or the mutators. Call the convert( ) method, sending days, hours, minutes, and seconds as arguments and by reference. Display the days, hours, minutes, and seconds in the main method. Any roads, here is my code:

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;}

time.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

I only have the two errors noted, however I can not seem to locate answers that specify my exact issues. I believe I went a little overboard in the .h file, attempting to add accessors and constants, but to no avail. most of those will be deleted.

UPDATE: After following the first answer provided I managed to solve the previous errors. However, I came across a new one. the convertToDecimal line shown below now has a different error.

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;

I have tried changing what resides in there, but I am having no luck. The only changes I have made to convertToDecimal, was that I ended it with

return convertToDecimal;  //this closed the error I was having with it

Upvotes: 0

Views: 445

Answers (1)

gigaplex
gigaplex

Reputation: 471

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

The first line doesn't make sense, you shouldn't be specifying the type 'long' in front of the arguments here. You also don't have any instances of these variables being passed to it. The error on the call to TotalSeconds fails because 'alpha' wasn't created with a valid type as a result of this problem. To get it to compile, try:

long seconds = 0, minutes = 0, hours = 0, days = 0;
BRAVO alpha(input);
alpha.convert(seconds, minutes, hours, days); // pass variables by reference

Note that what you had before isn't what your assignment asks for, you're supposed to give a single variable representing the total number of seconds, and the convert function should break it down to the various components. Change your class definitions to match.

Someone else has already commented that the

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

line doesn't make any sense since it's trying to return a type and not a variable.

Upvotes: 1

Related Questions