在Windows上使用GCC的代码出现编译器错误

Compiler Error on code with GCC that worked on Windows

本文关键字:代码 编译器 错误 GCC Windows      更新时间:2023-10-16

我有一段简单的代码,它给了我一个编译器错误。我在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;
  }
}