《Essent C++》习题 2.5 2.6

做了《Essent C++》的一个简单习题练习,结果却出现了莫名其妙的编译错误。。

先贴题目:

练习2.5
实现一个重载的max()函数,让它接受以下参数:(a) 两个整数 (b)两个浮点数 (c)两个字符串 (d)一个整数vector (e)一个浮点数vector (f)一个字符串vector (g)一个整数数组,以及一个表示数组大小的整数值 (h)一个浮点数数组,以及一个表示数组大小的整数值 (i)一个字符串数组,以及一个表示数组大小的整数值。最后,编写main()测试这些函数。

练习2.6
以template 重新完成练习2.5,并对main()做适度的修改。

很容易完成重载函数,求元素最大值可以用泛型算法max_element,直接写好代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

inline int max(int v1,int v2)
{ return v1>v2?v1:v2; }
inline double max(double v1,double v2)
{ return v1>v2?v1:v2; }
inline string max(const string &v1,const string &v2)
{ return v1>v2?v1:v2; }
inline int max(const vector<int> &vec)
{ return *max_element(vec.begin(),vec.end()); }
inline float max(const vector<float> &vec)
{ return *max_element(vec.begin(),vec.end()); }
inline string max(const vector<string> &vec)
{ return *max_element(vec.begin(),vec.end()); }
inline int max(int v[],int len)
{ return *max_element(v,v+len); }
inline double max(double v[],int len)
{ return *max_element(v,v+len); }
inline string max(string v[],int len)
{ return *max_element(v,v+len); }

int main(){

cout<<max(1,3)<<endl
<<max(1.1,3.1)<<endl
<<max("2333","233")<<endl;

const int len = 3;
int a[len] = {1,5,6};
float b[len] = {1.1,5.2,6.0};
string c[len] = {"322","223","233"};

cout<<max(a,len)<<endl
<<max(b,len)<<endl
<<max(c,len)<<endl;

vector<int> va(a,a+len);
vector<float> vb(b,b+len);
vector<string> vc(c,c+len);

cout<<max(va)<<endl
<<max(vb)<<endl
<<max(vc)<<endl;

return 0;
}

运行测试,输出结果为:

1
2
3
4
5
6
7
8
9
3
3.1
233
6
6
322
6
6
322

OK,继续练习2.6,以template重写以上代码,简单分析可以看出,9个函数可以简单的归纳为为种:给定两值求最大值、vector中的元素求最大值、array中的元素求最大值,对代码稍作改动,将9个max函数重写为3个重载template函数,运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

template <typename type>
inline type max(type v1,type v2)
{ return v1>v2?v1:v2; }

template <typename type>
inline type max(const vector<type> &vec)
{ return *max_element(vec.begin(),vec.end()); }

template <typename type>
inline type max(type v[],int len)
{ return *max_element(v,v+len); }


int main(){

cout<<max(1,3)<<endl
<<max(1.1,3.1)<<endl
<<max("2333","233")<<endl;

const int len = 3;
int arra[len] = {1,5,6};
float arrb[len] = {1.1,5.2,6.0};
string arrc[len] = {"322","223","233"};
cout<<max(arra,len)<<endl
<<max(arrb,len)<<endl
<<max(arrc,len)<<endl;

vector<int> veca(arra,arra+len);
vector<float> vecb(arrb,arrb+len);
vector<string> vecc(arrc,arrc+len);
cout<<max(veca)<<endl
<<max(vecb)<<endl
<<max(vecc)<<endl;

return 0;
}

编译运行不料,却显示报错信息如下:

D:\code\cpp\essential c++\2.6.cpp||In function int main()’:| D:\code\cpp\essential c++\2.6.cpp|32|error: call of overloadedmax(int, int)’ is ambiguous| D:\code\cpp\essential
c++\2.6.cpp|12|note: candidates are: type max(type, type) [with type =
int]|
D:\MinGW\bin..\lib\gcc\mingw32\3.4.5……..\include\c++\3.4.5\bits\stl_algobase.h|173|note:
const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]|
D:\code\cpp\essential c++\2.6.cpp|33|error: call of overloaded
`max(double, double)’ is ambiguous| D:\code\cpp\essential
c++\2.6.cpp|12|note: candidates are: type max(type, type) [with type =
double]|
D:\MinGW\bin..\lib\gcc\mingw32\3.4.5……..\include\c++\3.4.5\bits\stl_algobase.h|173|note:
const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = double]| ||===
Build Failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

注:这里我使用的CodeBlocks + MinGW,未测试其他编译环境

冥思苦想不得,发到群里求助,有人说了一句“using namespace std?”,于是顿时想起可能是与标准库冲突了,删去为偷懒而写的using namespace std的代码,编译运行正常。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using std::max_element;
using std::vector;
using std::string;

template <typename type>
inline type max(type v1,type v2)
{ return v1>v2?v1:v2; }

template <typename type>
inline type max(const vector<type> &vec)
{ return *max_element(vec.begin(),vec.end()); }

template <typename type>
inline type max(type v[],int len)
{ return *max_element(v,v+len); }


int main(){
using std::cout;
using std::cin;
using std::endl;

cout<<max(1,3)<<endl
<<max(1.1,3.1)<<endl
<<max("2333","233")<<endl;

const int len = 3;
int arra[len] = {1,5,6};
float arrb[len] = {1.1,5.2,6.0};
string arrc[len] = {"322","223","233"};
cout<<max(arra,len)<<endl
<<max(arrb,len)<<endl
<<max(arrc,len)<<endl;

vector<int> veca(arra,arra+len);
vector<float> vecb(arrb,arrb+len);
vector<string> vecc(arrc,arrc+len);
cout<<max(veca)<<endl
<<max(vecb)<<endl
<<max(vecc)<<endl;

return 0;
}

后记

后来发现其实编译的报错信息是有指出具体代码的,

D:\MinGW\bin..\lib\gcc\mingw32\3.4.5……..\include\c++\3.4.5\bits\stl_algobase.h|173|note: const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]|

就是这一行,当时没仔细看没有想到是命名冲突的问题。。。

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

请我喝杯咖啡吧~

支付宝
微信