[問題] C++教學手冊的習題

作者: lieslider   2018-03-18 08:33:43
*[36m開發平台(Platform): (Ex: Win10, Linux, ...) *[m
Win7
*[36m編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)*[m
TDM-GCC 4.9.2 64-bit release
*[36m錯誤結果(Wrong Output):*[m
D:\習題14.cpp In function 'int main()':
46 26 D:\習題14.cpp [Error] no matching function for call to
'CWin::set_data(char, int, int)'
46 26 D:\習題14.cpp [Note] candidates are:
22 8 D:\習題14.cpp [Note] void CWin::set_data(char&, int&, int&)
22 8 D:\習題14.cpp [Note] no known conversion for argument 1 from 'char' to
'char&'
29 8 D:\習題14.cpp [Note] void CWin::set_data(char&)
29 8 D:\習題14.cpp [Note] candidate expects 1 argument, 3 provided
34 8 D:\習題14.cpp [Note] void CWin::set_data(int&, int&)
34 8 D:\習題14.cpp [Note] candidate expects 2 arguments, 3 provided
47 20 D:\習題14.cpp [Error] no matching function for call to
'CWin::set_data(char)'
47 20 D:\習題14.cpp [Note] candidates are:
22 8 D:\習題14.cpp [Note] void CWin::set_data(char&, int&, int&)
22 8 D:\習題14.cpp [Note] candidate expects 3 arguments, 1 provided
29 8 D:\習題14.cpp [Note] void CWin::set_data(char&)
29 8 D:\習題14.cpp [Note] no known conversion for argument 1 from 'char' to
'char&'
34 8 D:\習題14.cpp [Note] void CWin::set_data(int&, int&)
34 8 D:\習題14.cpp [Note] candidate expects 2 arguments, 1 provided
48 23 D:\習題14.cpp [Error] no matching function for call to
'CWin::set_data(int, int)'
48 23 D:\習題14.cpp [Note] candidates are:
22 8 D:\習題14.cpp [Note] void CWin::set_data(char&, int&, int&)
22 8 D:\習題14.cpp [Note] candidate expects 3 arguments, 2 provided
29 8 D:\習題14.cpp [Note] void CWin::set_data(char&)
29 8 D:\習題14.cpp [Note] candidate expects 1 argument, 2 provided
34 8 D:\習題14.cpp [Note] void CWin::set_data(int&, int&)
34 8 D:\習題14.cpp [Note] no known conversion for argument 1 from 'int' to
'int&'
*[36m問題(Question):*[m
小弟自學C++,程度不太好,想請教書本裡第13章的習題14
題目內容是要將下面prog12_11的程式的pulic變數改為private,並將set_data()設定成
友誼函數,同時也可以多載。 (提示:將CWin物件以參照的型式傳入set_data()
函數裡)
*[36m程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) *[m
// prog12_11, 函數的多載
#include <iostream>
#include <cstdlib>
using namespace std;
class CWin // 定義視窗類別CWin
{
public:
char id;
int width;
int height;
int area() // 定義成員函數area(), 用來計算面積
{
return width*height;
}
void show_area(void)
{
cout << "Window " << id << ", area=" << area() << endl;
}
void set_data(char i,int w,int h) // 第一個set_data()函數
{
id=i;
width=w;
height=h;
}
void set_data(char i) // 第二個set_data()函數
{
id=i;
}
void set_data(int w,int h) // 第三個set_data()函數
{
width=w;
height=h;
}
};
int main(void)
{
CWin win1,win2;
win1.set_data('A',50,40);
win2.set_data('B');
win2.set_data(80,120);
win1.show_area();
win2.show_area();
system("pause");
return 0;
}
我的想法:
#include <iostream>
#include <cstdlib>
using namespace std;
class CWin
{
private:
char id;
int width, height;
public:
int area()
{
return width*height;
}
void show_area()
{
cout << "window " << id << ", area= " << area() <<
endl;
}
void set_data(char &i , int &w, int &h)
{
id = i;
width = w;
height = h;
}
void set_data(char &i)
{
id = i;
}
void set_data(int &w, int &h)
{
width = w;
height = h;
}
};
int main(void)
{
CWin winA, winB;
winA.set_data ('A',50,40);
winB.set_data ('B');
winB.set_data (80,120);
winA.show_area();
winB.show_area();
system("pause");
return 0;
}
我的想法執行後會出現錯誤,請問要如何修改才對
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.238.115.85
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1521333225.A.FEF.html
作者: jerryh001   2018-03-18 09:05:00
所以我說那個發問格式剛剛發現 為什麼80要加引號其實我問發文格式主要是要你補上 "出現什麼錯誤"
作者: lylu (理路)   2018-03-18 15:22:00
你的function要求傳入的都是reference
作者: kiedveian (極地之星光)   2018-03-19 00:13:00
解法1加const https://ideone.com/qRobI3解法2拿個變數去接 https://ideone.com/R68Iim

Links booklink

Contact Us: admin [ a t ] ucptt.com