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 139013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:21:32+00:00 2026-05-11T07:21:32+00:00

I need to write a templated function replace_all in C++ which will take a

  • 0

I need to write a templated function replace_all in C++ which will take a string, wstring, glibmm::ustring etc. and replace all occurrences of search in subject with replace.

replace_all.cc

template < class T > T replace_all(         T const &search,         T const &replace,         T const &subject ) {         T result;          typename T::size_type done = 0;         typename T::size_type pos;         while ((pos = subject.find(search, done)) != T::npos) {                 result.append (subject, done, pos - done);                 result.append (replace);                 done = pos + search.size ();         }         result.append(subject, done, subject.max_size());         return result; } 

test.cc

#include <iostream>  template < class T > T replace_all(         T const &search,         T const &replace,         T const &subject );  // #include 'replace_all.cc'  using namespace std;  int main() {         string const a = 'foo bar fee boor foo barfoo b';         cout << replace_all<string>('foo', 'damn', a) << endl;         return 0; } 

When I try to compile this using gcc 4.1.2

g++ -W -Wall -c replace_all.cc   g++ -W -Wall -c test.cc   g++ test.o replace_all.o   

I get:

test.o: In function `main': test.cc:(.text+0x13b): undefined reference to `    std::basic_string<char, std::char_traits<char>, std::allocator<char> >    replace_all< std::basic_string<char, std::char_traits<char>, std::allocator<char> > >(        std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&,        std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&,        std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&    ) ' collect2: ld returned 1 exit status 

But when I uncomment #include 'replace_all.cc' in test.cc and compile this way:

g++ -W -Wall test.cc 

The program links and produces expected output:

damn bar fee boor damn bardamn b 

Why linking fails and what can I do to make it work?

  • 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. 2026-05-11T07:21:33+00:00Added an answer on May 11, 2026 at 7:21 am

    You can’t link templates as compiler don’t know which code to generate before someone tries to use ( instantiate ) templates.

    You can ‘ask’ compiler to instantiate template if you knows which types are you going to use or if you know that they are limited.
    If you want – put this to your .cc file:

    template std::string replace_all( std::string const& search,                                   std::string const& replace,                                   std::string const& subject );   template glibmm::ustring replace_all( glibmm::ustring const& search,                                       glibmm::ustring const& replace,                                       glibmm::ustring const& subject ); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 113k
  • Answers 113k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use this to check if there are any spans with… May 11, 2026 at 10:08 pm
  • Editorial Team
    Editorial Team added an answer Putting the parentheses around data_from_the_wire is effectively equivalent to stuff… May 11, 2026 at 10:08 pm
  • Editorial Team
    Editorial Team added an answer List is an ordered sequence of elements whereas Set is… May 11, 2026 at 10:08 pm

Related Questions

Let's suppose I have a struct like this: struct my_struct { int a; int
It is a university task in my group to write a compiler of C-like
For a system I need to convert a pointer to a long then the
I need to write a Perl script to read in a file, and delete

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.