How to fix C++ compiler-error "cannot convert 'Type' to 'const Type*'"?
Solution 1
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.
Solution 2
The function is expecting a pointer to 2 variables but you are passing the variables themselves, that is the issue. You fix this by passing a pointer to the variables by just passing their memory addresses using the &
operator as shown below
DetermineElapsedTime(&tm, &tm2);
Alternatively you can change the function to receive references to the variables as @iammilind suggests, which would mean you can leave the above line as it was. This would be a safer, cleaner more "C++" way.
Solution 3
Your funciton needs to take two const pointers:
int DetermineElapsedTime(const MyTime *t1, const MyTime *t2)
\_____one______/ \______two_____/
Now, here's how you are calling it:
MyTime tm, tm2;
DetermineElapsedTime(tm, tm2);
As you see you are passing the variables by value instead of passing them by a pointer as a function would expect. You can fix it in a couple of ways:
change the function to expect a const reference:
int DetermineElapsedTime(const MyTime &t1, const MyTime &t2)
take the address of the variables that are being passed:
MyTime tm, tm2; DetermineElapsedTime(&tm, &tm2);
allocate stuff dynamically and pass pointers:
MyTime *tm = new MyTime(); MyTime *tm2 = new MyTime(); DetermineElapsedTime(tm, tm2);
Solution 4
Your function DetermineElapsedTime
expects pointers to MyTime
.
Change your code to:
DetermineElapsedTime(&tm, &tm2);
The &
operator in this context means "get the address of"
user1781382
Updated on November 12, 2020Comments
-
user1781382 over 3 years
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.
-
iammilind over 11 yearsDynamic allocation might be an overkill for such problems.