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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:55:26+00:00 2026-05-25T13:55:26+00:00

I have a fairly simple class that looks like this: class Person { public:

  • 0

I have a fairly simple class that looks like this:

class Person {
    public:

        Person(string name): _name(name) {};

        void greet(const Person& person) const {
            cout << "Hello, " <<  person._name << "!" << endl;
        };

    private:
        string _name;
};

Note that the greet method takes a parameter of the Person type. When I pass it a Person object, it works as expected. Now let’s pass it a string as a parameter in this way:

Person maher("maher");
maher.greet("sam");

When trying to run that code in QT (on a machine running ubuntu), it generates the following error:
no matching function for call to ‘Person::greet(const char [4])’

I was able to resolve this error by casting the string in this way: maher.greet(string("sam"));

My question is the following: Why can’t c++ ‘see’ that I’m passing a string to the greet method? Does it have anything to do with the fact that the greet method accepts a Person object?

  • 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-25T13:55:26+00:00Added an answer on May 25, 2026 at 1:55 pm

    maher is a const char[6], and sam is a const char[4], and both decay to const char * implicitly, but none of them is actually a std::string.

    In function calls, the C++ standard allows an implicit conversion to be performed if there’s a non-explicit constructor of the target type that accepts the type of the actual value passed to the function.

    This is what happens when you call the constructor: you pass a const char[6], which automatically decays to a const char *; the target type is std::string, which has a constructor that accepts a const char *; such constructor is called, and the Person constructor correctly receives his std::string parameter.

    In the second case, this is not happening: Person does not have a constructor that accepts a const char *, but only a constructor that accepts a std::string. To reach the desired Person type the compiler would have to first convert the const char * to std::string, and then call the Person constructor. This double conversion is not allowed, mainly because overloading resolution would become a complete mess (which already is) with lots of ambiguous cases.

    If you want to allow greet to be called with a C-style string you should either:

    • create a constructor for Person which accept a C-style string (const char *), so that it can be constructed directly from a const char *, without going through the prohibited extra conversion

    • create another overload for greet to accept an std::string.

    On the other hand, IMO the cleaner alternative is just leave it as it is; the caller will just have to write

    maher.greet(std::string("sam"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have a simple structure that looks like this: public class Range {
I have a XML Structure that looks like this. <sales> <item name=Games sku=MIC28306200 iCat=28
I have a fairly simple class that I want to save to SQL Server
Fairly simple question: I have an init method on my class that has the
I have a fairly simple model: public class Delivery { public int DeliveryId {
I have a very simple class public class SimpleClass // abreviated for this example
I have a fairly simple const struct in some C code that simply holds
I have a fairly simple block of code. Sub Run(Name) on error resume next
I have a fairly simple python loop that calls a few functions, and writes
So this question is fairly simple, and I feel like it's kind of an

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.