重载的“malloc(int)”调用不明确

San*_*era 0 c++

以下函数属于 CoinUtils 项目,用于开源 CBC 线性规划求解器。

\n
/** Returns strdup or NULL if original NULL */\ninline char *CoinStrdup(const char *name)\n{\n  char *dup = NULL;\n  if (name) {\n    const int len = static_cast< int >(strlen(name));\n    dup = static_cast< char * >(malloc(len + 1));\n    CoinMemcpyN(name, len, dup);\n    dup[len] = 0;\n  }\n  return dup;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

当我在 ubuntu 22.04 下的 GCC 11 中以调试模式进行编译时,出现以下错误。

\n
error: call of overloaded \xe2\x80\x98malloc(int)\xe2\x80\x99 is ambiguous\n  624 |     dup = static_cast< char * >(malloc(len + 1));\n
Run Code Online (Sandbox Code Playgroud)\n

有人可以解释一下发生了什么事吗?

\n

完整的错误信息是这样的:

\n
In file included from /src/third_party/coin-or/Clp/src/ClpModel.hpp:21,\n                 from /src/third_party/coin-or/Clp/src/ClpSimplex.hpp:16,\n                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:12,\n                 from /src/test/lp_tests.h:13,\n                 from /src/test/lp_tests.cpp:5:\n/src/third_party/coin-or/CoinUtils/src/CoinHelperFunctions.hpp: In function \xe2\x80\x98char* CoinStrdup(const char*)\xe2\x80\x99:\n/src/third_party/coin-or/CoinUtils/src/CoinHelperFunctions.hpp:624:39: error: call of overloaded \xe2\x80\x98malloc(int)\xe2\x80\x99 is ambiguous\n  624 |     dup = static_cast< char * >(malloc(len + 1));\n      |                                 ~~~~~~^~~~~~~~~\nIn file included from /usr/include/c++/11/cstdlib:75,\n                 from /usr/include/c++/11/ext/string_conversions.h:41,\n                 from /usr/include/c++/11/bits/basic_string.h:6608,\n                 from /usr/include/c++/11/string:55,\n                 from /usr/include/c++/11/bits/locale_classes.h:40,\n                 from /usr/include/c++/11/bits/ios_base.h:41,\n                 from /usr/include/c++/11/ios:42,\n                 from /usr/include/c++/11/ostream:38,\n                 from /usr/include/c++/11/iostream:39,\n                 from /src/test/testing_utilities.h:8,\n                 from /src/test/lp_tests.h:8,\n                 from /src/test/lp_tests.cpp:5:\n/usr/include/stdlib.h:540:14: note: candidate: \xe2\x80\x98void* malloc(size_t)\xe2\x80\x99\n  540 | extern void *malloc (size_t __size) __THROW __attribute_malloc__\n      |              ^~~~~~\nIn file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,\n                 from /src/test/testing_utilities.h:9,\n                 from /src/test/lp_tests.h:8,\n                 from /src/test/lp_tests.cpp:5:\n/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:332:3: note: candidate: \xe2\x80\x98void* arma::superlu::malloc(std::size_t)\xe2\x80\x99\n  332 |   malloc(size_t N)\n      |   ^~~~~~\nIn file included from /src/third_party/coin-or/CoinUtils/src/CoinModel.hpp:8,\n                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:16,\n                 from /src/test/lp_tests.h:13,\n                 from /src/test/lp_tests.cpp:5:\n/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp: In destructor \xe2\x80\x98CoinYacc::~CoinYacc()\xe2\x80\x99:\n/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp:200:11: error: call of overloaded \xe2\x80\x98free(char*&)\xe2\x80\x99 is ambiguous\n  200 |       free(symbuf);\n      |       ~~~~^~~~~~~~\nIn file included from /usr/include/c++/11/cstdlib:75,\n                 from /usr/include/c++/11/ext/string_conversions.h:41,\n                 from /usr/include/c++/11/bits/basic_string.h:6608,\n                 from /usr/include/c++/11/string:55,\n                 from /usr/include/c++/11/bits/locale_classes.h:40,\n                 from /usr/include/c++/11/bits/ios_base.h:41,\n                 from /usr/include/c++/11/ios:42,\n                 from /usr/include/c++/11/ostream:38,\n                 from /usr/include/c++/11/iostream:39,\n                 from /src/test/testing_utilities.h:8,\n                 from /src/test/lp_tests.h:8,\n                 from /src/test/lp_tests.cpp:5:\n/usr/include/stdlib.h:555:13: note: candidate: \xe2\x80\x98void free(void*)\xe2\x80\x99\n  555 | extern void free (void *__ptr) __THROW;\n      |             ^~~~\nIn file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,\n                 from /src/test/testing_utilities.h:9,\n                 from /src/test/lp_tests.h:8,\n                 from /src/test/lp_tests.cpp:5:\n/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:341:3: note: candidate: \xe2\x80\x98void arma::superlu::free(void*)\xe2\x80\x99\n  341 |   free(void* mem)\n      |   ^~~~\nIn file included from /src/third_party/coin-or/CoinUtils/src/CoinModel.hpp:8,\n                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:16,\n                 from /src/test/lp_tests.h:13,\n                 from /src/test/lp_tests.cpp:5:\n/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp:205:11: error: call of overloaded \xe2\x80\x98free(char*&)\xe2\x80\x99 is ambiguous\n  205 |       free(s->name);\n      |       ~~~~^~~~~~~~~\nIn file included from /usr/include/c++/11/cstdlib:75,\n                 from /usr/include/c++/11/ext/string_conversions.h:41,\n                 from /usr/include/c++/11/bits/basic_string.h:6608,\n                 from /usr/include/c++/11/string:55,\n                 from /usr/include/c++/11/bits/locale_classes.h:40,\n                 from /usr/include/c++/11/bits/ios_base.h:41,\n                 from /usr/include/c++/11/ios:42,\n                 from /usr/include/c++/11/ostream:38,\n                 from /usr/include/c++/11/iostream:39,\n                 from /src/test/testing_utilities.h:8,\n                 from /src/test/lp_tests.h:8,\n                 from /src/test/lp_tests.cpp:5:\n/usr/include/stdlib.h:555:13: note: candidate: \xe2\x80\x98void free(void*)\xe2\x80\x99\n  555 | extern void free (void *__ptr) __THROW;\n      |             ^~~~\nIn file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,\n                 from /src/test/testing_utilities.h:9,\n                 from /src/test/lp_tests.h:8,\n                 from /src/test/lp_tests.cpp:5:\n/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:341:3: note: candidate: \xe2\x80\x98void arma::superlu::free(void*)\xe2\x80\x99\n  341 |   free(void* mem)\n      |   ^~~~\nIn file included from /src/third_party/coin-or/CoinUtils/src/CoinModel.hpp:8,\n                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:16,\n                 from /src/test/lp_tests.h:13,\n                 from /src/test/lp_tests.cpp:5:\n/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp:208:11: error: call of overloaded \xe2\x80\x98free(symrec*&)\xe2\x80\x99 is ambiguous\n  208 |       free(symtable);\n      |       ~~~~^~~~~~~~~~\nIn file included from /usr/include/c++/11/cstdlib:75,\n                 from /usr/include/c++/11/ext/string_conversions.h:41,\n                 from /usr/include/c++/11/bits/basic_string.h:6608,\n                 from /usr/include/c++/11/string:55,\n                 from /usr/include/c++/11/bits/locale_classes.h:40,\n                 from /usr/include/c++/11/bits/ios_base.h:41,\n                 from /usr/include/c++/11/ios:42,\n                 from /usr/include/c++/11/ostream:38,\n                 from /usr/include/c++/11/iostream:39,\n                 from /src/test/testing_utilities.h:8,\n                 from /src/test/lp_tests.h:8,\n                 from /src/test/lp_tests.cpp:5:\n/usr/include/stdlib.h:555:13: note: candidate: \xe2\x80\x98void free(void*)\xe2\x80\x99\n  555 | extern void free (void *__ptr) __THROW;\n      |             ^~~~\nIn file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,\n                 from /src/test/testing_utilities.h:9,\n                 from /src/test/lp_tests.h:8,\n                 from /src/test/lp_tests.cpp:5:\n/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:341:3: note: candidate: \xe2\x80\x98void arma::superlu::free(void*)\xe2\x80\x99\n  341 |   free(void* mem)\n      |   ^~~~\ngmake[3]: *** [CMakeFiles/Tests.dir/build.make:8532: CMakeFiles/Tests.dir/src/test/lp_tests.cpp.o] Error 1\ngmake[3]: *** Se espera a que terminen otras tareas....\nIn file included from /src/third_party/coin-or/Clp/src/ClpModel.hpp:21,\n                 from /src/third_party/coin-or/Clp/src/ClpSimplex.hpp:16,\n                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:12,\n                 from /src/test/lp_tests.h:13,\n                 from /src/test/tests.cpp:20:\n/src/third_party/coin-or/CoinUtils/src/CoinHelperFunctions.hpp: In function \xe2\x80\x98char* CoinStrdup(const char*)\xe2\x80\x99:\n/src/third_party/coin-or/CoinUtils/src/CoinHelperFunctions.hpp:624:39: error: call of overloaded \xe2\x80\x98malloc(int)\xe2\x80\x99 is ambiguous\n  624 |     dup = static_cast< char * >(malloc(len + 1));\n      |                                 ~~~~~~^~~~~~~~~\nIn file included from /usr/include/c++/11/cstdlib:75,\n                 from /usr/include/c++/11/ext/string_conversions.h:41,\n                 from /usr/include/c++/11/bits/basic_string.h:6608,\n                 from /usr/include/c++/11/string:55,\n                 from /usr/include/c++/11/bits/locale_classes.h:40,\n                 from /usr/include/c++/11/bits/ios_base.h:41,\n                 from /usr/include/c++/11/ios:42,\n                 from /usr/include/c++/11/ostream:38,\n                 from /usr/include/c++/11/iostream:39,\n                 from /src/test/tests.cpp:2:\n/usr/include/stdlib.h:540:14: note: candidate: \xe2\x80\x98void* malloc(size_t)\xe2\x80\x99\n  540 | extern void *malloc (size_t __size) __THROW __attribute_malloc__\n      |              ^~~~~~\nIn file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,\n                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/core/arma_extend/arma_extend.hpp:21,\n                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/base.hpp:87,\n                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack.hpp:25,\n                 from /src/engine/../utils/type_declarations.h:9,\n                 from /src/engine/../utils/armadillo_extension.h:19,\n                 from /src/test/../engine/core/data_structures/bus_data.h:10,\n                 from /src/test/../engine/core/data_structures/numeric_circuit.h:22,\n                 from /src/test/../newton.h:9,\n                 from /src/test/tests.cpp:9:\n/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:332:3: note: candidate: \xe2\x80\x98void* arma::superlu::malloc(std::size_t)\xe2\x80\x99\n  332 |   malloc(size_t N)\n      |   ^~~~~~\nIn file included from /src/third_party/coin-or/CoinUtils/src/CoinModel.hpp:8,\n                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:16,\n                 from /src/test/lp_tests.h:13,\n                 from /src/test/tests.cpp:20:\n/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp: In destructor \xe2\x80\x98CoinYacc::~CoinYacc()\xe2\x80\x99:\n/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp:200:11: error: call of overloaded \xe2\x80\x98free(char*&)\xe2\x80\x99 is ambiguous\n  200 |       free(symbuf);\n      |       ~~~~^~~~~~~~\nIn file included from /usr/include/c++/11/cstdlib:75,\n                 from /usr/include/c++/11/ext/string_conversions.h:41,\n                 from /usr/include/c++/11/bits/basic_string.h:6608,\n                 from /usr/include/c++/11/string:55,\n                 from /usr/include/c++/11/bits/locale_classes.h:40,\n                 from /usr/include/c++/11/bits/ios_base.h:41,\n                 from /usr/include/c++/11/ios:42,\n                 from /usr/include/c++/11/ostream:38,\n                 from /usr/include/c++/11/iostream:39,\n                 from /src/test/tests.cpp:2:\n/usr/include/stdlib.h:555:13: note: candidate: \xe2\x80\x98void free(void*)\xe2\x80\x99\n  555 | extern void free (void *__ptr) __THROW;\n      |             ^~~~\nIn file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,\n                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/core/arma_extend/arma_extend.hpp:21,\n                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/base.hpp:87,\n                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack.hpp:25,\n                 from /src/engine/../utils/type_declarations.h:9,\n                 from /src/engine/../utils/armadillo_extension.h:19,\n                 from /src/test/../engine/core/data_structures/bus_data.h:10,\n                 from /src/test/../engine/core/data_structures/numeric_circuit.h:22,\n                 from /src/test/../newton.h:9,\n                 from /src/test/tests.cpp:9:\n/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:341:3: note: candidate: \xe2\x80\x98void arma::superlu::free(void*)\xe2\x80\x99\n  341 |   free(void* mem)\n      |   ^~~~\nIn file included from /src/third_party/coin-or/CoinUtils/src/CoinModel.hpp:8,\n                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:16,\n                 from /src/test/lp_tests.h:13,\n                 from /src/test/tests.cpp:20:\n/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp:205:11: error: call of overloaded \xe2\x80\x98free(char*&)\xe2\x80\x99 is ambiguous\n  205 |       free(s->name);\n      |       ~~~~^~~~~~~~~\nIn file included from /usr/include/c++/11/cstdlib:75,\n                 from /usr/include/c++/11/ext/string_conversions.h:41,\n                 from /usr/include/c++/11/bits/basic_string.h:6608,\n                 from /usr/include/c++/11/string:55,\n                 from /usr/include/c++/11/bits/locale_classes.h:40,\n                 from /usr/include/c++/11/bits/ios_base.h:41,\n                 from /usr/include/c++/11/ios:42,\n                 from /usr/include/c++/11/ostream:38,\n                 from /usr/include/c++/11/iostream:39,\n                 from /src/test/tests.cpp:2:\n/usr/include/stdlib.h:555:13: note: candidate: \xe2\x80\x98void free(void*)\xe2\x80\x99\n  555 | extern void free (void *__ptr) __THROW;\n      |             ^~~~\nIn file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,\n                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/core/arma_extend/arma_extend.hpp:21,\n                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/base.hpp:87,\n                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack.hpp:25,\n                 from /src/engine/../utils/type_declarations.h:9,\n                 from /src/engine/../utils/armadillo_extension.h:19,\n                 from /src/test/../engine/core/data_structures/bus_data.h:10,\n                 from /src/test/../engine/core/data_structures/numeric_circuit.h:22,\n                 from /src/test/../newton.h:9,\n                 from /src/test/tests.cpp:9:\n/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:341:3: note: candidate: \xe2\x80\x98void arma::superlu::free(void*)\xe2\x80\x99\n  341 |   free(void* mem)\n      |   ^~~~\nIn file included from /src/third_party/coin-or/CoinUtils/src/CoinModel.hpp:8,\n                 from /src/engine/core/../core/lp_interface/lp_osi_interface.h:16,\n                 from /src/test/lp_tests.h:13,\n                 from /src/test/tests.cpp:20:\n/src/third_party/coin-or/CoinUtils/src/CoinModelUseful.hpp:208:11: error: call of overloaded \xe2\x80\x98free(symrec*&)\xe2\x80\x99 is ambiguous\n  208 |       free(symtable);\n      |       ~~~~^~~~~~~~~~\nIn file included from /usr/include/c++/11/cstdlib:75,\n                 from /usr/include/c++/11/ext/string_conversions.h:41,\n                 from /usr/include/c++/11/bits/basic_string.h:6608,\n                 from /usr/include/c++/11/string:55,\n                 from /usr/include/c++/11/bits/locale_classes.h:40,\n                 from /usr/include/c++/11/bits/ios_base.h:41,\n                 from /usr/include/c++/11/ios:42,\n                 from /usr/include/c++/11/ostream:38,\n                 from /usr/include/c++/11/iostream:39,\n                 from /src/test/tests.cpp:2:\n/usr/include/stdlib.h:555:13: note: candidate: \xe2\x80\x98void free(void*)\xe2\x80\x99\n  555 | extern void free (void *__ptr) __THROW;\n      |             ^~~~\nIn file included from /src/third_party/header_only/armadillo-11.1.0/include/armadillo:134,\n                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/core/arma_extend/arma_extend.hpp:21,\n                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack/base.hpp:87,\n                 from /src/third_party/header_only/mlpack-4.0.0/src/mlpack.hpp:25,\n                 from /src/engine/../utils/type_declarations.h:9,\n                 from /src/engine/../utils/armadillo_extension.h:19,\n                 from /src/test/../engine/core/data_structures/bus_data.h:10,\n                 from /src/test/../engine/core/data_structures/numeric_circuit.h:22,\n                 from /src/test/../newton.h:9,\n                 from /src/test/tests.cpp:9:\n/src/third_party/header_only/armadillo-11.1.0/include/armadillo_bits/translate_superlu.hpp:341:3: note: candidate: \xe2\x80\x98void arma::superlu::free(void*)\xe2\x80\x99\n  341 |   free(void* mem)\n      |   ^~~~\ngmake[3]: *** [CMakeFiles/Tests.dir/build.make:8462: CMakeFiles/Tests.dir/src/test/tests.cpp.o] Error 1\ngmake[2]: *** [CMakeFiles/Makefile2:324: CMakeFiles/Tests.dir/all] Error 2\ngmake[1]: *** [CMakeFiles/Makefile2:331: CMakeFiles/Tests.dir/rule] Error 2\ngmake: *** [Makefile:293: Tests] Error 2\n
Run Code Online (Sandbox Code Playgroud)\n

Kev*_*vin 6

它考虑的两个函数是标准函数void* malloc(size_t)void* arma::superlu::malloc(std::size_t). 大概在某个地方,您可以将该using namespace arma::superlu名称空间中的所有内容提取到全局名称空间中。

为了解决这个问题,你可以

  1. 摆脱using namespace arma::superlu并完全限定该名称空间中的所有内容,或者完全引入您需要的内容(使用using arma::superlu::xyz;)。或者
  2. 使用 限定malloc调用::malloc,或者最好std::malloc调用 C++ 标准版本。

与对 的模糊调用类似free