简体   繁体   English

Visual C ++ ::'malloc'中的奇怪错误:函数没有1个参数

[英]Strange errors in Visual C++ :: 'malloc' : function does not take 1 arguments

Error 38 error C2660: 'malloc' : function does not take 1 arguments C:\VolumeRenderer\render.cpp 296 1 VolumeRenderer
Error 39 error C2660: 'malloc' : function does not take 1 arguments C:\VolumeRenderer\render.cpp 412 1 VolumeRenderer
Error 40 error C2660: 'malloc' : function does not take 1 arguments C:\VolumeRenderer\render.cpp 414 1 VolumeRenderer
Error 41 error C2660: 'read_den' : function does not take 4 arguments C:\VolumeRenderer\render.cpp 506 1 VolumeRenderer

My all malloc sections are like this: 我所有的malloc部分都是这样的:

/* allocate space for the raw data */
density_size = BRAIN_XLEN * BRAIN_YLEN * BRAIN_ZLEN;
density = (unsigned char*)malloc(density_size);
if (density == NULL) {
  fprintf(stderr, "out of memory\n");
  exit(1);
}

regarding read_den (last error) . 关于read_den (最后一个错误)。 read_den does take 4 parameters. read_den确实有4个参数。 You can see the function prototype & its corresponding call here: 您可以在此处看到函数原型及其相应的调用:

  unsigned char *read_den(char *filename,int *xptr,int *yptr,int *zptr)// function prototype
  src_volume = read_den(src_file, &src_xlen, &src_ylen, &src_zlen);// function call

Is it my code or the errors that are absurd. 是我的代码还是荒谬的错误。 How to rectify them? 如何纠正它们?

EDIT: can some one comment on the last error because. 编辑:可以对最后一个错误发表评论,因为。 I'm unable to justify it. 我无法证明这一点。

EDIT2: When I changed the file extension from *.cpp to *.c then all errors are gone. EDIT2:当我将文件扩展名从* .cpp更改为* .c时,所有错误均消失了。 So, I think it has something to do with C & C++. 因此,我认为这与C&C ++有关。

Wild guess: you used malloc incorrectly somewhere else, passing two arguments instead of one. 疯狂猜测:您在其他地方错误地使用了malloc ,传递了两个参数而不是一个。 This would result in an implicit declaration. 这将导致隐式声明。

Try compiling with all warnings enabled and see if anything comes up. 尝试在启用所有警告的情况下进行编译,看看是否有任何问题发生。


Update: You could also put #include <stlib.h> as the very first line in your source file, so that any potential implicit declaration would be flagged as an error. 更新:您还可以将#include <stlib.h>作为源文件的第一行,以便将任何潜在的隐式声明都标记为错误。

Maybe you shadow the real malloc function somewhere in your code. 也许您将真正的malloc函数隐藏在代码中。 In gcc you can use the -Wshadow flag to test. 在gcc中,您可以使用-Wshadow标志进行测试。 I am sure there is something similiar in Visual Studio. 我确信Visual Studio中有类似的东西。

Edit: I read the second part you added and the errors seem to come indeed from incompatibilities between C and C++. 编辑:我阅读了您添加的第二部分,并且错误似乎确实来自C和C ++之间的不兼容性。 Depending on the size of your project, this might be tedious work to sort out. 根据您的项目规模,这可能是繁琐的工作。 I suggest, that you use the "extern" keyword to link your new C++ code to your working C code. 我建议您使用“ extern”关键字将新的C ++代码链接到工作的C代码。

Example: 例:

#include <stdio.h>
#include <stdlib.h>

int cplusplus_function(int a); 

main(){
    printf("c code\n");

    int* a = malloc(sizeof(int)); //just proving that this is indeed C code.
                                  //this would not compile with a C++ compiler

    cplusplus_function(5);
    return 0;
}

and a C++ function with the "extern" keyword: 以及带有“ extern”关键字的C ++函数:

#include <iostream>

extern "C" void cplusplus_function(int);

void cplusplus_function(int a){
    std::cout << "c++ code" << std::endl;
}

Now you can compile the files separately and link them together. 现在,您可以分别编译文件并将它们链接在一起。

  1. Do you have #include <stdlib.h> ? 您是否有#include <stdlib.h>
  2. Try generating the preprocessed output (with MSVC, this is done with the /E option). 尝试生成预处理的输出(对于MSVC,这是使用/E选项完成的)。 Look through the resulting output for malloc and read_den declarations. 在结果输出中查找mallocread_den声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM