Question

I am programming an assignment currently and I'm mostly new to C++. I do understand though that you need to have defined a function before you can use it, or use a header file to prototype the functions before using them in the .cpp file.

However, doing either of these is still throwing the error that the methods are undefined.

This is my messy header file...

#ifndef DRIVER_H
#define DRIVER_H
#include "Account.h"
#include <vector>
#include <iostream>
#pragma once

using namespace std;

class Driver {

private:

public:
void prevAccount(vector<Customer> customers);
void nextAccount(vector<Customer> customers);
void accountActions(vector<Customer> customers);
void listCustomerAccounts(vector<Customer> customers);
void selectAccount(vector<Customer> customers);
void createAccount(vector<Customer> customers);
void prevCustomer(vector<Customer> customers);
void nextCustomer(vector<Customer> customers);
void customerActions(vector<Customer> customers);
void listCustomers(vector<Customer> customers);
void selectCustomer(vector<Customer> customers);
void createCustomer(vector<Customer> customers);
void customerMenu(vector<Customer> customers);
void main();
};

#endif

And here is the .cpp file for the Driver which essentially runs the program...

#include "Account.h"
#include "Customer.h"
#include "Driver.h"
#include "JuniorCurrentAccount.h"
#include "CorporateSavingsAccount.h"
#include "StudentSavingsAccount.h"
#include "CurrentAccount.h"
#include "Transaction.h"
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

static int customerIndex = 0;
static int accountIndex = 0;
static int accNum = 1;


//tier 5
void Driver::accountActions(vector<Customer> customers) { //code }
void Driver::prevAccount(vector<Customer> customers) { //code }

//tier 4
void Driver::createAccount(vector<Customer> customers) { //code }
void Driver::selectAccount(vector<Customer> customers) { //code }
void Driver::listCustomerAccounts(vector<Customer> customers) { //code }

//tier 3
void Driver::customerActions(vector<Customer> customers) { //code }
void Driver::nextCustomer(vector<Customer> customers) { //code }
void Driver::prevCustomer(vector<Customer> customers) { //code }

// tier 2
void Driver::createCustomer(vector<Customer> customers) { //code }  
void Driver::selectCustomer(vector<Customer> customers) { //code }
void Driver::listCustomers(vector<Customer> customers) { //code }

//tier 1
void Driver::customerMenu(vector<Customer> customers) { //code }

void main() 
{
vector<Customer> customers;

cout << "________________________" << endl;
cout << "//MAIN MENU " << endl;
cout << "||Customers (1) " << endl;     
cout << "||";

int mainMenuChoice;
cin >> mainMenuChoice;

if (mainMenuChoice == 1)
{
    customerMenu(customers);
}
}

The very bottom of the .cpp file is where the error is complaining about, really not understanding why this error is appearing since I think I've covered the definitions.

I've taken the code out of the method bodies to save space, but they essentially perform a function OR call a method in a tier higher (1 calls to 2, 2 calls to 3).

Was it helpful?

Solution

It would help if you had posted the text of the error message, but it looks like the problem is that you have defined your functions in a class called Driver, but there is no Driver object in your main function.

Try this

int main() 
{
    Driver driver;
    vector<Customer> customers;

    cout << "________________________" << endl;
    cout << "//MAIN MENU " << endl;
    cout << "||Customers (1) " << endl;     
    cout << "||";

    int mainMenuChoice;
    cin >> mainMenuChoice;

    if (mainMenuChoice == 1)
    {
        driver.customerMenu(customers);
    }
}

OTHER TIPS

The function customerMenu is not a free-standing function, it's a member of the Driver class. That means you need a Driver object instance first, that you call the function on:

Driver myDriver;
myDriver.customerMenu(customers);

If you don't understand why you need this, I think you might want to go back to some basic tutorial.


As an extra tip: After the basics, you might want to learn about how to pass arguments by reference. Because right now you are passing the customers vector by value meaning it is copied for each function call you make. It also means that each function have its own copy and modifications will be made on this private copy and not the vector passed in as an argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top