Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8474357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:36:17+00:00 2026-06-10T17:36:17+00:00

I have a simple piece of code that is giving me a compiler error.

  • 0

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. Note I am using gcc 4.4.5, and using the -std=c++0x directive.

This code snippet is in a header file, file_handling.h, which does include all the necessary libraries (vector, string, fstream, etc). The variable ‘output_file’ is a member of the LogFile object, and gets properly checked/instantiated/etc elsewhere. 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? 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:36:19+00:00Added an answer on June 10, 2026 at 5:36 pm

    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 is required when you’re using a templated type. begin() and end() are not for const_iterators.

    Edit: Apparently begin() and end() will return 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. To each his own I guess.

    Note: To simplify, you can use the new auto keyword from c++11.

    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;
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple piece of Javascript code and I get this Error :
I have a simple piece of code that I'm not understanding where my error
I have this simple piece of code that returns what's in the title. Why
I have this simple piece of code that uses boost::bind: #include <boost/bind.hpp> #include <utility>
This must have a simple answer -- I have a piece of code that
I have this simple piece of code on a windows form containing said DataGridView
I have this simple piece of code : int a = 1, b =
I have this simple piece of code: MyObjectContext db = new MyObjectContext(); Person new_person
I have this simple piece of HTML code: <div> <input type=file name=english-file /> </div>
I've wrote this simple piece of code. And I have a slight problem with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.