Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix C++ compiler-error "cannot convert 'Type' to 'const Type*'"?

This is the complete error message:

error: cannot convert 'MyTime' to 'const MyTime*' for argument '1' to 'int DetermineElapsedTime(const MyTime*, const MyTime*)'|

And this is my code:

#include <iostream>
#include<cstdlib>
#include<cstring>

using namespace std;
struct MyTime { int hours, minutes, seconds; };
int DetermineElapsedTime(const MyTime *t1, const MyTime *t2);
const int hourSeconds = 3600;
const int minSeconds = 60;

int DetermineElapsedTime(const MyTime *t1, const MyTime *t2)
{
    long timeDiff = ((((t2->hours * hourSeconds) + (t2->minutes * minSeconds) + t2->seconds) -
                   ((t1->hours * hourSeconds) + (t1->minutes * minSeconds) + t1->seconds)));
    return(timeDiff);
}


int main(void)
{
    char delim1, delim2;
    MyTime tm, tm2;
    cout << "Input two formats for the time. Separate each with a space. Ex: hr:min:sec\n";
    cin >> tm.hours >> delim1 >> tm.minutes >> delim2 >> tm.seconds;
    cin >> tm2.hours >> delim1 >> tm2.minutes >> delim2 >> tm2.seconds;

    DetermineElapsedTime(tm, tm2);

    return 0;

}

Is there any way that I can fix? Please feel free to point out any other errors that you see. I do know about fixing DetermineTimeElapsed to properly output the hr:min:sec format. but right now I need to get past this.

like image 786
user1781382 Avatar asked Nov 12 '12 06:11

user1781382


1 Answers

The error should be at below line:

DetermineElapsedTime(tm, tm2);

You are passing MyTime objects to the above function when it expects const MyTime*.

Fix it by either passing the object addresses:

DetermineElapsedTime(&tm, &tm2);

Or better C++ way: by changing the function prototype to accept object references:

int DetermineElapsedTime(const MyTime &t1, const MyTime &t2);

the body also will change accordingly; e.g. -> will be replaced by . operator and so on.

like image 102
iammilind Avatar answered Nov 14 '22 22:11

iammilind