I am new to learning classes/constructors/etc... and between the two programs that I have written thus far that use classes and everything that goes in them I have been getting the same error and cannot figure out how to solve it. The error for the code below reads:
Code:
main.cc: In function 'int main()':main.cc:7:23: error: no matching function for call to 'MyTime::MyTime(int, int)'
MyTime.h:9:3: note: candidates are: MyTime::MyTime(int&, int&)
MyTime.h:8:3: note: MyTime::MyTime()
MyTime.h:4:13: note: MyTime::MyTime(const MyTime&)
main.cc:28:18: error: no matching function for call to 'MyTime::add_hours(int)'
MyTime.h:13:7: note: candidate is: int MyTime::add_hours(int&)
main.cc:33:20: error: no matching function for call to 'MyTime::add_minutes(int)'
MyTime.h:14:7: note: candidate is: int MyTime::add_minutes(int&)
Can anyone point me to where the problem is and how to go about fixing it?
Code:
Code:
#include <iostream>
#include "MyTime.h"
using namespace std;
int main(){
MyTime t1, t2(1, 115);
cout << "Testing the constructors \n\n";
cout << "Testing the constructor with two arguments " << t2 << endl << endl;
cout << "Testing the default constructor " << t1 << endl << endl;
cout << "Testing the overloaded >> operator \n";
cout << "Enter the time you spent on the porject today ";
cin >> t2;
cout << "The time you entered is " << t2 << endl << endl;
cout << t1 << " + " << t2 << " = " << t1 + t2 << endl << endl;
cout << t1 << " - " << t2 << " = " << t1 - t2 << endl << endl;
cout << "Maybe I should spend twice as much today \n"
<< t2 << " * " << 2 << " = " << t2 * 2 << endl << endl;
cout << "Maybe I should add 2 hours to today \n"
<< t2 << " + " << 2 << " = ";
t2.add_hours(30);
cout << t2 << endl << endl;
cout << "Maybe I should add 72 minutes to today as well \n"
<< t2 << " + " << 72 << " = ";
t2.add_minutes(72);
cout << t2 << endl << endl;
return 0;
}
Code:
#include <iostream>using namespace std;
class MyTime{
public:
// constructors
MyTime(); // initialize the private member variables to zero
MyTime (int& time1, int& time2);
// mutator
void set_time(int time1, int time2);
int add_hours(int& num1);
int add_minutes(int& num2);
// accessors
int get_time1()const;
int get_time2()const;
friend istream& operator >> (istream& ins, MyTime t1);
friend ostream& operator << (ostream& fout, MyTime t1);
friend MyTime operator + (const MyTime& t1, const MyTime& t2);
friend MyTime operator - (const MyTime& t1, const MyTime& t2);
friend MyTime operator * (const MyTime& t1, int num);
private:
int minutes;
int hours;
void simplify();
};
Code:
#include <iostream>#include <cstdlib>
#include "MyTime.h"
using namespace std;
MyTime::MyTime(){
hours = 0;
minutes = 0;
simplify();
}
MyTime::MyTime(int& time1, int& time2){
time1 = hours;
time2 = minutes;
simplify();
}
void MyTime::set_time(int time1, int time2){
hours = 0;
minutes = 0;
simplify();
}
int MyTime::get_time1()const{
return hours;
}
int MyTime::get_time2()const{
return minutes;
}
istream& operator >> (istream& ins, MyTime t1){
ins >> t1.hours >> t1.minutes;
t1.simplify();
return (ins);
}
ostream& operator << (ostream& fout, MyTime t1){
fout << t1.hours << ":" << t1.minutes;
t1.simplify();
return (fout);
}
void MyTime::simplify(){
int tmp1, tmp2;
if (minutes >= 60){
tmp1 = minutes / 60;
tmp2 = minutes % 60;
hours += tmp1;
minutes = tmp2;
}
}
int MyTime::add_hours(int& num1){
hours += num1;
return (hours);
}
int MyTime::add_minutes(int& num2){
minutes += num2;
return (minutes);
}
MyTime operator + (const MyTime& t1, const MyTime& t2){
MyTime temp;
temp.hours = t1.hours + t2.hours;
temp.minutes = t1.minutes + t2.minutes;
temp.simplify();
return (temp);
}
MyTime operator - (const MyTime& t1, const MyTime& t2){
MyTime temp;
temp.hours = t1.hours - t2.hours;
temp.minutes = t1.minutes - t2.minutes;
temp.simplify();
return (temp);
}
MyTime operator * (const MyTime& t1, int num){
MyTime temp;
temp.hours = t1.hours * num;
temp.minutes = t1.minutes * num;
temp.simplify();
return (temp);
}