| 9 | 1/1 | 返回列表 |
| 查看: 2082 | 回復(fù): 8 | ||
lhy317409772銀蟲 (初入文壇)
|
[求助]
求大神幫忙解答! 已有1人參與
|
|
困擾了蠻久了....求大神幫忙解答程序錯在哪里....疑問都在程序旁邊注釋出來了。 新手貼,沒什么金幣,抱歉....今后努力賺金幣。 非常感謝..... ![]() 源程序如下: //頭文件 #include<iostream> class Port { private: char* brand; char style[20]; int bottles; public: Port(const char* br="none", const char* st="none", int b=0); Port(const Port& p); virtual ~Port() {delete [] brand;} Port& operator =(const Port& p); Port& operator+=(int b); Port& operator-=(int b); int BottleCount() const {return bottles;} virtual void Show() const; friend std: stream& operator<< (std: stream& os, const Port& p);}; class VintagePort: public Port { private: char* nickname; int year; public: VintagePort(); VintagePort(const char* br, int b, const char* nn, int y); VintagePort(const VintagePort& vp); ~VintagePort() {delete [] nickname;} VintagePort& operator= (const VintagePort& vp); virtual void Show() const; friend std: stream& operator<< (std: stream& os, const VintagePort& vp);}; //實現(xiàn)文件 #include<iostream> #include<cstring> #include"Port.h" Port: ort(const char* br, const char* st, int b){ brand=new char[strlen(br)+1]; strcpy(brand, br); strncpy(style, st, 20); bottles=b; } Port: ort(const Port& p){ brand=new char[strlen(p.brand)+1]; strcpy(brand, p.brand); strncpy(style, p.style, 20); bottles=p.bottles; } Port& Port: perator = (const Port& p){ if (this==&p) return *this; delete [] brand; brand=new char[strlen(p.brand)+1]; strcpy(brand, p.brand); strncpy(style, p.style, 20); bottles=p.bottles; return *this; } Port& Port: perator += (int b){ bottles+=b; return *this; } Port& Port: perator -=(int b){ if (b<=bottles) bottles-=b; else std::cout<< "You can't take more bottles than we have!\n" << "Operation cancelled!\n"; return *this; } void Port::Show ()const { std::cout<< "Brand: "<< brand << "\nKind: "<< style << "\nBottles: "<< bottles <<std::endl; } std: stream& operator<< (std: stream& os, const Port& p){ os << p.brand << ", " << p.style << ", " << p.bottles; return os; } VintagePort::VintagePort() ort(){ nickname=new char[5]; strcpy(nickname, "none" ;year=0; } VintagePort::VintagePort(const char* br, int b, const char* nn, int y) ort(br, "vintage", b){ nickname=new char[strlen(nn)+1]; strcpy(nickname, nn) ; year=y; } VintagePort::VintagePort(const VintagePort& vp) ort(vp){ nickname=new char[strlen(vp.nickname)+1]; strcpy(nickname, vp.nickname); year=vp.year; } VintagePort& VintagePort: perator = (const VintagePort& vp){ Port: perator = (vp);delete [] nickname; nickname=new char[strlen(vp.nickname)+1]; strcpy(nickname, vp.nickname); year=vp.year; return *this; } void VintagePort::Show () const { Port::Show (); std::cout<< "Nickname: " << nickname << "\nYear: " << year << std::endl; } std: stream& operator<< (std: stream& os, const VintagePort& vp){ os << (const Port& vp; os << ", " << vp.nickname << ", " << vp.year; return os; } //主程序 #include<iostream> #include"Port.h" int main() { using std::cout; using std::cin; using std::endl; Port* pointer[10]; pointer[0]=&Port("Lafite", "tawny", 27); pointer[1]=&Port("Latour", "ruby", 23); pointer[2]=&VintagePort("Haut-Brion", 35, "The Noble", 1875); pointer[3]=&VintagePort("Margaux", 56, "Old Velvet", 1898); for (int k=0;k<4;k++) //這個不是主程序的部分,我是想用來測試,我用基類指針調(diào)用Show()函數(shù)的結(jié)果, //結(jié)果與我預(yù)想的不一樣,我的Show()函數(shù)是虛擬的,我用基類指針(Port*)指向派類對象 { pointer[k]->Show (); //用pointer[0]->Show()的時候,應(yīng)該是調(diào)用Port::Show(),這個調(diào)用是正確的,但是我的brand成員顯示卻出現(xiàn)了亂碼,這個不明白是怎么回事。 cout << endl; //用pointer[2]->Show()的時候,應(yīng)該是調(diào)用VintagePort::Show(),這個調(diào)用是錯的,結(jié)果調(diào)用的是Port::Show(),而且與上面一樣brand成員顯示卻出現(xiàn)了亂碼。 } Port& b=Port("Lafite", "tawny", 27); //我這里用基類引用指向基類對象,用引用調(diào)用的時候,就不出現(xiàn)亂碼,所以我覺得應(yīng)該問題出現(xiàn)在指針上 b.Show (); //但是,實在想不到是為什么? 求解答。 cout << b <<endl; Port& a=VintagePort("Margaux", 56, "Old Velvet", 1898); a.Show(); //還有這里,我用基類引用指向派生對象,在調(diào)用Show()的時候,就調(diào)用的是VintagePort::Show(),是正確的。 cout << a << endl; //但是這里又出現(xiàn)了問題,我重載的cout << a,應(yīng)該需要顯示nickname和year的,但是卻與cout << b是一樣的,是我的重載出現(xiàn)問題了么?? int choice,i=4; cout << "Do you want to take or save port(s)? (1 to take/2 to save/q to quit)"; while (cin >> choice) { if (choice==1) { int kind=0,num=0; cout << "Which brand do you need?(1:Lafite, 2:Latour, 3:Haut-Brion, 4:Margaux)"; cin >> kind; cin.get(); switch (kind) { case 1: { cout << "How many bottles do you need?\n"; cout << "There're " << pointer[0]->BottleCount() << " saved.\n"; cin >> num; *pointer[0]-=num; pointer[0]->Show(); break; } case 2: { cout << "How many bottles do you need?\n"; cout << "There're " << pointer[1]->BottleCount() << " saved.\n"; cin >> num; *pointer[1]-=num; pointer[1]->Show(); break; } case 3: { cout << "How many bottles do you need?"; cout << "There're " << pointer[2]->BottleCount() << " saved.\n"; cin >> num; (Port& *pointer[2]-=num; //或者直接用*pointer[2]-=num;因為-=的函參為Port&,可以指向VintagePort。pointer[2]->Show(); break; } case 4: { cout << "How many bottles do you need?"; cout << "There're " << pointer[3]->BottleCount() << " saved.\n"; cin >> num; *pointer[3]-=num; pointer[3]->Show(); break; } default: cout << "That's not a choice!\n"; } } else { int num=0; cout << "Please enter the brand:"; //這里我無法進(jìn)行輸入。 char temp[50]; cin.getline(temp, 50); cin.get(); if (temp=="Lafite" //我這里的字符串的比較應(yīng)該是有問題的,不能用==判斷。{ cout << "Save bottles: "; cin >> num; *pointer[0]+=num; pointer[0]->Show(); } else if (temp=="Latour" ![]() { cout << "Save bottles: "; cin >> num; *pointer[1]+=num; pointer[1]->Show(); } else if (temp=="Haut-Brion" ![]() { cout << "Save bottles: "; cin >> num; *pointer[2]+=num; pointer[2]->Show(); } else if (temp=="Margaux" ![]() { cout << "Save bottles: "; cin >> num; *pointer[3]+=num; pointer[3]->Show(); } else { cout << "Enter the style: "; char s_temp[10]; cin.getline(s_temp, 10); if (s_temp=="vintage" //這里的判斷也有問題。{ cout << "Enter the nickname: "; char n_temp[20]; cin.getline(n_temp, 20); cout << "Enter the year: "; int y_temp; cin >> y_temp; cout << "Enter the bottles: "; int b_temp; cin >> b_temp; pointer=&VintagePort(temp, b_temp, n_temp, y_temp); } else { cout << "Enter the bottles: "; int b_temp; cin >> b_temp; pointer=&Port(temp, s_temp, b_temp); } } } ++i; cout << "Do you want to take or save port(s)? (1 to take/2 to save/q to quit)"; } char ch; cin.get(); cout << "Do you want to see all the Port's information?(y/n) "; //最后還有這里,無法進(jìn)行輸入.... cin.get(ch); if (ch=='y'||ch=='Y') for (int c=0;c<i;++c) cout << *pointer[c] << endl; cout << "Bye!\n"; return 0; } [ Last edited by lhy317409772 on 2014-1-8 at 22:49 ] |

