+ -
当前位置:首页 → 问答吧 → 学习Essential C++例题时遇到的问题

学习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的源码
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>

  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. using std::string;
  10. using std::max_element;

  11. inline int max(int t1,int t2){
  12.     return t1>t2? t1:t2;
  13. }

  14. inline float max(float t1,float t2){
  15.     return t1>t2? t1:t2;
  16. }

  17. inline string max(const string &t1,const string &t2){
  18.     return t1>t2? t1:t2;
  19. }
  20. //////////////////////////////////////////////////////////////
  21. inline int max(const vector<int> &vec){
  22.     return *max_element(vec.begin(),vec.end());
  23. }

  24. inline float max(const vector<float> &vec){
  25.     return *max_element(vec.begin(),vec.end());
  26. }

  27. inline string max(const vector<string> &vec){
  28.     return *max_element(vec.begin(),vec.end());
  29. }
  30. ////////////////////////////////////////////////////////////
  31. inline int max(const int *parray,int size){
  32.     return *max_element(parray,parray+size);
  33. }

  34. inline float max(const float *parray,int size){
  35.     return *max_element(parray,parray+size);
  36. }

  37. inline string max(const string *parray,int size){
  38.     return *max_element(parray,parray+size);
  39. }

  40. int main()
  41. {
  42.     string sarray[]={"we","were","her","pride","of","ten"};
  43.     vector<string> svec(sarray,sarray+6);

  44.     int iarray[]={12,70,2,169,1,5,29};
  45.     vector<int>ivec(iarray,iarray+7);

  46.     float farray[]={2.5,24.8,18.7,4.1,23.9};
  47.     vector<float>fvec(farray,farray+5);

  48.     int imax=max(max(ivec),max(iarray,7));
  49.     float fmax=max(max(fvec),max(farray,5));
  50.     string smax=max(max(svec),max(sarray,6));

  51.     cout<<"imax should be 169 --found:"<<imax<<endl;
  52.     cout<<"imax should be 24.8 --found:"<<fmax<<endl;
  53.     cout<<"imax should be were --found:"<<smax<<endl<<endl;

  54.     cout<<max(ivec)<<","<<max(iarray,7)<<endl;
  55.     cout<<max(fvec)<<","<<max(farray,5)<<endl;
  56.     cout<<max(svec)<<","<<max(sarray,6)<<endl;

  57.     return true;
  58. }
复制代码
练习2.6的源码
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>

  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. using std::string;
  10. using std::max_element;

  11. template <typename Type> inline Type max(Type t1,Type t2){
  12.     return t1>t2? t1:t2;
  13. }

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

  17. template <typename arrayType> inline arrayType max(const arrayType *parray,int size){
  18.     return *max_element(parray,parray+size);
  19. }

  20. int main()
  21. {
  22.     string sarray[]={"we","were","her","pride","of","ten"};
  23.     vector<string> svec(sarray,sarray+6);

  24.     int iarray[]={12,70,2,169,1,5,29};
  25.     vector<int>ivec(iarray,iarray+7);

  26.     float farray[]={2.5,24.8,18.7,4.1,23.9};
  27.     vector<float>fvec(farray,farray+5);

  28.     int imax=max(max(ivec),max(iarray,7));
  29.     float fmax=max(max(fvec),max(farray,5));
  30.     string smax=max(max(svec),max(sarray,6));

  31.     cout<<"imax should be 169 --found:"<<imax<<endl;
  32.     cout<<"imax should be 24.8 --found:"<<fmax<<endl;
  33.     cout<<"imax should be were --found:"<<smax<<endl<<endl;

  34.     cout<<max(ivec)<<","<<max(iarray,7)<<endl;
  35.     cout<<max(fvec)<<","<<max(farray,5)<<endl;
  36.     cout<<max(svec)<<","<<max(sarray,6)<<endl;

  37.     return true;
  38. }
复制代码
编译练习2.6的错误信息如下
  1. D:\MY_CPLUSPLUS\essential_C++\2.6.cpp||In function `int main()':|
  2. D:\MY_CPLUSPLUS\essential_C++\2.6.cpp|38|error: call of overloaded `max(std::string, std::string)' is ambiguous|
  3. 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> >]|
  4. 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]|
  5. ||=== Build finished: 1 errors, 0 warnings ===|
复制代码
用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可以编译通过,不过有不少警告,具体信息如下
  1. d:\my_cplusplus\essential_c++\2.6.cpp(33) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
  2. d:\my_cplusplus\essential_c++\2.6.cpp(33) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
  3. d:\my_cplusplus\essential_c++\2.6.cpp(33) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
  4. d:\my_cplusplus\essential_c++\2.6.cpp(33) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
  5. 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
  6. 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
  7. 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:
  8. :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
  9. 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
  10. 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
  11. 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
  12. 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

  13. 2.6.obj - 0 error(s), 0 warning(s)
复制代码

作者: showboy   发布时间: 2010-09-10

本帖最后由 showboy 于 2010-09-10 05:18 编辑

上面是自己实际运行的程序,下面贴一下原书的源码,直接编译存在问题

下载 (163.79 KB)
essential26
2010-09-10 03:48

作者: showboy   发布时间: 2010-09-10

相关阅读 更多