勿语星空丶 2014-10-15 13:09 采纳率: 66.7%
浏览 1772
已采纳

关于C++ 类 编译出错问题

大家好,最近在努力学习C++,但是遇见了一些问题,很希望懂的人士帮我解答下,这对于你们很简单吧,嘿嘿,谢谢了
代码如下:

#include<iostream>
using namespace std;
class Box{
    public:
        double length;
        double heigth;
        double width;
        int d;
        Box();
    Box(double length1,double heigth1,double width1){
        length=length1;
        heigth=heigth1;
        width=width1;
    }
    double volume(){
        return length*heigth*width;
    }
};//要有分号哦! 
int main(){
    Box firstBox(2.0,4.0,6.0);
    firstBox.d=2;
    cout<<endl
        <<firstBox.length<<"\n"
        <<firstBox.heigth<<"\n"
        <<firstBox.width<<"\n"
        <<firstBox.d
        <<endl;
    cout<<firstBox.volume();

    Box smallBox;
    smallBox.length=10.0;
    smallBox.heigth=5.0;
    smallBox.width=4.0;
    cout<<endl;
    cout<<smallBox.length<<"\n"
        <<smallBox.heigth<<"\n"
        <<smallBox.width<<"\n"
        <<smallBox.volume()
        <<endl;
    return 0;
}

在Dev_C++5.6.1上编译的,出现了如下:

C:\Users\ADMINI~1\AppData\Local\Temp\ccgxkOkf.o lei1.cpp:(.text+0x10f): undefined reference to `Box::Box()'
D:\Dev-Cpp\大二C\collect2.exe [Error] ld returned 1 exit status

我在百度上搜了很多,但很不理解,希望各位帮忙解答下。谢谢了

  • 写回答

2条回答 默认 最新

  • nissanquest 2014-10-15 18:41
    关注

    因为你编写的Box smallBox;需要使用到无参构造函数,但是你已经提供了一个带参数的构造函数,因此编译器将不会为你提供无参构造函数,即默认构造函数,你只需在类定义加上Box(){},即显式定义一个无参构造函数就可以了。
    另外你原来代码中有个Box(),这个只是声明,没有定义,但是你Box smallBox;这个需要使用这个函数,但连接器找不到这个函数的入口,因此也会报错的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程