不能算出这个“被调用对象不是函数”C时间错误吗

时间:2021-03-08 21:11:00

So for part of my school assignment, I need to find the current time, and i used this as a reference:http://www.cplusplus.com/reference/ctime/localtime/ I tried to copy what they did, but I keep getting this error:

所以在我的学校作业中,我需要找到当前的时间,我用这个作为参考:http://www.cplusplus.com/reference/ctime/localtime/我试图复制他们所做的,但是我一直得到这个错误:

l8stat.c:33: error: called object '15552000' is not a function

This is the line:

这是线:

time(&current_time);

I really don't understand why this is happening when I'm doing the same thing as the example.

我真的不明白为什么会发生这种情况,当我做和这个例子一样的事情时。

Here's my code:

这是我的代码:

#include <errno.h>
#include <libgen.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <time.h>
#include <limits.h>

#define SEC_PER_DAY (24 * 60 * 60)
#define time (SEC_PER_DAY*180)

 void print_data(char *path ){
   setlocale (LC_NUMERIC, "en_US");
   struct stat *new=malloc(sizeof(struct stat));
   char linkname[PATH_MAX + 1];
   int status=lstat(path, new);
   if(status==0){
      printf("%.6o  %9d ",new->st_mode,
         new->st_size );
  }
  else{
     fprintf (stderr, "l8stat: %s: %s\n",
           path, strerror (errno));
 }

 time_t current_time;
 struct tm *local_time;
 time(&current_time);
 local_time=localtime(&current_time);
 char buffer[50];


 if(current_time-new->st_mtime<=time ){
    strftime(buffer,50,"%b %e %R",new->st_mtime);
    puts(buffer);
 } else{
     strftime(buffer,50,"%b %e  %Y",new->st_mtime);
     puts(buffer);
   }

 ssize_t retval = readlink (path, linkname, sizeof linkname);
   if(retval >=0){
      linkname[retval < PATH_MAX + 1 ? retval : PATH_MAX] = '\0';
      printf ("%s -> \"%s\"\n", path, linkname);
   }

 printf(" %s",path);
}

int main (int argc, char **argv) {
  int exit_status=EXIT_SUCCESS;
  for(int argi = 1; argi <argc; ++argi){
     if(argc!=2)  ;
     print_data(argv[argi]);
     printf("\n");
  }

  return exit_status;
 }

3 个解决方案

#1


4  

You #define time (SEC_PER_DAY*180), then later you call `time(&current_time);'

定义时间(SEC_PER_DAY*180),然后调用“time(&current_time)”;

The preprocessor will expand

预处理器将扩大

time(&current_time);

as per your #define::

按照你的# define::

(SEC_PER_DAY*180)(&current_time);

and then

然后

(24*60*60*180)(&current_time);

Note that this happens before the actual compiler sees anything (the preprocessor runs before the compiler). So as far as the compiler is concerned, you're trying to call 24*60*60*180 = 15552000.

注意,这发生在实际编译器看到任何东西之前(预处理器在编译器之前运行)。就编译器而言,您正在尝试调用24*60* 180 = 15552000。

#2


1  

The line #define time (SEC_PER_DAY*180) at the top causes all occurrences of the word "time" in the code to be replaced with (SEC_PER_DAY*180), which then becomes ((24 * 60 * 60)*180), which evaluates to 15552000. Thus, when you try to call the time function later, what the compiler actually sees is 15552000(&current_time), which is an error. The solution is simple, and applies to all code, not just this case: don't name anything after standard functions — change the #define time to use a different name.

顶部的第一行#define time (SEC_PER_DAY*180)导致代码中出现的所有“time”字被替换为(SEC_PER_DAY*180),然后变为(24 * 60 * 60)*180,其值为15552000。因此,当您稍后尝试调用time函数时,编译器实际看到的是15552000(&current_time),这是一个错误。解决方案很简单,并且适用于所有代码,而不仅仅是这种情况:在标准函数之后不要命名任何东西——更改#define time以使用不同的名称。

#3


0  

Your:

你的:

#include <time.h>

is defining some time variable that you redefine with your

定义一个时间变量,你用你的

#define time (SEC_PER_DAY*180)

and that's where the problem is, change that define name and it will work.

这就是问题所在,改变定义名,它就会工作。

#1


4  

You #define time (SEC_PER_DAY*180), then later you call `time(&current_time);'

定义时间(SEC_PER_DAY*180),然后调用“time(&current_time)”;

The preprocessor will expand

预处理器将扩大

time(&current_time);

as per your #define::

按照你的# define::

(SEC_PER_DAY*180)(&current_time);

and then

然后

(24*60*60*180)(&current_time);

Note that this happens before the actual compiler sees anything (the preprocessor runs before the compiler). So as far as the compiler is concerned, you're trying to call 24*60*60*180 = 15552000.

注意,这发生在实际编译器看到任何东西之前(预处理器在编译器之前运行)。就编译器而言,您正在尝试调用24*60* 180 = 15552000。

#2


1  

The line #define time (SEC_PER_DAY*180) at the top causes all occurrences of the word "time" in the code to be replaced with (SEC_PER_DAY*180), which then becomes ((24 * 60 * 60)*180), which evaluates to 15552000. Thus, when you try to call the time function later, what the compiler actually sees is 15552000(&current_time), which is an error. The solution is simple, and applies to all code, not just this case: don't name anything after standard functions — change the #define time to use a different name.

顶部的第一行#define time (SEC_PER_DAY*180)导致代码中出现的所有“time”字被替换为(SEC_PER_DAY*180),然后变为(24 * 60 * 60)*180,其值为15552000。因此,当您稍后尝试调用time函数时,编译器实际看到的是15552000(&current_time),这是一个错误。解决方案很简单,并且适用于所有代码,而不仅仅是这种情况:在标准函数之后不要命名任何东西——更改#define time以使用不同的名称。

#3


0  

Your:

你的:

#include <time.h>

is defining some time variable that you redefine with your

定义一个时间变量,你用你的

#define time (SEC_PER_DAY*180)

and that's where the problem is, change that define name and it will work.

这就是问题所在,改变定义名,它就会工作。