Zane
Zane

Reputation: 23

C++ class instance declaration no matching function

I'm practicing OOP in C++ and I'm creating a bank account class. Here is the class definition:

#include <iostream>
#include <cmath>
#include <limits>
#include <string>
#include <cstring>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>

using namespace std;

const int acc_num_length = 8;
const int acc_id_num_length = 13;

class bankAccount {
private:
    int acc_num;    // account number
    int acc_id_num; // account holder ID number
    string acc_tel_no;     // account holder telephone number
    string acc_first_name; // account holder first name
    string acc_last_name;  // account holder last name
public:
    /* constructor */
    bankAccount() {
        acc_num = 0;
        acc_id_num = 0;
        acc_tel_no = "";
        acc_first_name = "";
        acc_last_name = "";
    }
    /* detail retrieval */
    int get_acc_num() {
        return(acc_num);
    }
    int get_acc_id_num() {
        return(acc_id_num);
    }
    string get_acc_tel_no() {
        return(acc_tel_no);
    }
    string get_acc_first_name() {
        return(acc_first_name);
    }
    string get_acc_last_name() {
        return(acc_last_name);
    }
};

Now in my main program I'm trying to declare an instance of this class,

int main() {

    float acc_num = 0;
    while ( ((cout << "Enter account number: ") && !(cin >> acc_num)) || (floor(acc_num) < acc_num) ) {
        cout << "Invalid account number.\n";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    int acc_num_int = acc_num;
    string acc_num_string = to_string(acc_num_int);
    while ( acc_num_string.length() != acc_num_length ) {
        cout << "Invalid account number (8 characters only).\n";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cin >> acc_num;
        cout << "Enter account number: \n";
    }

    bankAccount ba1 = bankAccount(acc_num_int,0, "","","");
    bankAccount ba2 = bankAccount();

    return 0;
}

Now everything works except my instance declarations,

C:/Cartrack/C++/Creating a form/Form/main.cpp: In function 'int main()': C:/Cartrack/C++/Creating a form/Form/main.cpp:68:67: error: no matching function for call to 'bankAccount::bankAccount(int&, long long int, const char [1], const char [1], const char [1])'
bankAccount ba1 = bankAccount(acc_num_int,0, "","",""); ^ C:/Cartrack/C++/Creating a form/Form/main.cpp:25:2: note: candidate: bankAccount::bankAccount() bankAccount() { ^~~~~~~~~~~ C:/Cartrack/C++/Creating a form/Form/main.cpp:25:2: note: candidate expects 0 arguments, 5 provided C:/Cartrack/C++/Creating a form/Form/main.cpp:16:7: note: candidate: bankAccount::bankAccount(const bankAccount&) class bankAccount { ^~~~~~~~~~~ C:/Cartrack/C++/Creating a form/Form/main.cpp:16:7: note: candidate expects 1 argument, 5 provided C:/Cartrack/C++/Creating a form/Form/main.cpp:16:7: note: candidate: bankAccount::bankAccount(bankAccount&&) C:/Cartrack/C++/Creating a form/Form/main.cpp:16:7: note: candidate expects 1 argument, 5 provided mingw32-make.exe[1]: * [Form.mk:97: Debug/main.cpp.o] Error 1 mingw32-make.exe[1]: Leaving directory 'C:/Cartrack/C++/Creating a form/Form' mingw32-make.exe: * [Makefile:5: All] Error 2 ====1 errors, 6 warnings====

I'm not sure why there's no matching call. I'm passing it two ints and three strings, which seems to be the mistake most people make when I've read through the existing threats.

Can anyone help please? Thanks!

Zane

Upvotes: 2

Views: 15134

Answers (1)

paper.plane
paper.plane

Reputation: 1197

You need to declare parameterized constructors as per your need. All you have is default(or zero-argument) constructor. Please read the constructor section of any C++ book/tutorial.

Moreover, you should not create objects like

 bankAccount ba1 = bankAccount(acc_num_int,0, "","","");

Above statement will create a nameless object and do copy construction of ba1. Please read about copy constructors. When you need a single object this is an overload for extra function call. Instead you can create your object like below

bankAccount ba1(acc_num_int,0, "","","");

Upvotes: 3

Related Questions