2

I have a class BankAccount with two string members - name and num. What I want is to assign values to these objects when I create them (when the constructor is called). However the compiler says No instance of constructor matches the argument list when I try to create an object. I would like to ask why is that?

// hwk-2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
class BankAccout {
    char name[23];
    char num[15];
    double sum;
public:
    BankAccout(char *nm, char *nr, double s) {
        strcpy(name,nm);
        strcpy(num, nr);
        sum = s;
    }


};
int main()
{
    BankAccout k("Peter", "0403940940", 34.21);

}
5
  • 4
    Use const char* instead of char* in the constructor parameters. String literals are const char[] values, which decay to const char*, not to char*. Though, you really should be using std::string instead of char[] arrays to begin with. This is C++, not C, afterall Commented Oct 9, 2018 at 20:03
  • 4
    Why does getName() return a std::string but you aren't using one anywhere else? Everywhere you have a c-string you should be using a std::string. Commented Oct 9, 2018 at 20:05
  • and dont do using namespace std Commented Oct 9, 2018 at 20:06
  • 3
    why the downvote people? Its a great question. Its clear, it says what the issue is, it includes the complete source code needed to reproduce. I wish all rep == 1 questions were like this Commented Oct 9, 2018 at 20:09
  • people are still downvoting it! Commented Oct 9, 2018 at 20:40

3 Answers 3

1

as a coffee break exercise here is more idiomatic version

#include "pch.h"
#include <iostream>
#include <string>

class BankAccount {
    std::string name_;
    std::string num_;
    double sum_;
public:
    BankAccount(std::string name, std::string num, double sum) {
        name_ = name;
        num_ = num;
        sum_ = sum;
    }
};

int main()
{
    BankAccount k("Peter", "0403940940", 34.21);
}
Sign up to request clarification or add additional context in comments.

2 Comments

It would be even more idiomatic to use constructor initialization lists and move the string constructor parameters into the data members.
I agree this solution is nice. But it does not answer the question of the OP: Why does the compiler not find a constructor that matches his actual parameters. Which I think is a quite interesting question for beginners. But anyway, there is also much to learn from your idiomatic version, that is true as well.
1

The signature of the constructor does not match. This one will match:

BankAccount(const char *nm, const char *nr, double s);

EDIT:

The reason is the way you are calling the constructor in the main function. You are giving literal strings as parameters. These literals are const, you cannot change them at runtime. Thus you will pass pointers to const char*.

This is very obvious if you look at this opposing example. This is a way that would be compatible with the old signature BankAccout(char *nm, char *nr, double s);.

int main(int argc, char* argv[])
{
    char name[] = "hello";
    char number[] = "1234";

    std::cout << "name before: " << name << std::endl;

    BankAccount k(name, number, 8.5);

    // name and number are not const,
    // you can change them :
    name[2] = 'x';
    name[3] = 'x';

    std::cout << "name after: " << name << std::endl;

    return 0;
}

2 Comments

but dont do that anyway, use std::string everywhere
It would helpful to the OP and future readers if you could explain why this change is needed.
0

An even simpler version, if you don’t need to have additional functionality in the class: just use a struct.

#include <string>

struct BankAccount {
  std::string name;
  std::string number;
  double balance;
};

int main() {
  BankAccount account{"Joy", "44", 43.};
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.