简体   繁体   English

编译器错误代码与GCC在Windows上工作

[英]Compiler Error on code with GCC that worked on Windows

I have a simple piece of code that is giving me a compiler error. 我有一段简单的代码,它给了我一个编译器错误。 I've had no issues compiling and running this in a windows environment under Visual Studio, but now under linux, using gcc, I am having problems. 我在Visual Studio下的Windows环境中编译和运行它没有任何问题,但现在在linux下,使用gcc,我遇到了问题。 Note I am using gcc 4.4.5 , and using the -std=c++0x directive. 注意我使用的是gcc 4.4.5 ,并使用-std = c ++ 0x指令。

This code snippet is in a header file, file_handling.h, which does include all the necessary libraries (vector, string, fstream, etc). 此代码段位于头文件file_handling.h中,其中包含所有必需的库(vector,string,fstream等)。 The variable 'output_file' is a member of the LogFile object, and gets properly checked/instantiated/etc elsewhere. 变量'output_file'是LogFile对象的成员,并在其他地方正确检查/实例化/ etc。 The code itself is trivially simple, which is why I am stumped: 代码本身很简单,这就是我难倒的原因:

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

}

The compiler states: 编译器声明:

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

Why does gcc not see the in-situ declaration of 'value' as a const_iterator? 为什么gcc没有看到'value'的原位声明为const_iterator? I've tried the following as a sanity check: 我已经尝试了以下作为一个完整性检查:

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

}

And receive the exact same compiler report. 并收到完全相同的编译器报告。 Given this looks simple, and worked fine in Visual Studio, what am I missing or misunderstanding about gcc and/or a Linux environment? 鉴于这看起来很简单,并且在Visual Studio中运行良好,我对gcc和/或Linux环境有什么遗漏或误解?

The correct form should be: 正确的形式应该是:

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

}

Note the addition of typename, and the changes from begin() and end() to cbegin() and cend(). 注意添加typename,以及从begin()和end()到cbegin()和cend()的更改。

typename is required when you're using a templated type. 使用模板化类型时需要typename。 begin() and end() are not for const_iterators. begin()和end()不适用于const_iterators。

Edit: Apparently begin() and end() will return const_iterators. 编辑:显然begin()和end()将返回const_iterators。 I'd never used them for that purpose and always used cbegin() and cend() due to the added clarity and forced return types. 我从来没有将它们用于那个目的,并且总是使用cbegin()和cend(),因为增加了清晰度和强制返回类型。 To each his own I guess. 我猜他自己。

Note: To simplify, you can use the new auto keyword from c++11. 注意:为简化起见,您可以使用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;
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM