user3264198
user3264198

Reputation: 13

Class Function Not declared in this scope?

i'm getting some errors in a function i addedi n a class, here is my build log:

-------------- Build: Debug in Lab1 (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -g  -c C:\Users\Dekkiller\Documents\clockType.cpp -o    
obj\Debug\clockType.o
C:\Users\Dekkiller\Documents\clockType.cpp: In function 'clockType operator+(const 
clockType&, const clockType&)':
C:\Users\Dekkiller\Documents\clockType.cpp:51:21: error: 'incrementhr' was not declared
in this scope
     incrementhr();
                 ^
C:\Users\Dekkiller\Documents\clockType.cpp:54:22: error: 'incrementmin' was not declared 
in this scope
     incrementmin();
                  ^
C:\Users\Dekkiller\Documents\clockType.cpp:59:1: warning: control reaches end of non-
void function [-Wreturn-type]
 }
 ^
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 1 warning(s) (0 minute(s), 0 second(s))

Here is my main cpp code:

#include <iostream>
#include "clockType.h"
using namespace std;


int main()
{
clockType c1(15, 45, 30), c2(3, 20);  // hour, min, sec
cout << "c1 is " << c1;   // add whatever to beautify it
cout << "c2 is " << c2;
cout << "c1+c2 is " <<  c1+c2;
c2 = c1+c1;
cout << "c1+c1 is " << c2;
}

here is my header class file:

#ifndef CLOCKTYPE_H
#define CLOCKTYPE_H
#include <iostream>


class clockType
{
friend std::ostream& operator<<(std::ostream& os, const clockType& out);
friend clockType operator+(const clockType& one, const clockType& two);
public:
    clockType();
    clockType(int hours, int minutes, int seconds);
    clockType(int hours, int minutes);
    void setTime(int hours, int minutes, int seconds);
    void incrementhr();
    void incrementmin();
    void incrementsec();
private:
    int hrs;
    int mins;
    int secs;
};

#endif // CLOCKTYPE_H

Here is my cpp class file:

#include "clockType.h"
#include <iostream>
using namespace std;

clockType::clockType()
{
hrs = 0;
mins = 0;
secs = 0;
}

clockType::clockType(int hours, int minutes, int seconds)
{
 setTime(hours, minutes, seconds);
}

clockType::clockType(int hours, int minutes)
{
hrs = hours;
mins = minutes;
secs = 0;
}

void clockType::setTime(int hours, int minutes, int seconds)
{
if(0<=hours && hours<24)
    hrs=hours;
else
    hrs=0;
if(0<=minutes && minutes<60)
    mins=minutes;
else
    mins=0;
if(0<=seconds && seconds<60)
    secs=seconds;
else
    secs=0;
}

ostream& operator<<(ostream& os, const clockType& out)
{
os << "Hour is " << out.hrs << "Minute is " << out.mins << "Seconds is " << out.secs;
return os;
}

clockType operator+(const clockType& one, const clockType& two)
{
clockType three;
three.hrs = one.hrs + two.hrs;
if(three.hrs>23)
    incrementhr();
three.mins = one.mins + two.mins;
if(three.mins>59)
    incrementmin();
three.secs = one.secs + two.secs;
if(three.secs>59)

return three;
}

void clockType::incrementhr()
{
hrs++;
if(hrs > 23)
    hrs=0;
}

void clockType::incrementmin()
{
mins++;
if(mins>59)
{
 mins=0;
 incrementhr();
}
}

void clockType::incrementsec()
{
secs++;
if(secs>59)
{
    secs=0;
    incrementmin();
}
}

Any help is appreciated, i've been working on this program for a while but can't seem to get it to fully work, maybe i am implementing the increment functions incorrectly i think but i can't think of how else or how i could fix this incrementing.

Upvotes: 0

Views: 809

Answers (1)

clockType operator+(const clockType& one, const clockType& two)
{
clockType three;
three.hrs = one.hrs + two.hrs;
if(three.hrs>23)
    incrementhr();

Friend functions are not members of the type that befriends them. In this case the operator is a free function. The member function incrementhr is a member function, and to call that you need an object. You probably meant: three.incrementhr()?

Upvotes: 2

Related Questions