2013-11-28 115 views
2

我正在編寫一個任務,我大部分都是C++新手。但我確實明白,在使用它之前需要定義一個函數,或者在使用它們到.cpp文件之前使用頭文件對函數進行原型設計。標識符「customerMenu」未定義

但是,這樣做無論是對這些仍然是引發錯誤的方法是不確定的。

這是我凌亂的頭文件...

#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)。

回答

3

這將幫助,如果你曾發佈錯誤消息的文本,但它看起來像問題是,你在一個類中調用驅動程序定義的功能,但在你的主要功能無驅動程序對象。

試試這個

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); 
    } 
} 
+0

這很有道理,我假設Driver會被自動假定爲主類,因此不需要初始化(因爲它是Java),但我不知道我需要專門初始化它。謝謝! – Azzah

2

功能customerMenu不是一個獨立的功能,它是Driver類的成員。這意味着你需要一個Driver對象實例第一,你調用該函數:

Driver myDriver; 
myDriver.customerMenu(customers); 

如果你不明白你爲什麼需要這個,我想你可能想回去一些基本的教程。


作爲一個額外的提示:基礎知識後,您可能希望瞭解如何通過參考來傳遞參數。因爲現在你正在通過customers向量的值,這意味着它被複制了你所做的每個函數調用。這也意味着每個函數都有自己的副本,並且將在此私有副本上進行修改,而不是作爲參數傳入的向量。

+0

我會假設,編譯器會明白Driver.cpp作爲我的主類,但顯然不是。我甚至還沒有完成任何基礎教程,我正在基於我對Java的知識進行編程。我們剛剛被拋入深處,所以我盡我所能。關於引用和指針,我將首先得到基本的實現,然後正確地瞭解這些。感謝您的答案,現在有道理! – Azzah