Unsure why I'm getting this error

This is a discussion on Unsure why I'm getting this error within the C++ Programming forums, part of the General Programming Boards category; I am new to learning classes/constructors/etc... and between the two programs that I have written thus far that use classes ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    15

    Unsure why I'm getting this error

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

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    The constructor with two ref to ints
    Code:
    MyTime::MyTime(int& time1, int& time2){
     
     
      time1 = hours;
      time2 = minutes;
     
     
      simplify();
     
     
    }
    I would suggest you to change the prototype to have as 1st and 2nd arguments just ints
    Then in the body, you want the assignment to happen in the opposite way. You want the arguments to be assigned into the private fields of the class, thus the body needs a bit of modification.
    In code
    Code:
    MyTime::MyTime(int time1, int time2){
     
     
      hours = time1;
      minutes = time2;
     
     
      simplify();
     
     
    }
    Modify the prototypes of add_hour and add_minute likewise

    Learn to understand what the debugger is saying to you
    Code:
    main.cc: In function 'int main()':main.cc:7:23: error: no matching function for call to 'MyTime::MyTime(int, int)'
    It locates the error and says to you : You are asking for something that is not written in the code.
    However it had some almost matches, so it says to you what are your choices are depending on the code present at the compilation time. Candidates are your choices. The compiler knows only about the candidates. Everything else is just unknown to him (no matching found! )
    Code:
    MyTime.h:9:3: note: candidates are: MyTime::MyTime(int&, int&)
    This candidate has as arguments two ref to ints
    Code:
    MyTime.h:8:3: note:                 MyTime::MyTime()
    By default, it will accept no arguments at all (default constructor that you have overloaded. Even if you hadn't, this would still be a candidate.
    Code:
    MyTime.h:4:13: note:                 MyTime::MyTime(const MyTime&)
    Copy constructor.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    15
    that did it, thanks man
    Last edited by pepperlizard; 11-29-2012 at 05:00 PM.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by pepperlizard View Post
    that did it, thanks man
    Welcome

    But keep that in mind, debugger is your friend
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,305
    Quote Originally Posted by std10093 View Post
    This candidate has as arguments two ref to ints.
    and it's not possible to convert an int literal to a non-const int reference, so therefore you get the error.
    on the other hand, if the function had been declared like so:
    Code:
    MyTime::MyTime(const int&, const int&)
    it would have worked fine. the compiler can then automatically generate a temporary int.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by std10093 View Post
    By default, it will accept no arguments at all (default constructor that you have overloaded. Even if you hadn't, this would still be a candidate.
    The rest of your post was great, but with the above sentence are you thinking that it would generate the default (zero argument) constructor if it was not provided?

    If so, you should probably be made aware that the compiler only auto-generates that when you don't provide an alternative constructor with arguments. So in this case the (int, int) constructor prevents it, and so it could not be a valid candidate without being explicitly provided.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    @Elvkis : I admit that I had a doubt about it. Thank you for reminding me the truth

    @iMalc : Thanks for the nice words . I admit that I was not aware of that and you understood well.

    proof
    Code:
    #include <iostream>
    
    using namespace std;
    
    class A {
    private:
            int x;
    public:
            A(int arg) { x = arg*2; }
    
            void setX(int arg) { x = arg; }
    
            void print(void) { cout<<x<<endl; }
    };
    
    int main()
    {
            A a;
            a.setX(5);
            a.print();
    
            return 0;
    }
    error
    Code:
    Macintosh-c8bcc88e5669-9:~ usi$ g++ -Wall px.cpp -o px
    px.cpp: In function ‘int main()’:
    px.cpp:18: error: no matching function for call to ‘A::A()’
    px.cpp:9: note: candidates are: A::A(int)
    px.cpp:5: note:                 A::A(const A&)
    I really had forgotten what Elkvis pointed out and I bet that I did not know what iMalc said. Bravo to both. Thank you very very much.

    PS - Very glad to be in the same forum with scientists like you ( and several others )
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-11-2010, 01:00 PM
  2. Unsure how to fix this error
    By SniperSAS in forum Game Programming
    Replies: 5
    Last Post: 08-21-2005, 04:46 PM
  3. unsure
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-13-2002, 09:37 AM
  4. Little Unsure?
    By kas2002 in forum C Programming
    Replies: 8
    Last Post: 06-12-2002, 08:18 PM
  5. Help unsure
    By Joel in forum C++ Programming
    Replies: 1
    Last Post: 02-12-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21