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

C++ 在Windows上使用GCC的代码出现编译器错误,c++,gcc,for-loop,compiler-errors,C++,Gcc,For Loop,Compiler Errors,我有一段简单的代码,它给了我一个编译器错误。我在VisualStudio下的windows环境中编译和运行它没有问题,但现在在linux下,使用gcc,我遇到了问题。注意:我使用的是GCC4.4.5,并且使用的是-std=c++0x指令 此代码段位于头文件_handling.h中,其中包含所有必要的库(vector、string、fstream等)。变量“output_file”是LogFile对象的一个成员,并在其他地方得到正确的检查/实例化/etc。代码本身非常简单,这就是我感到困惑的原因:

我有一段简单的代码,它给了我一个编译器错误。我在VisualStudio下的windows环境中编译和运行它没有问题,但现在在linux下,使用gcc,我遇到了问题。注意:我使用的是GCC4.4.5,并且使用的是-std=c++0x指令

此代码段位于头文件_handling.h中,其中包含所有必要的库(vector、string、fstream等)。变量“output_file”是LogFile对象的一个成员,并在其他地方得到正确的检查/实例化/etc。代码本身非常简单,这就是我感到困惑的原因:

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;
  }

}
模板无效日志文件::put(std::字符串常量和头,std::向量常量和数据){

输出文件正确的格式应为:

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;
  }

}
模板无效日志文件::put(std::字符串常量和头,std::向量常量和数据){

输出文件我以为vector会为我引入迭代器,因为::const_迭代器是它的一个属性,并且它在Visual Studio中一直工作。也就是说,我添加它只是为了测试您的想法,不幸的是,这并没有解决它。不过,这是个好主意。请尝试使用cbegin()而不是begin()。begin()不适用于常量迭代器。您还需要cend()而不是end()。真的吗?建议使用begin()如果需要,可以返回常量迭代器。不管怎样,我尝试了你的建议,它stll给出了相同的编译器错误。这似乎解决了它。所以我猜Visual Studio对我来说很容易,并采用了typename关键字?我以前使用过该语法,但从未使用过,但只在VS2010版本的代码上使用过。为什么typename关键字required?为什么不清楚std::vector::const_迭代器是一种类型?@Avacar:不,不清楚,因为部分专门化。例如,如果
T
bool
,事情会很快变得非常奇怪。啊,这是因为模板。如果这是std::vector::const_迭代器,它会有类似的东西吗e同样需要typename?似乎在解释它,至少在模板化的函数和类中是这样。这是因为它在模板中,还是因为vector本身就是模板?@Avacar:看到了吗
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;
  }

}
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;
  }

}
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;
  }

}