在Windows上使用GCC的代码出现编译器错误
Compiler Error on code with GCC that worked on Windows
我有一段简单的代码,它给了我一个编译器错误。我在Visual Studio下的windows环境中编译和运行这个程序没有遇到任何问题,但现在在linux下,使用gcc,我遇到了问题。注意:我使用gcc 4.4.5,并使用-std=c++0x指令。
此代码片段位于头文件file_handling.h中,该文件包含所有必要的库(vector、string、fstream等)。变量"output_file"是LogFile对象的一个成员,并在其他地方得到正确的检查/实例化等。代码本身非常简单,这就是我被难住的原因:
template <typename T> void LogFile::put(std::string const & header, std::vector<T> const & data) {
output_file << header << " " << std::scientific << data[0] << std::endl;
for (std::vector<T>::const_iterator value = (data.begin()+1); value < data.end(); ++value) {
output_file << *value << std::endl;
}
}
编译器声明:
In file included from file_handling.cpp:2:
file_handling.h: In member function 'void LogFile::put(const std::string&, const std::vector<T, std::allocator<_Tp1> >&)':
file_handling.h:132: error: expected ';' before 'value'
file_handling.h:132: error: 'value' was not declared in this scope
make: *** [file_handling.o] Error 1
为什么gcc不将"value"的原位声明视为const_iterator?我尝试了以下内容作为健全性检查:
template <typename T> void LogFile::put(std::string const & header, std::vector<T> const & data) {
std::vector<T>::const_iterator value;
output_file << header << " " << std::scientific << data[0] << std::endl;
for (value = (data.begin()+1); value < data.end(); ++value) {
output_file << *value << std::endl;
}
}
并收到完全相同的编译器报告。考虑到这看起来很简单,并且在Visual Studio中运行良好,我对gcc和/或Linux环境缺少什么或误解了什么?
正确的形式应该是:
template <typename T> void LogFile::put(std::string const & header, std::vector<T> const & data) {
output_file << header << " " << std::scientific << data[0] << std::endl;
for (typename std::vector<T>::const_iterator value = (data.cbegin()+1); value != data.cend(); ++value) {
output_file << *value << std::endl;
}
}
注意typename的添加,以及从begin()和end()到cbegin()和cend()的变化。
使用模板化类型时,typename是必需的。begin()和end()不适用于const_iterator。
编辑:显然begin()和end()将返回const_iterator。我从来没有为此目的使用过它们,由于添加了清晰性和强制返回类型,我一直使用cbegin()和cend()。我想每个人都有自己的。
注意:为了简化,您可以使用c++11中的新auto关键字。
template <typename T> void LogFile::put(std::string const & header, std::vector<T> const & data) {
output_file << header << " " << std::scientific << data[0] << std::endl;
for (auto value = (data.cbegin()+1); value != data.cend(); ++value) {
output_file << *value << std::endl;
}
}
相关文章:
- 为什么所有C++编译器都会崩溃或挂起此代码
- 为C++03编译器编写部分unique_ptr,该编译器与较新的编译器在公共代码库上运行
- 与其他编译器相比,相同的代码在工作室Microsoft提供不同的输出
- Visual Studio 中是否有来自代码块的编译器标志的类似物?
- C++ 代码的行为因编译器而异
- 代码未在联机编译器上显示结果
- 在 Azure DevOps 构建管道中使用英特尔C++编译器为 Linux 环境构建C++代码
- 编译器资源管理器(godbolt)如何安全地运行代码?
- 使用特征C++需要哪个代码块编译器?
- GCC 编译器是否应该对涉及 [[fallthrough]] 属性的格式错误的C++代码进行诊断?
- boost::spirit--试图编译大多数简单代码的编译器错误
- 使用其他编译器编译QT代码
- 使用 Mac 终端编译器编译 c++14 代码
- 我最近更改了编译器路径以运行 c++ 代码,但现在我无法运行任何 python 代码。我该如何解决这个问题?
- 编译器是否C++具有相同模板参数集的每个模板类实例生成代码?
- 为什么当我的代码超出函数范围时,"does not name a type"出现编译器错误?
- 如何获得代码::块的C++代码编译器?
- 提升精神代码.编译器错误
- 代码编译器-如何编译ASM代码
- C/C++ C# 代码编译器