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);
}
const char*instead ofchar*in the constructor parameters. String literals areconst char[]values, which decay toconst char*, not tochar*. Though, you really should be usingstd::stringinstead ofchar[]arrays to begin with. This is C++, not C, afterallgetName()return astd::stringbut you aren't using one anywhere else? Everywhere you have a c-string you should be using astd::string.using namespace std