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

  • Home
  • SEARCH
  • 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 3942266
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:38:31+00:00 2026-05-20T00:38:31+00:00

I am a newbie. I have tried to write a template function that gets

  • 0

I am a newbie. I have tried to write a template function that gets string AND numerical data (for example, douubles) from a text file, then puts it in a vector. Each element of the vector is a different name or number.

Last week I asked whether overloading or a template might be best for this. I would like to go with a template method, where the results vector (what I want) is passed to the function to provide the template parameter T. But I’m having problems. If anyone could help, I would be grateful! The code is below, followed by the error I’m getting.

// My code:

template<typename T>
void readFile( const std::string& name, const std::string& find, std::vector<T>& results ){
    std::ifstream file( name.c_str( ) );
    std::string   line;

    while( std::getline( file, line ) )
    {
        if( line == find )
        {
            std::getline( file, line );
            line.erase(remove( line.begin(), line.end(), '\'' ), line.end() );
            std::istringstream streamLine( line );

            results = std::vector<T>( std::istream_iterator<T>(streamLine), std::istream_iterator<T>() );
        }
    }
}

Call in main():

readFile( name, "label", results );

The error I’m getting is below. I don’t understand how the function call doesn’t match the definition. Apologies in advance for any dumb mistakes!

error: no matching function for call to 
'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >
::resize(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'

The weird thing is that if I make the function return the vector “result” instead of void, then things seem to work. Here’s what I mean:

std::vector<T> readFile( const std::string& name, const std::string& find, std::vector<T>& results )

with a return statement in the function definition of:

return std::vector<T>( std::istream_iterator<T>(streamLine), std::istream_iterator<T>() );

But it seems clunky/bad style to do it this way. I would have thought that using references would be better. Even if it isn’t better, I’m curious to know why the first method (with void) doesn’t work.

Any tips would be appreciated!

  • 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-05-20T00:38:31+00:00Added an answer on May 20, 2026 at 12:38 am

    I had to do a bunch of work to make this a fully compilable code snippet. Here is what I came up with:

    #include <string>
    #include <vector>
    #include <istream>
    #include <fstream>
    #include <iterator>
    #include <sstream>
    #include <algorithm>
    
    template<typename T>
    void readFile(const std::string& name,
                  const std::string& find,
                  std::vector<T>& results )
    {
       std::ifstream file( name.c_str( ) );
       std::string   line;
    
       while( std::getline( file, line ) )
       {
          if( line == find )
          {
             std::getline( file, line );
             line.erase(remove( line.begin(), line.end(), '\'' ), line.end() );
             std::istringstream streamLine( line );
    
             results = std::vector<T>( std::istream_iterator<T>(streamLine),
                                       std::istream_iterator<T>() );
          }
       }
    }
    
    void do_it_with_strings(std::vector<std::string> &results)
    {
       readFile("fred", "barney", results);
    }
    

    This code snippet compiles just fine with gcc 4.5.1. What are the exact types of name and results in your main function? Also, the problem seems to be calling the vector’s resize function. You don’t appear to be doing that directly in your code, though it’s possible the vector constructor or assignment operator does it internally.

    But regardless, I wouldn’t suggest you write your code that way anyway. I would suggest you do this instead:

    #include <string>
    #include <vector>
    #include <istream>
    #include <fstream>
    #include <iterator>
    #include <sstream>
    #include <algorithm>
    
    template<typename T>
    std::vector<T> readFile(const std::string& name, const std::string& find )
    {
       std::ifstream file( name.c_str( ) );
       std::string   line;
    
       while( std::getline( file, line ) )
       {
          if( line == find )
          {
             std::getline( file, line );
             line.erase(remove( line.begin(), line.end(), '\'' ), line.end() );
             std::istringstream streamLine( line );
    
             return std::vector<T>( std::istream_iterator<T>(streamLine),
                                    std::istream_iterator<T>() );
          }
       }
       return std::vector<T>();
    }
    
    void do_it_with_strings(std::vector<std::string> &results)
    {
       results = readFile<std::string>("fred", "barney");
    }
    

    In a function like this, I think it’s better to be explicitly stating the type you’re expecting to read when you call the function rather than have it implicitly determined from the type of the vector you pass in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier
Java Newbie here. I have a JFrame that I added to my netbeans project,
I am a newbie to Python and have come across the following example in
I'm a newbie to swing development. I have a swing app that needs to
This is a newbie question (I'm sure it is). I have tried for the
hey all.i'm newbie at this problem.i have this data in table result: item range_code
(Warning - asp newbie) I have an aspx file with the tag <%@ Page
I am a newbie to WPF and have a couple of questions about WPF
I'm a ClearCase newbie and up until now have been used to SVN. Therefore,
I'm a newbie to pgsql. I have few questionss on it: 1) I know

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.