所以我正在努力使用分支定界算法来实现 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/