銀蟲 (初入文壇)

木蟲 (正式寫手)

銀蟲 (初入文壇)
|
非常感謝,問題已經(jīng)解決了絕大部分.... 這里還有一些疑問,希望能得到解決: 1.如何發(fā)代碼帖子,我的符號變成表情了,注意到您的代碼外面穿了馬甲:code,請問怎么做到的....我是新人,嘿嘿。 2.Port &&b = Port("Lafite", "tawny", 27); 這里好像有錯誤,報錯 引用無法賦值給引用..... 您說,只有常量引用和右值引用可以綁定在一個臨時變量上,我寫的是Port& b = Port("Lafite", "tawny", 27);我把左值引用綁定在了臨時變量上(請問,您說是不規(guī)范的,這里是為什么不規(guī)范?),const Port &b = Port("Lafite", "tawny", 27);這個是把常量引用綁定在臨時變量上,但是請問一下把右值引用綁定在臨時變量上是什么例子? 3. pointer[5] = new VintagePort(temp, b_temp, n_temp, y_temp); 這里為什么要new一個VintagePort? 還是因為只能對左值進(jìn)行取地址操作?如果僅僅是 Port("Lafite", "tawny", 27);這里創(chuàng)建的是臨時變量么?? 4. cout << "Do you want to see all the Port's information?(y/n) "; //最后還是無法輸入。 char infor; cin.get();//我在這里已經(jīng)用這個把cin >> choice;留下的\n給銷毀了,但是下面一個讀取還是不能工作。 cin.get(infor); if (infor=='y'||infor=='Y') for (int c=0;c<i;++c) cout << *pointer[c] << endl; else cout << "Bye!\n"; 十分感謝。 |

