学习Essential C++例题时遇到的问题
时间:2010-09-10
来源:互联网
本帖最后由 showboy 于 2010-09-10 05:18 编辑
Essential C++这本书估计大家都知道吧,不知道的可以看这里
http://book.douban.com/subject/1215826/
在练习题2.5和2.6中遇到一问题,不求甚解,请指教,谢谢
编译环境是Code::Block 8.02
其自带的GCC为 gcc-g++-3.4.5-20060117-1-vista.tar.gz
以下是2.5中的源码,可以正确编译,问题在后面的2.6中
练习2.5的源码
复制代码
练习2.6的源码
复制代码
编译练习2.6的错误信息如下
复制代码
用DEV C++也是差不多的错误,
怀疑其中的问题在于练习2.5中的21行的函数中存在传址(by reference)的问题,而int和float函数没用使用反应用,不知道这样理解对不对?
inline string max(const string &t1,const string &t2){
return t1>t2? t1:t2;
}
这里的&在练习2.6中会出现共用template max()函数出现问题
初学c++不知道如何解决,请指教
另外用MS VC++6.0可以编译通过,不过有不少警告,具体信息如下
复制代码
Essential C++这本书估计大家都知道吧,不知道的可以看这里
http://book.douban.com/subject/1215826/
在练习题2.5和2.6中遇到一问题,不求甚解,请指教,谢谢
编译环境是Code::Block 8.02
其自带的GCC为 gcc-g++-3.4.5-20060117-1-vista.tar.gz
以下是2.5中的源码,可以正确编译,问题在后面的2.6中
练习2.5的源码
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
-
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
- using std::string;
- using std::max_element;
-
- inline int max(int t1,int t2){
- return t1>t2? t1:t2;
- }
-
- inline float max(float t1,float t2){
- return t1>t2? t1:t2;
- }
-
- inline string max(const string &t1,const string &t2){
- return t1>t2? t1:t2;
- }
- //////////////////////////////////////////////////////////////
- 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(const int *parray,int size){
- return *max_element(parray,parray+size);
- }
-
- inline float max(const float *parray,int size){
- return *max_element(parray,parray+size);
- }
-
- inline string max(const string *parray,int size){
- return *max_element(parray,parray+size);
- }
-
- int main()
- {
- string sarray[]={"we","were","her","pride","of","ten"};
- vector<string> svec(sarray,sarray+6);
-
- int iarray[]={12,70,2,169,1,5,29};
- vector<int>ivec(iarray,iarray+7);
-
- float farray[]={2.5,24.8,18.7,4.1,23.9};
- vector<float>fvec(farray,farray+5);
-
- int imax=max(max(ivec),max(iarray,7));
- float fmax=max(max(fvec),max(farray,5));
- string smax=max(max(svec),max(sarray,6));
-
- cout<<"imax should be 169 --found:"<<imax<<endl;
- cout<<"imax should be 24.8 --found:"<<fmax<<endl;
- cout<<"imax should be were --found:"<<smax<<endl<<endl;
-
- cout<<max(ivec)<<","<<max(iarray,7)<<endl;
- cout<<max(fvec)<<","<<max(farray,5)<<endl;
- cout<<max(svec)<<","<<max(sarray,6)<<endl;
-
- return true;
- }
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
-
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
- using std::string;
- using std::max_element;
-
- template <typename Type> inline Type max(Type t1,Type t2){
- return t1>t2? t1:t2;
- }
-
- template <typename elemType> inline elemType max(const vector<elemType> &vec){
- return *max_element(vec.begin(),vec.end());
- }
-
- template <typename arrayType> inline arrayType max(const arrayType *parray,int size){
- return *max_element(parray,parray+size);
- }
-
- int main()
- {
- string sarray[]={"we","were","her","pride","of","ten"};
- vector<string> svec(sarray,sarray+6);
-
- int iarray[]={12,70,2,169,1,5,29};
- vector<int>ivec(iarray,iarray+7);
-
- float farray[]={2.5,24.8,18.7,4.1,23.9};
- vector<float>fvec(farray,farray+5);
-
- int imax=max(max(ivec),max(iarray,7));
- float fmax=max(max(fvec),max(farray,5));
- string smax=max(max(svec),max(sarray,6));
-
- cout<<"imax should be 169 --found:"<<imax<<endl;
- cout<<"imax should be 24.8 --found:"<<fmax<<endl;
- cout<<"imax should be were --found:"<<smax<<endl<<endl;
-
- cout<<max(ivec)<<","<<max(iarray,7)<<endl;
- cout<<max(fvec)<<","<<max(farray,5)<<endl;
- cout<<max(svec)<<","<<max(sarray,6)<<endl;
-
- return true;
- }
- D:\MY_CPLUSPLUS\essential_C++\2.6.cpp||In function `int main()':|
- D:\MY_CPLUSPLUS\essential_C++\2.6.cpp|38|error: call of overloaded `max(std::string, std::string)' is ambiguous|
- D:\MY_CPLUSPLUS\essential_C++\2.6.cpp|13|note: candidates are: Type max(Type, Type) [with Type = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]|
- D:\Program Files\CodeBlocks\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 = std::string]|
- ||=== Build finished: 1 errors, 0 warnings ===|
怀疑其中的问题在于练习2.5中的21行的函数中存在传址(by reference)的问题,而int和float函数没用使用反应用,不知道这样理解对不对?
inline string max(const string &t1,const string &t2){
return t1>t2? t1:t2;
}
这里的&在练习2.6中会出现共用template max()函数出现问题
初学c++不知道如何解决,请指教
另外用MS VC++6.0可以编译通过,不过有不少警告,具体信息如下
- d:\my_cplusplus\essential_c++\2.6.cpp(33) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
- d:\my_cplusplus\essential_c++\2.6.cpp(33) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
- d:\my_cplusplus\essential_c++\2.6.cpp(33) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
- d:\my_cplusplus\essential_c++\2.6.cpp(33) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
- d:\my_cplusplus\essential_c++\2.6.cpp(49) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<cha
- r,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
- d:\my_cplusplus\essential_c++\2.6.cpp(49) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std:
- :char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
- d:\program files\vc6\vc98\include\vector(56) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::vector<std::basi
- c_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
- d:\program files\vc6\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::~vector<std::bas
- ic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
-
- 2.6.obj - 0 error(s), 0 warning(s)
作者: showboy 发布时间: 2010-09-10
本帖最后由 showboy 于 2010-09-10 05:18 编辑
上面是自己实际运行的程序,下面贴一下原书的源码,直接编译存在问题
上面是自己实际运行的程序,下面贴一下原书的源码,直接编译存在问题

作者: showboy 发布时间: 2010-09-10
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28