我目前正在编写一个作业,而且我是C ++的新手。我确实理解你需要在使用之前定义一个函数,或者在.cpp文件中使用它们之前使用头文件来构建函数原型。
但是,执行其中任何一项仍然会导致方法未定义的错误。
这是我凌乱的头文件......
#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
这是驱动程序的.cpp文件,它基本上运行程序......
#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);
}
}
.cpp文件的最底部是错误抱怨的地方,真的不明白为什么会出现这个错误,因为我认为我已经涵盖了这些定义。
我从方法体中取出代码以节省空间,但它们实际上执行一个函数或调用更高层中的方法(1次调用2,2次调用3)。
答案 0 :(得分:3)
如果您发布了错误消息的文本会有所帮助,但看起来问题是您已经在名为Driver的类中定义了您的函数,但主函数中没有Driver对象。
试试这个
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);
}
}
答案 1 :(得分:2)
函数customerMenu
不是独立函数,它是Driver
类的成员。这意味着您首先需要一个Driver
对象实例,您可以调用该函数:
Driver myDriver;
myDriver.customerMenu(customers);
如果你不明白为什么需要这个,我想你可能想回到一些基础教程。
作为额外提示:在基础知识之后,您可能想要了解如何通过引用传递参数。因为您现在正在通过值传递customers
向量,这意味着它会为您进行的每个函数调用复制。它还意味着每个函数都有自己的副本,并且将在此私有副本上进行修改,而不是作为参数传入的向量。