use*_*382 5 c++ constructor pointers compiler-errors
这是完整的错误消息:
错误:无法将'MyTime'转换为'const MyTime*'以将参数'1'转换为'int DetermineElapsedTime(const MyTime*,const MyTime*)'|
这是我的代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以解决吗?请随时指出您看到的任何其他错误.我知道修复DetermineTimeElapsed以正确输出hr:min:sec格式.但是现在我需要克服这个问题.
错误应该在以下行:
DetermineElapsedTime(tm, tm2);
Run Code Online (Sandbox Code Playgroud)
您正在将MyTime
对象传递给上述函数const MyTime*
.
通过传递对象地址来修复它:
DetermineElapsedTime(&tm, &tm2);
Run Code Online (Sandbox Code Playgroud)
或者更好的C++方式:通过更改函数原型来接受对象引用:
int DetermineElapsedTime(const MyTime &t1, const MyTime &t2);
Run Code Online (Sandbox Code Playgroud)
身体也会相应改变; 例如->
将被.
操作员等替换.