注册 登录
编程论坛 C++教室

求大虾帮忙,新手求教,实在不知道为啥错了

时间流走记忆 发布于 2012-11-14 13:03, 365 次点击
红色字体处是编译时的报错
#include <cstdlib>
#include <iostream>

using namespace std;

class Box
{
      public:
             Box(int h=10,int w=12,int len=15):height(h),width(w),length(len){}     //声明有默认参数的构造函数,用参数初始化表对成员初始化
             Box(int l=10,int s=10):lng(l),shrt(s){}
             int volume();
             int square();
      private:
              int height;
              int width;
              int length;
              int lng;
              int shrt;        
      };
int Box::volume()
{
    return(height*width*length);
    }
int Box::square()
{
    return(lng*shrt);
    }   
           
int main(int argc, char *argv[])
{
    Box a[3]=                                   //定义对象数组
    {
        Box(10,12,15),                          //调用构造函数Box,提供第一个元素的参数
        Box(15,18,20),                          //调用构造函数Box,提供第二个元素的参数
        Box(16,20,26)                           //调用构造函数Box,提供第三个元素的参数
        };

    cout<<"volume of a[0] is "<<a[0].volume()<<endl;         //调用a[0]的volume函数
    cout<<"volume of a[1] is "<<a[1].volume()<<endl;         //调用a[1]的volume函数
    cout<<"volume of a[2] is "<<a[2].volume()<<endl;         //调用a[2]的volume函数
   
   Box b[2]=
    {
        Box(11,11),     call of overloaded `Box(int, int)' is ambiguous   好像是引用模糊,不确定的意思
        Box(12,12)      call of overloaded `Box(int, int)' is ambiguous
       };
    cout<<"square of b[0] is "<<b[0].square()<<endl;
    cout<<"square of b[1] is "<<b[1].square()<<endl;   
    system("PAUSE");
    return EXIT_SUCCESS;
}
5 回复
#2
lz10919149992012-11-14 13:29
意思就是Box(11,11)的时候,编译器不知道应该调用哪一个函数,因为两个构造函数都匹配。
#3
newdos2012-11-14 13:45
这是重载陷阱,存在默认参数值的时候要特别小心。


#include <cstdlib>
#include <iostream>

using namespace std;

class Box
{
public:
    Box(int h, int w, int len = 15) : height(h),width(w),length(len) {}    //声明有默认参数的构造函数,用参数初始化表对成员初始化
    Box(int l = 13):height(l),width(l),length(l) {}
    int volume();
    bool isSquare(void);
private:
    int height;
    int width;
    int length;
};
int Box::volume()
{
    return(height*width*length);
}

bool Box::isSquare(void)
{
    return height == width && height == length;
}

int main(int argc, char *argv[])
{
    Box a[6]=                                   //定义对象数组
    {
        Box(10,12,15),                          //调用构造函数Box,提供第一个元素的参数
        Box(15,18,20),                          //调用构造函数Box,提供第二个元素的参数
        Box(16,20,26),                           //调用构造函数Box,提供第三个元素的参数
        Box(11),
        Box(12),
        Box()
    };

    for(int i = 0; i < 6; ++i)
    {
        if( a[i].isSquare() ) cout << "正方体 [" << i << "] - 体积:" << a[i].volume() << endl;
        else cout << "长方体 [" << i << "] - 体积:" << a[i].volume() << endl;
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}
#4
liufashuai2012-11-14 21:21
1楼正解,你写的默认参数构造函数让程序产生了歧义,编译器不知道该调用哪一个
#5
时间流走记忆2012-11-17 10:34
回复 2楼 lz1091914999
Thanks ,明白了。。。
#6
时间流走记忆2012-11-17 10:34
回复 3楼 newdos
威武!!了解了,多谢啊。
1