0

以下代码是教程的一部分。我已经根据教程检查了无数次代码,虽然它适用于视频,但我的程序有以下错误:

1>------ Build started: Project: simpleclass, Configuration: Debug Win32 ------
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall rectangle::rectangle(void)" (??0rectangle@@QAE@XZ) referenced in function _main
1>C:\Users\Bob K\Documents\Visual Studio 2010\Projects\simpleclass\Debug\simpleclass.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是代码:

#include <iostream>

class rectangle
{
private:
    double length;
    double width;
public:
    rectangle();
    ~rectangle();
        double calcperim() const;
        double calcarea() const;
        double getlength() const;
        double getwidth() const;
        void setlength(double l);
        void setwidth(double w);

};

rectangle::~rectangle()
{
}

double rectangle::calcperim() const
{
    return ( length + length + width + width);
}

double rectangle::getlength() const
{
    return length;
}

double rectangle::getwidth()const
{
    return width;
}

void rectangle::setwidth( double w)
{   
    width = w;
}
void rectangle::setlength(double l)
{
    length = l;
}

double rectangle::calcarea() const
{
    return (length * width);
}



int main()
{
using namespace std;

    rectangle r;
    r.setwidth(3);
    r.setlength(9);

    cout << "length " << r.getlength() << endl;
    cout << "width " << r.getwidth() << endl;
    cout << "perimiter: " << r.calcperim() << endl;
    cout << "Area: " << r.calcarea() << endl;


    system("pause");
return 0;
}

请帮忙。几年前我曾经编程过,现在我正在尝试重新开始编程。

4

1 回答 1

6

您缺少rectangle构造函数的实现

rectangle::rectangle()
{
    length = 0;
    width = 0;
}

或者,如果您不关心初始化成员,只需从类定义中删除构造函数的定义。(请注意,这可能是不可取的;我只是为了完整性而将其包括在内。)

于 2013-08-21T19:24:08.250 回答