xcode 被调用的对象“char*”类型不是函数或函数指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11119494/
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-09-15 00:38:46  来源:igfitidea点击:

called object 'char* 'type is not a function or function pointer

c++cxcodetime

提问by Itzik984

I have this function which is supposed to set a certain time format to the given char*:

我有这个函数,它应该为给定的 char* 设置特定的时间格式:

static void timeStamp(char* time)
{
  time(&strTime);<---ERROR
  curTime = std::localtime(&strTime);
  strftime(time, 8, "%H:%M::", curTime);    
}

strTime and curTime were declared like this:

strTime 和 curTime 声明如下:

tm* curTime; 
time_t strTime;

but for some reason i get:

但出于某种原因,我得到:

called object type 'char*' is not a function or function pointer

on the marked place.

在标记的地方。

any idea why?

知道为什么吗?

im using xCode by the way.

顺便说一下,我正在使用 xCode。

回答by Dancrumb

Your function parameter timeis a pointer to a char.

您的函数参数time是一个指向字符的指针。

However, in your function body, you're trying to treat it if it were a function that you can call. That's what the error is saying...

但是,在您的函数体中,如果它是一个可以调用的函数,您就会尝试处理它。这就是错误所说的......

the object of type char *is not a function or a function pointer [therefore, I can't call it!]

类型的对象char *不是函数或函数指针[因此,我不能调用它!]

Essentially, you've hidden the timefunction by having a local variable of the same name. I'd recommend changing your function parameter's name.

本质上,您time通过使用同名的局部变量隐藏了该函数。我建议更改函数参数的名称。

回答by Daniel Fischer

The function parameter

函数参数

static void timeStamp(char* time)
{
  time(&strTime);<---ERROR
  // ...
}

shadows the time()function. Rename the parameter.

阴影time()功能。重命名参数。

回答by Eight

static void timeStamp(char* time)  

here, this char* timeparameter is hidding the function time().you will need to rename it.

在这里,这个char* time参数隐藏了函数。time()你需要重命名它。

回答by Gart

The function timeis also the name of your parameter to timeStampfunction. The compiler tries to call the char* parameter as if it were a function.

该函数 time也是要timeStamp运行的参数的名称。编译器尝试调用 char* 参数,就好像它是一个函数一样。

回答by Richard J. Ross III

You have redefined the symbol time, to fix, try the following:

您已经重新定义了符号time,要修复,请尝试以下操作:

static void timeStamp(char *time)
{
    time_t strTime;

    { // brackets here are important!
        extern time_t time(time_t *);
        time(&strTime);
    }

    // ...
}

We add brackets here to add additional scope to the function, and then we tell the compiler that hey, in this block, time is not a variable, its a function, and that allows the compiler to work fine with your current variable name, as well.

我们在此处添加括号以向函数添加额外的作用域,然后我们告诉编译器hey, in this block, time is not a variable, its a function,这样编译器也可以使用您当前的变量名正常工作。

Optionally, you can also use the global namespace specifier, like this:

或者,您还可以使用全局命名空间说明符,如下所示:

static void timeStamp(char *time)
{
    ::time(&strTime);
    // ...
}

回答by glglgl

Because timeis a char*which you cannot call - look at your parameter list.

因为timechar*你不能调用的 - 查看你的参数列表。

Just rename that and your problem will be gone.

只需重命名,您的问题就会消失。