銀蟲 (初入文壇)

木蟲 (正式寫手)
|
1 高級回復(fù)可以插入代碼 2 右值引用需要c++11支持,編譯的時候帶上選項,vs2012和2013都是直接編譯就行了,要是沒有這么高版本的編譯器又不想用g++,那直接改成const Port&就行了;至于臨時變量的問題我只能說好好的找本c++的書看看; 3 我只能說那確實是個臨時變量,我代碼里面沒有寫釋放內(nèi)存的部分,因為我不知道你到地是想用5還是多少,需要自己把這部分代碼補上; 4 把quit的部分直接改成3或者其它數(shù)字,cin >> choice, 你輸入一個字母算什么呀; 5 把輸出的部分寫成虛函數(shù),或者用RTTI查看是不是派生類,是的話進(jìn)行轉(zhuǎn)換再輸出。 |

銀蟲 (初入文壇)
|
嗯....我自己用程序測試了一下,這個是個臨時變量,創(chuàng)建后馬上調(diào)用了析構(gòu)函數(shù)...... 呃,我這里用cin >> choice是想把cin的返回值當(dāng)作判斷條件,因為當(dāng)我輸入數(shù)字的時候cin返回1進(jìn)入循環(huán),輸入非數(shù)字的時候就讀取失敗cin返回0,退出循環(huán)。 我按著您的提示修改了程序,用break來跳出循環(huán)....程序確實是可以運行了,最后也可以進(jìn)行輸入了...但是我不明白用cin >> choice來進(jìn)行輸入,為什么會影響到后面一個字符的輸入,我覺得這里應(yīng)該是最后一個輸入讀取了前方的q或者\n,所以直接判斷為非y所以無法顯示所有信息..... 然后,只有成員函數(shù)才可以寫成虛函數(shù),如果我這里寫成虛函數(shù)了就只能有一個函參,這么寫:virtual std: stream& operator << (std: stream & os) {os << *this;(ps:原來的<<沒變,這里調(diào)用原來的友元) return os;},所以我在輸出的時候這么調(diào)用*pointer[c]<< cout << endl;看起來蠻別扭,但是結(jié)果確實是正確的....請問,這個方法能不能改進(jìn)使得用 cout << *opinter[c];來調(diào)用?? 還有您說的RTTI自己還沒有涉及到,但是我在想如果檢查出是派生對象,把基類*pointer[c]進(jìn)行轉(zhuǎn)換了輸出,這個豈不是向下轉(zhuǎn)換,理論上是不允許的,因為可能丟失數(shù)據(jù),難道這里的可以么?? 最后,我目前才學(xué)習(xí)C++不久,使用的教材是《C++ primer Plus(第五版)》,覺得還不錯,即將把這本書看完,這里希望您能給點指導(dǎo),想知道看完這本書后可以繼續(xù)看看別的什么比較好的教材來深入學(xué)習(xí).... 非常感謝您不厭其煩的指導(dǎo).... |

