gpt4 book ai didi

c++ - extern数组出现 undefined reference 错误,但对于int效果很好

转载 作者:行者123 更新时间:2023-12-02 10:04:43 24 4
gpt4 key购买 nike

编译错误:
[build] main.cpp:8:未定义对`pallete'的引用

示例代码:
common.cpp

const unsigned char pallete[] = {0, 60, 100, 119};
int a = 1;

main.cpp
#include <iostream>

extern const unsigned char pallete[];
extern int a;

int main() {
std::cout << a << std::endl;
std::cout << pallete[0] << std::endl;
return 0;
}


最佳答案

您必须在extern中声明pallete common.cpp。现在main.cpp知道应该在单独的文件中“查找”货盘,但是common.cpp将其视为本地文件,因为它是const,如此处https://en.cppreference.com/w/cpp/language/cv#Notes

Notes

The const qualifier used on a declaration of a non-local non-volatile non-template (since C++14)non-inline (since C++17) variable that is not declared extern gives it internal linkage. This is different from C where const file scope variables have external linkage.



extern const unsigned char pallete[] = {0, 60, 100, 119};
int a = 1;

关于c++ - extern数组出现 undefined reference 错误,但对于int效果很好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60864962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com