向量类的重载 *, +, -'运算符<double>



我正在写一个Line类来制作数值方法,我想要这些运算符(*,+,-)使我的代码更易读,更容易理解。

        #include <vector>
        using namespace std;
        typedef vector<double> Vector;
        class Line : public Vector
        {
        public:
            Line();
            ~Line();
            Line operator+(Line);
            Line operator-(Line);
            Line operator*(double);
        };

        Line Line::operator*(double alfa)
        {
            Line temp;
            int n = size();
            temp.resize(n);
            for (int i = 0; i < n; i++)
            {
                temp.at(i) = this->at(i)*alfa;
            }
            return temp;
        }
        Line Line::operator+(Line line)
        {
            int n = size();
            Line temp;
            temp.resize(n);
            for (int i = 0; i < n; i++)
            {
                temp.at(i) = this->at(i) + line[i];
            }
            return temp;
        }
        Line Line::operator-(Line line)
        {
            int n = size();
            Line temp;
            temp.resize(n);
            for (int i = 0; i < n; i++)
            {
                temp.at(i) = this->at(i) - line[i];
            }
            return temp;
        }

        int main()
        {
            return 0;
        }

是否可以从Vector类重载这样的操作符?我应该只是做函数(或方法)而不是操作符吗?还有其他建议吗?

ps1:我使用Visual Studio 11作为编译器。

ps2:我没有将项目启动为"win32项目",它是控制台应用程序。

我得到以下错误:

Error   1   error LNK2019: unresolved external symbol "public: __thiscall Line::Line(void)" (??0Line@@QAE@XZ) referenced in function "public: class Line __thiscall Line::operator*(double)" (??DLine@@QAE?AV0@N@Z) C:UsersLucasDocumentsVisual Studio 11Projectstesttesttest.obj   test

Error   2   error LNK2019: unresolved external symbol "public: __thiscall Line::~Line(void)" (??1Line@@QAE@XZ) referenced in function "public: class Line __thiscall Line::operator*(double)" (??DLine@@QAE?AV0@N@Z)    C:UsersLucasDocumentsVisual Studio 11Projectstesttesttest.obj   test

必须在全局范围内重载操作符:

vector<double> operator*(const vector<double>& v, double alfa)
{
    ...
}
vector<double> operator+(const vector<double>& v1, const vector<double>& v2)
{
    ...
}
vector<double> operator-(const vector<double>& v1, const vector<double>& v2)
{
    ...
}

至于链接器错误,看起来就像您没有实现Line构造函数和析构函数。

你永远不应该继承std类,这些类不是用来继承的。从没有虚析构函数的类继承是非常危险的。

我建议您使用聚合:使您的Line类包含vector类型的成员,例如命名为myVector_,并以使用该成员变量的方式实现所需的操作符。

所以你把所有对size()的调用替换为myVector.size()等:

Line Line::operator*(double alfa)
{
    Vector temp;
    int n = myVector_.size();
    temp.resize(n);
    for (int i = 0; i < n; i++)
    {
        temp.at(i) = myVector_.at(i)*alfa;
    }
    return temp;
}

链接器错误告诉您代码缺少您声明的两个成员函数的定义—构造函数和析构函数:

Line::Line() {
    // Code of the constructor goes here
}
Line::~Line() {
    // Code of the destructor goes here
}

当然正确的事情是有一个Vector对象INSIDE行,而不是"继承"从Vector?通常从std::容器继承不是一个伟大的数据…我很确定"线"实际上不是一个向量,它是一个"有一个"向量。[当你继承时"的规则是"X是Y",当你创建一个复合对象时"X有一个Y" -所以X里面有一个Y]

你需要声明构造函数和析构函数来消除链接错误。

我也会使用const Line&作为数学运算的输入,因为您从不更改输入。

最新更新