*[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