木蟲 (正式寫手)

銀蟲 (初入文壇)

| 9 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 298求B區(qū)調(diào)劑 +4 | zzz,,r 2026-04-02 | 5/250 |
|
|---|---|---|---|---|
|
[考研] 求調(diào)劑推薦 +3 | 南山南@ 2026-04-01 | 3/150 |
|
|
[考研] 求調(diào)劑!生物與醫(yī)藥專碩 +4 | 逆轉(zhuǎn)陸先生 2026-04-01 | 4/200 |
|
|
[考研] 266求調(diào)劑 +3 | 學(xué)員97LZgn 2026-04-02 | 3/150 |
|
|
[考研] 262求調(diào)劑 +4 | 勵志一定發(fā)文章 2026-04-02 | 5/250 |
|
|
[考研] 07生物學(xué)求調(diào)劑 一志愿同濟(jì)大學(xué)359分 +3 | LAMC. 2026-03-30 | 3/150 |
|
|
[考研] 070300化學(xué)279求調(diào)劑 +15 | 哈哈哈^_^ 2026-03-31 | 17/850 |
|
|
[考研] 求調(diào)劑0703 +5 | 周嘉堯 2026-03-31 | 8/400 |
|
|
[考研] 286求調(diào)劑 +5 | Sa67890. 2026-04-01 | 7/350 |
|
|
[考研] 0710生物學(xué)考研調(diào)劑 +3 | 李多米lee. 2026-03-27 | 4/200 |
|
|
[考研] 086502化學(xué)工程342求調(diào)劑 +7 | 阿姨復(fù)古不過 2026-03-27 | 7/350 |
|
|
[考研] 省雙一流重點一本大學(xué)招收調(diào)劑 +4 | wwwwffffff 2026-03-31 | 7/350 |
|
|
[考研] 08工科275求調(diào)劑,可跨考。 +5 | AaAa7420 2026-03-31 | 5/250 |
|
|
[考研] 08工科,295,接受跨專業(yè)調(diào)劑 +6 | lmnlzy 2026-03-31 | 6/300 |
|
|
[考研] 一志愿 南京航空航天大學(xué) ,080500材料科學(xué)與工程學(xué)碩 +10 | @taotao 2026-03-31 | 11/550 |
|
|
[考研] 求調(diào)劑,一志愿北林食品與營養(yǎng)095500,301分,已過六級,有科研經(jīng)歷 +4 | 快樂儲蓄罐 2026-03-31 | 4/200 |
|
|
[考研]
|
Gymno 2026-03-30 | 6/300 |
|
|
[考研] 085600 286分 材料求調(diào)劑 +11 | 麻辣魷魚 2026-03-27 | 12/600 |
|
|
[考研] 085600,專業(yè)課化工原理,321分求調(diào)劑 +5 | 大饞小子 2026-03-28 | 5/250 |
|
|
[考研] 藥學(xué)105500求調(diào)劑 +3 | Ssun。。 2026-03-28 | 3/150 |
|