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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:32:26+00:00 2026-06-09T21:32:26+00:00

noob here. Having a doubt while doing an exercise from a book. The doubt

  • 0

noob here. Having a doubt while doing an exercise from a book.

The doubt is the following: Why if I use a const std::string * as an argument to a method, the compiler sends me the following error: no matching function for call to ‘BankAccount::define(const char [11], const char [11], int)’ in the client?

If I swap the prototype and definition of the method to accept as an argument a const std::string & (like I did in the constructor) it’s no problem for the compiler.

Client:

// usebacnt.cpp --

#include "BankAccount.h"
#include <iostream>

int main()
{
    BankAccount john;
    BankAccount mary("Mary Wilkinson", "5000000000"); // balance set to 0 because of default argument
    john.display();
    mary.display();
    john.define("John Smith", "4000000000", 26); // error line
    mary.deposit(1000.50);
    john.withdraw(25);
    john.display();
    mary.display();
    std::cin.get();
    return 0;
}

Class declaration:

// BankAccount.h -- Header file for project Exercise 10.1

#ifndef BANKACCOUNT_H_
#define BANKACCOUNT_H_

#include <string>

class BankAccount
{
private:
    std::string fullName;
    std::string accountNumber;
    double balance;
public:
    BankAccount(); // default constructor
    BankAccount(const std::string &fN, const std::string &aN, double b = 0.0); // constructor with a default argument
    void display() const;
    void deposit(double amount);
    void withdraw(double amount);
    void define(const std::string *pfN, const std::string *paN, double b);
};

#endif

Class implementation:

// methods.cpp -- Compile alongside usebacnt.cpp

#include "BankAccount.h"
#include <iostream>

void BankAccount::display() const
{
    using std::cout;
    using std::ios_base;
    ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield); // formatting output and saving original
    cout.precision(2);
    cout << "Full Name: " << fullName << '\n';
    cout << "Account Number: " << accountNumber << '\n';
    cout << "Balance: " << balance << "\n\n";
    cout.setf(orig);
}

void BankAccount::deposit(double amount)
{
    if (amount < 0)
    {
        std::cout << "You can't deposit a negative number! Amount set to zero.";
        amount = 0;
    }
    balance += amount;
}

void BankAccount::withdraw(double amount)
{
    if (amount < 0)
    {
        std::cout << "You can't withdraw a negative number! Amount set to zero.";
        amount = 0;
    }
    if (balance < amount)
    {
        std::cout << "You can't withdraw more money than you have. Amount set to zero.";
        amount = 0;
    }
    balance -= amount;
}

void BankAccount::define(const std::string *pfN, const std::string *paN, double b)
{
    fullName = *fN;
    accountNumber = *aN;
    balance = b;
}

// constructors
BankAccount::BankAccount() // default constructor
{
    fullName = "empty";
    accountNumber = "empty";
    balance = 0.0;
}

BankAccount::BankAccount(const std::string &fN, const std::string &aN, double b) // constructor with default argument
{
    fullName = fN;
    accountNumber = aN;
    balance = b;
}

Shouldn’t the compiler interpret the literal as a string object (like it does with the reference)? So if I say it’s a pointer it should pass the address of the string (its first element).

Any help?

  • 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-09T21:32:27+00:00Added an answer on June 9, 2026 at 9:32 pm

    Shouldn’t the compiler interpret the literal as a string object (like it does with the reference)?

    No. Pointers are not references; smack anyone who tells you that they are.

    It all comes down to this: std::string is not a string literal. Therefore, when you pass a string literal to a function that takes a std::string, a temporary std::string that holds the contents of the literal must be created. This is done with the conversion constructor of std::string.

    A const& is allowed to be initialized from a temporary, which allows conversion constructors to work their magic. A const* must be a pointer, and you’re not supposed to get pointers to temporaries. Therefore, const* cannot be magically filled in by a temporary created from a conversion constructor.

    You should use a const std::string & instead of a pointer. Or a std::string value if you have C++11 move constructors available.

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

Sidebar

Related Questions

noob here. I'm doing an exercise from a book and the compiler reports no
so I am having a noob moment here, I haven't ever used the command
NOOb here. I've got a HTTP request that pulls all of the content from
c#/oop noob here. I am creating a datatable from the schema of a sql
Perl noob here - i have the following script if(substr($pc, 3,1)!= ){ $newpc =
A noob here. I have a personal Macbook and I want to use Git
Python noob here, Currently I'm working with SQLAlchemy, and I have this: from __init__
Rails noob here so I'm not sure what I'm doing wrong. We're replacing a
Groovy noob here, I'm working through my first Groovy book and it has example
Noob here getting stuck and probably doing something stupid and would appreciate some guidance.

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.