2015-03-02 68 views
0

所以我試圖從用戶獲取輸入,然後將我的類客戶的對象命名爲與來自用戶的輸入相同。當我這樣做,我得到了錯誤使用變量聲明對象的名稱

"main.cpp: In function ‘void getinfo(String)’: main.cpp:34:13: error: declaration of ‘customer x’ shadows a parameter customer x(firstname, lastname, account, pinnum, balance, accthist);

我需要一種方法來自動定製類的名稱,可以由指定變量,它可以被修改或由用戶輸入。

*****更新****** 我確實想要很簡單。這是一個學校項目,目的是由兩個或三個人進行,我現在正在獨奏。以下是該作業的鏈接。我將其包括在內,以防有人可能有更好的方式使用我的代碼,或者解決我正在解決的問題的更好方法。

assignemnt

void getinfo(string x){ 
//----------Variable Declarations-------------- 

    string firstname; 
    string lastname; 
    string account; 
    int pinnum; 
    double balance; 
    vector <int> accthist; 


//----------Get Last Name---------------------------- 
    cout<<"Please enter your last name\n"; 
    cin>>lastname; 
//----------Get First Name---------------------------- 
    cout<<"Please enter your first name\n"; 
    cin>>firstname; 
//----------Get Account Number---------------------------- 
    cout<<"Please enter your desired account number\n"; 
    cin>>account; 
//----------Get Pin Number---------------------------- 
    cout<<"Please enter your desired pin number\n"; 
    cin>>pinnum; 
//----------Get First Deposit of $1,000.00-------------- 
    cout<<"Please enter your desired balance\n"; 
    cin>>balance; 

//----------create customer---------------------------- 
    customer x(firstname, lastname, account, pinnum, balance, accthist); 
} 



int main(){ 
    int choice; 
    cout<<"\t\t Please enter the number corresponding to your selected action.\n\n"; 
    cout<<"1) Open a New Account (Minimum $1,000.00 Deposit)\t"<<"2) Close an Existing Account\n"; 
    cout<<"3) Make a Withdraw ($50.00 -$500.00)\t\t\t"<<"4) Make a Deposit\n"; 
    cout<<"5)Check Account Balance\t\t\t\t\t"<<"6) Bank Statistics Menu\n"; 
    cin>>choice; 

    if (choice == 1){ 
     string test; 
     cout<<"Please enter your first name"; 
     cin>>test; 
     getinfo(test); 

    } 
    /*if (choice == 2){ 

    } 
    if (choice == 3){ 

    } 
    if (choice == 4){ 

    } 
    if (choice == 5){ 

    } 
    if (choice == 6){ 

    } 
    */else cout<<"Oops!"; 
return 0; 
} 

HEADER FILE- functions.h

#include "std_lib_facilities_4.h" 

//--------------------------------------------------------------------- 
class customer{ 
    private: 
     string firstn; 
     string lastn; 
     string acct; 
     int pin; 
     double bal; 
     vector <int> lastten; 

    public: 
    customer(string t1,string t2,string t3, int t4, double t5, vector <int> t6); 
    void deposit(double n){}; 
    void withdraw (double n){}; 
    double get_bal(){}; 

}; 
//--------------------------------------------------------------------- 
class stats{ 
    double avg_bal();  //average balance 
    double total_deposits();  //sum of all account balances 
    int total_cust(); //total number of customers 
}; 
//--------------------------------------------------------------------- 
class bank{ 
    private: 
     vector <string> allcust; 

    public: 
     void display_cust_account(); 
     bool verify_cust(); 
     void create_new_acct(string temp); 
     void check_maintenance_fee(); 
     void read_cust_accounts_from_file(); 
     void save_cust_account_to_file(); 
     void make_backup_file(); 
     void print_stats(); 
}; 
+0

請填寫完整代碼。你有兩個同名的變量嗎? – amchacon 2015-03-02 21:10:11

+0

假設你能夠弄清楚如何做到這一點(順便說一句,你不能),你究竟如何計劃在你的代碼中引用這個動態變量名?你甚至想象這樣的代碼可能看起來像什麼?它必須通過'x'變量完成。但是,如果你打算使用'x'變量,那麼只需將'x'作爲變量的靜態名稱,因爲變量名在運行時不可見。 – 2015-03-02 21:14:54

+0

嗯,我想知道是否可以通過使用指針和引用來使用'new'函數來命名對象。如'customer * p = new customer(blah,blah);'或者類似的東西。我會把這個客戶放到一個數據庫中,我可以通過名字,姓氏和社交標記他們。 – claywd 2015-03-03 16:13:23

回答

1

"main.cpp: In function ‘void getinfo(String)’: main.cpp:34:13: error: declaration of ‘customer x’ shadows a parameter customer x(firstname, lastname, account, pinnum, balance, accthist);

無論你想達到這個代碼

(我嚴重懷疑它使任何意義上固定之後簡單的錯誤) ,這是非常明顯的錯誤消息說:

customer x(firstname, lastname, account, pinnum, balance, accthist); 
     //^

從上面的行中的變量名x相同用於函數的參數

void getinfo(string x){ 
       //^

這就是陰影其實就是在這種背景下。


因此,要解決這個問題,你選擇一個不同的名稱爲其中任意一個,像

customer y(firstname, lastname, account, pinnum, balance, accthist); 
     //^

void getinfo(string y){ 
       //^
+0

不過,這將是一個noop – 2015-03-02 21:18:31

+0

@DieterLücking引用自己:「無論你想用這個代碼實現什麼......」(我現在VTC的問題現在) – 2015-03-02 21:19:31

+0

我明白錯誤,我把它包括了徹底不是解釋。 我需要的是一種在類中創建對象的方法......使用不同的名稱和基於用戶輸入的名稱。任何想法如何做到這一點,或者如果可能的話? – claywd 2015-03-03 16:09:50

0

我建議你將你的getInfo功能的方法裏面你customer班。

class Customer 
{ 
    string firstname; 
    string lastname; 
    string account; 
    int pinnum; 
    double balance; 
    vector <int> accthist; 
    public: 
    void getInfo(void); 
}; 

void Customer::getInfo() 
{ 
//----------Get Last Name---------------------------- 
    cout<<"Please enter your last name\n"; 
    cin>>lastname; 
//----------Get First Name---------------------------- 
    cout<<"Please enter your first name\n"; 
    cin>>firstname; 
//----------Get Account Number---------------------------- 
    cout<<"Please enter your desired account number\n"; 
    cin>>account; 
//----------Get Pin Number---------------------------- 
    cout<<"Please enter your desired pin number\n"; 
    cin>>pinnum; 
//----------Get First Deposit of $1,000.00-------------- 
    cout<<"Please enter your desired balance\n"; 
    cin>>balance; 
} 

int main(void) 
{ 
    Customer person; 

    // Print prompt and get the customer information. 
    person.getInfo(); 

    //... 
    return EXIT_SUCCESS; 
} 
+0

我想這會幫助,但我的目標是動態地創建我的課程的對象,我開始認爲這是不可能的。使用您的編輯代碼,我希望創建人,因爲用戶的名字是人。 這有道理嗎? – claywd 2015-03-03 16:09:22

+0

要動態創建對象,請使用'operator new'和一個智能或唯一的指針。程序的架構(組織)與其執行不同。 – 2015-03-03 16:43:59

+0

'客戶* x =新客戶(客戶名稱,帳戶,品牌,平衡,accthist);' 那樣? – claywd 2015-03-03 17:07:30