c - KnapSack Branch and Bound 奇怪的编译错误

标签 c algorithm compilation compiler-errors knapsack-problem

所以我正在努力使用分支定界算法来实现 KnapSack 问题。我已经完成了它的实现,但我遇到了一些奇怪的编译错误,我不知道如何修复:

编译错误

gcc -Wall -pedantic -g -std=c99   -c -o bnb.o bnb.c
bnb.c: In function ‘branch_and_bound’:
bnb.c:225: warning: cast from pointer to integer of different size
bnb.c:229: warning: implicit declaration of function ‘copy_string’
bnb.c:248: warning: cast from pointer to integer of different size
bnb.c:251: error: ‘struc_sol’ has no member named ‘string’
bnb.c:260: error: ‘struc_sol’ has no member named ‘string’
bnb.c:260: warning: cast from pointer to integer of different size
bnb.c:263: error: ‘struc_sol’ has no member named ‘string’
make: *** [bnb.o] Error 1

对我做错了什么有什么建议吗?

最佳答案

bnb.c:225: warning: cast from pointer to integer of different size

这来自以下行:

     topNode->solution_vec[i] = (int)malloc(sizeof(int));

正如消息中提到的,malloc() 返回一个指针。您不应该将其转换为整数。事实上,您根本不需要为 solution_vec[i] 分配内存,因为它已经由 topNode 的早期分配分配了。

<小时/>

bnb.c:229: warning: implicit declaration of function ‘copy_string’

检查头文件中是否声明了 copy_string()

<小时/>

bnb.c:251: error: ‘struc_sol’ has no member named ‘string’

正如上面提到的,struc_sol 没有名为 string 的成员,因此 child1Node->string 会导致语法错误。

关于c - KnapSack Branch and Bound 奇怪的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22486683/

相关文章:

algorithm - 解决以下重现: T(n) = T(n/3) + T(n/2) + sqrt(n)

c - 内核输入/输出

c++ - 调用带有编译错误的 C++ 方法

CLion 不会在调试中显示输出

python - Cython:教程找不到从未引用过的外部符号

c - OpenCV 代码示例错误

arrays - Ruby - 如何切片数组并根据条件对其元素求和

c - ubuntu 18.04 上的 c 文件中对 "readline"的 undefined reference (已安装 libreadline-dev,与 "-lreadline"链接

algorithm - LZW 或 JBIG 是更好的图像无损压缩算法?

Scala 和前向引用