簡體   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

我所有的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);
}

關於read_den (最后一個錯誤)。 read_den確實有4個參數。 您可以在此處看到函數原型及其相應的調用:

  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

是我的代碼還是荒謬的錯誤。 如何糾正它們?

編輯:可以對最后一個錯誤發表評論,因為。 我無法證明這一點。

EDIT2:當我將文件擴展名從* .cpp更改為* .c時,所有錯誤均消失了。 因此,我認為這與C&C ++有關。

瘋狂猜測:您在其他地方錯誤地使用了malloc ,傳遞了兩個參數而不是一個。 這將導致隱式聲明。

嘗試在啟用所有警告的情況下進行編譯,看看是否有任何問題發生。


更新:您還可以將#include <stlib.h>作為源文件的第一行,以便將任何潛在的隱式聲明都標記為錯誤。

也許您將真正的malloc函數隱藏在代碼中。 在gcc中,您可以使用-Wshadow標志進行測試。 我確信Visual Studio中有類似的東西。

編輯:我閱讀了您添加的第二部分,並且錯誤似乎確實來自C和C ++之間的不兼容性。 根據您的項目規模,這可能是繁瑣的工作。 我建議您使用“ extern”關鍵字將新的C ++代碼鏈接到工作的C代碼。

例:

#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;
}

以及帶有“ extern”關鍵字的C ++函數:

#include <iostream>

extern "C" void cplusplus_function(int);

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

現在,您可以分別編譯文件並將它們鏈接在一起。

  1. 您是否有#include <stdlib.h>
  2. 嘗試生成預處理的輸出(對於MSVC,這是使用/E選項完成的)。 在結果輸出中查找mallocread_den聲明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM