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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:08:20+00:00 2026-06-05T10:08:20+00:00

I want to create a public api that takes a string as an argument

  • 0

I want to create a public api that takes a string as an argument and places this string at the loaction where I have placed a format specifer in another string in that Api.

e.g. string PrintMyMessage( const string& currentValAsString)
     {
          string s1("Current value is %s",currentValAsString);
          return s1;
     }

Currently I am getting following build Error.

1>d:\extra\creatingstrwithspecifier\creatingstrwithspecifier\main.cxx(8): error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &,unsigned int,unsigned int)' : cannot convert parameter 2 from 'const std::string' to 'unsigned int'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

I just want to know what could be a better way to accomplish this task.

  • 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-05T10:08:22+00:00Added an answer on June 5, 2026 at 10:08 am

    As discussed also in this rather similar question, you could use the Boost Format Library. For example:

    std::string PrintMyMessage( const string& currentValAsString )
    {
      boost::format fmt = boost::format("Current value is %s") % currentValAsString; 
      // note that you could directly use cout:
      //std::cout << boost::format("Current value is %s") % currentValAsString;
      return fmt.str();
    }
    

    In the answers to the other question you can also find other approaches, e.g. using stringstreams, snprintf, or string concatenation.

    Here’s a complete generic example:

    #include <string>
    #include <iostream>
    #include <boost/format.hpp>
    
    std::string greet(std::string const & someone) {
        boost::format fmt = boost::format("Hello %s!") % someone;
        return fmt.str();
    }
    
    int main() {
        std::cout << greet("World") << "\n";
    }
    

    Or, if you can’t or don’t want to use Boost:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <cstdio>
    
    std::string greet(std::string const & someone) {
        const char fmt[] = "Hello %s!";
        std::vector<char> buf(sizeof(fmt)+someone.length());
        std::snprintf(&buf[0], buf.size(), fmt, someone.c_str());
        return &buf[0];
    }
    
    int main() {
        std::cout << greet("World") << "\n";
    }
    

    Both examples produce the following output:

    $ g++ test.cc && ./a.out
    Hello World!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create whois class like that public class DomainInfo { public string
I have a link from that link: http://api.maxxiscentral.com/maxxiscentral/GetTyrePatterns from this link i want to
i want create image animation , i have 50 images with png format now
I want to create a class with a nested enum. public class Foo {
I want to create a template class in C#, for example: public class Foo<T>
I wanna create a public repo (bare repo) which contains multiple submodules. I want
I have created a public static class utils.cs I want to use it in
I have a Service that downloads a file from the internet. What I want
I have a java project I want to create which will be built on
There are many java standard and 3rd party libraries that in their public API,

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.