1

I am new to learning C++. I tried to create a code that is meant to tell time and add minutes to it by using a constructor. However, upon building the code with CodeBlocks, an error is displayed under the scope resolution 'time::time()'.

I tried creating parameters within the constructor but to no avail.

#include<iostream>
using namespace std;
class time
{
private:
    int hours,minutes;
public:
    time(int);
    ~time();
    void enterTime();
    void addMinutes();
    void displayTime();
};
time::time()
{
    int timehours;
    int timeminutes;
    cout<<"Enter hours: "<<endl;
    cin>>timehours;
    if(timehours<00 && timehours>23)
    {
        cout<<"Invalid Entry of time. Try again."<<endl;
        time();
    }
    else
        timehours==00;
    cout<<"Enter minutes: "<<endl;
    cin>>timeminutes;
    if(timeminutes<0 && timeminutes>59)
    {
        cout<<"Invalid Entry of time. Try again."<<endl;
        time();
    }
    else
        timeminutes==00;
    cout<<"Initial Time: "<<timehours<<": "<<timeminutes<<endl;
}
void time::enterTime()
{
    cout<<"Enter hours: "<<endl;
    cin>>hours;
    if(hours<00 && hours>23)
    {
        cout<<"Invalid entry of time. Try Again."<<endl;
        enterTime();
    }
    cout<<"Enter minutes: "<<endl;
    cin>>minutes;
    if(minutes<00)
    {
        cout<<"Invalid entry of time. Try Again,"<<endl;
        enterTime();
    }
    cout<<"Initial Time:- "<<hours<<": "<<minutes<<endl;
}
void time::addMinutes()
{
    if(minutes>60 && minutes%60>0)
    {
        int addhours,addMin;
        addMin=(minutes%60>0);
        addhours=(minutes/60);
        hours+=addhours;
        if(hours>=24)
            hours-=24;
    }
}
void time::displayTime()
{
    cout<<"Updated time:"<<hours<<" :"<<minutes<<" "<<endl;
}
int main()
{
    int n;
    for(n=0;n<4;n++)
    {
        time Time[4];
        if((n+1)%2==0)
        {
            Time[n].time();
            Time[n].addMinutes();
            Time[n].displayTime();
        }
        else
        {
            Time[n].enterTime();
            Time[n].addMinutes();
            Time[n].displayTime();
        }
    }
    return 0;
}

2 Answers2

2

There are two problems with your code as described below:


Problem 1

The first problem is that you're trying to define the default constructor outside the class while you have not declared it inside the class. Remember that in order to define any member function outside the class, its corresponding declaration must be there inside the class first.

So to solve this remove the ~ which actually made that declaration a destructor declaration, as shown below:

class time
{
private:
    int hours,minutes;
public:
    time(int);
//-v---------------------->removed ~ from here
    time();

    //other code as before
};

Problem 2

Additionally avoid using using namespace std; so that there is no conflict between names of user defined classes and standard library names. Why is "using namespace std;" considered bad practice?

Jason
  • 36,170
  • 5
  • 26
  • 60
2

You have a conflict between the class time, and the function time from the standard library.

Here

time Time[4];

the compiler expects a parameter for the function call.

This is one reason for avoiding using namespace std.

BoP
  • 2,310
  • 1
  • 15
  • 24
  • That has nothing to do with the reported error mentioned by op. Though OP will eventually need to resolve it as well. – Jason Feb 26 '23 at 13:15