如何修复 C++ 编译器错误“无法将‘Type’转换为‘const Type*’”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13339260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:12:26  来源:igfitidea点击:

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

c++pointersconstructorcompiler-errors

提问by user1781382

This is the complete error message:

这是完整的错误消息:

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

错误:无法将参数 '1' 的 'MyTime' 转换为 'const MyTime*' 到 'int DescribeElapsedTime(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.

有什么办法可以解决吗?请随时指出您看到的任何其他错误。我知道如何修复确定时间已过以正确输出 hr:min:sec 格式。但现在我需要解决这个问题。

回答by iammilind

The error should be at below line:

错误应该在下面一行:

DetermineElapsedTime(tm, tm2);

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

您正在将MyTime对象传递给上述函数,因为它需要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:

或者更好的 C++ 方式:通过改变函数原型来接受对象引用:

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

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

身体也会随之发生变化;eg->将被.操作符等替换。

回答by Karthik T

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

该函数需要一个指向 2 个变量的指针,但您正在传递变量本身,这就是问题所在。您可以通过使用&运算符传递指向变量的指针来解决此问题,如下所示

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.

或者,您可以更改函数以接收对变量的引用,如@iammilind 建议的那样,这意味着您可以保留上面的行。这将是一种更安全、更清洁的“C++”方式。

回答by sampson-chen

Your function DetermineElapsedTimeexpects pointers to MyTime.

您的函数DetermineElapsedTime需要指向MyTime.

Change your code to:

将您的代码更改为:

DetermineElapsedTime(&tm, &tm2);

The &operator in this context means "get the address of"

&此上下文中的运算符的意思是“获取的地址”

回答by SingerOfTheFall

Your funciton needs to take two const pointers:

您的函数需要使用两个 const 指针:

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);
    
  • 更改函数以期待 const 引用:

    int DetermineElapsedTime(const MyTime &t1, const MyTime &t2)
    
  • 获取正在传递的变量的地址:

    MyTime tm, tm2;
    DetermineElapsedTime(&tm, &tm2);
    
  • 动态分配东西并传递指针:

    MyTime *tm = new MyTime();
    MyTime *tm2 = new MyTime();
    DetermineElapsedTime(tm, tm2);