c++ - "undefined reference to" just when using MinGW/GCC compiler, an ambiguous problem?
问题描述
I have an ambiguous problem only when using MinGW/GCC compiler, on Visual Studio 2019 works well.
Firstly, The error messages:
undefined reference to Binary::Length()
undefined reference to Binary::operator[](unsigned long long)
Secondly, The code:
(The part of code that error messages point to (related to "File" class))
bool File::AppendBinary(const String filePath, Binary& binaryData) {
std::ofstream WFile(std::filesystem::path(filePath.ToCString()), std::ios_base::binary | std::ios_base::app);
if (!WFile.is_open()) return false;
WFile.write((char*)&binaryData[0], binaryData.Length()); // <<--- "The error messages point to this line"
WFile.close();
return true;
}
(The Binary class)
binary.h
class Binary {
std::vector<unsigned char> m_binary;
public:
Binary() = default;
Binary(const uintmax_t containerSize);
Binary(unsigned char binary);
Binary(::std::initializer_list<unsigned char> binaryList);
~Binary() = default;
uintmax_t Length();
unsigned char& operator[](uintmax_t index);
};
binary.cpp
Binary::Binary(const uintmax_t containerSize) {
m_binary.resize(containerSize);
}
Binary::Binary(unsigned char binary) {
m_binary.push_back(binary);
}
Binary::Binary(std::initializer_list<unsigned char> binaryList) {
m_binary.assign(binaryList.begin(), binaryList.end());
}
uintmax_t Binary::Length() {
return m_binary.size();
}
unsigned char& Binary::operator[](uintmax_t index) {
if (index > m_binary.size() - 1)
return m_binary[0];
return m_binary[index];
}
When using:
File file ("C:\\ff.txt");
That problem occurs just when using the constructor without using the method the cause of the problem. Also, what makes me crazy is when using the binary class directly the problem does not happen, for example:
Binary bin ((uintmax_t)5);
bin = {0x10, 0x20, 0x30, 0x40};
for(int i=0;i<bin.Length();i++){
cout << hex << (unsigned short) bin[i] << "\n";
}
The previous code works well, so, the binary class compiled without any problem, then what is the problem?
This is the compiler output as a log file:
compiler_log
Note that there are 3 projects (2 static libraries, 1 executable test application)
I found the solution. I said previously there are 2 static libraries must be linked with the executable application, what I did is just changed the order of libraries (which one is the first). recently, the "core" library was the first one and the "files" library was the second but I have changed the order (Linking) to the opposite and then I found the problem solved ScreenShot. Is the order of libraries really a problem?
解决方案
推荐阅读
- hive - Hive 查询在直线上运行良好,但在 zeppelin 上运行良好(使用 %jdbc(hive)):SemanticException
- r - 使用“summarise_if”按列计算均值,接收每个类别的错误 NA
- ruby-on-rails - Rails 6 - 嵌套字段在设计注册/新中不可见
- python - 在 python 中将 os.system() 交换为 driver.execute_script
- python - 编辑或删除 Seaborn displot 的构面标签
- .htaccess - 将多个文件夹重定向到另一个文件夹
- javascript - 如何找到相互服务器并在 Discord.js Bot 的选择菜单中显示它?
- delphi - Delphi 11 TRichEdit 超链接停止工作
- python - 一个热编码列表列,包括附加置信度十进制数
- python - 如何将行附加到 CSV 数据?