所以我試圖從用戶獲取輸入,然後將我的類客戶的對象命名爲與來自用戶的輸入相同。當我這樣做,我得到了錯誤使用變量聲明對象的名稱
"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);
我需要一種方法來自動定製類的名稱,可以由指定變量,它可以被修改或由用戶輸入。
*****更新****** 我確實想要很簡單。這是一個學校項目,目的是由兩個或三個人進行,我現在正在獨奏。以下是該作業的鏈接。我將其包括在內,以防有人可能有更好的方式使用我的代碼,或者解決我正在解決的問題的更好方法。
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();
};
請填寫完整代碼。你有兩個同名的變量嗎? – amchacon 2015-03-02 21:10:11
假設你能夠弄清楚如何做到這一點(順便說一句,你不能),你究竟如何計劃在你的代碼中引用這個動態變量名?你甚至想象這樣的代碼可能看起來像什麼?它必須通過'x'變量完成。但是,如果你打算使用'x'變量,那麼只需將'x'作爲變量的靜態名稱,因爲變量名在運行時不可見。 – 2015-03-02 21:14:54
嗯,我想知道是否可以通過使用指針和引用來使用'new'函數來命名對象。如'customer * p = new customer(blah,blah);'或者類似的東西。我會把這個客戶放到一個數據庫中,我可以通過名字,姓氏和社交標記他們。 – claywd 2015-03-03 16:13:23