为什么我需要运算符重载?

时间:2013-11-15 04:22:50

标签: c++ operator-overloading cout

请咨询。下面的代码,我只是试图cout存储在Line2D类中的x和y值。但我得到了这个错误。

Assn3.cpp:166:34: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>]((* & std::cout), ((const char*)"Pt1: ")) << l2d.Line2D::getPt1()’

我是否需要运营商重载?如果有,为什么? 我为其他类尝试了相同的操作,没有错误。

这是我的代码:

vector<Line2D> l2dvector;
Line2D l2d;

void readData()
{
    cout<< "Please enter filename : ";
    cin >> inputFile;

    fstream fileStream;
        fileStream.open(inputFile.c_str(),fstream::in);
        int records = 0;

    while( fileStream.good() ) 
    {
        string line = "";
        while (getline (fileStream, line))  
        {   
            stringstream ss (line);
            getline (ss, className, ',');

                 if (className == "Line2D")
            {   
                int x1, x2, y1, y2;
                getline (ss, l2dX1, '[');
                getline (ss, l2dX1, ',');

                getline (ss, l2dY1, ' ');
                getline (ss, l2dY1, ']');

                getline (ss, l2dX2, ' ');
                getline (ss, l2dX2, '[');
                getline (ss, l2dX2, ',');

                getline (ss, l2dY2, ' ');
                getline (ss, l2dY2, ']');

                istringstream (l2dX1) >> x1;
                istringstream (l2dX2) >> x2;
                istringstream (l2dY1) >> y1;
                istringstream (l2dY2) >> y2;

                Point2D pt1 (x1,y1);
                Point2D pt2 (x2,y2);

                Line2D l2d (pt1, pt2);
                l2dvector.push_back(l2d);
                l2d.setPt1(pt1);
                l2d.setPt2(pt2);

            //cout << "class name: " << className << endl;          
            cout << "Pt1: " << l2d.getPt1() << endl;
            cout << "Pt2: " << l2d.getPt2() << endl;
            }
                }

    records++;
    }

    cout << records << "records read in successfully!" << endl;
    cout << "Going back to main menu .. " << endl;

}




Line2D.cpp

Line2D::Line2D ()
{

}

Line2D::Line2D (Point2D pt1, Point2D pt2)
{
    this->pt1 = pt1;
    this->pt2 = pt2;
}


double Line2D::setLength()
{
    length = sqrt(pow((pt1.getX()-pt2.getX()),2)) + pow ((pt1.getY()-pt2.getY()),2);
    return length;
}


//Accessors

Point2D Line2D::getPt1()
{
    return pt1;
}


Point2D Line2D::getPt2()
{
    return pt2;
}

double Line2D::getScalarValue() //returns the value of attribute length
{
    return length;
}


//Mutators

void Line2D::setPt1 (Point2D pt1)
{
    this->pt1 = pt1;
}

void Line2D::setPt2 (Point2D pt2)
{
    this->pt2 = pt2;
}

编辑:

在Point2D.h中添加了运算符重载

ostream &operator<<( ostream &output, const Point2D &P)
              { 
             output << "X : " << P.x << " Y : " << P.y;
             return output;            
              }

编译错误:

In file included from Point2D.cpp:3:0:
Point2D.h:39:60: error: ‘std::ostream& Point2D::operator<<(std::ostream&, const Point2D&)’ must take exactly one argument
In file included from Point3D.h:9:0,
                 from Point3D.cpp:3:
Point2D.h:39:60: error: ‘std::ostream& Point2D::operator<<(std::ostream&, const Point2D&)’ must take exactly one argument
In file included from Line2D.h:9:0,
                 from Line2D.cpp:3:
Point2D.h:39:60: error: ‘std::ostream& Point2D::operator<<(std::ostream&, const Point2D&)’ must take exactly one argument

3 个答案:

答案 0 :(得分:6)

每当对象分别输出到流或从流输入时,就调用流操作符operator<<operator>>。因此,如果某个类没有定义其中一个,那么归属于它的功能将无法访问。

基本上,长话短说,您的Point2D课程需要operator<<。如果您想将其与cin一起使用,则还需要operator>>

Here是一个关于重载流操作符的教程。

答案 1 :(得分:1)

你的getPt1()和getPt2()返回一个Point2D。您需要指示编译器在输出Point2D时输出的内容。

因此,您需要重载运算符&lt;&lt;这样当你说“输出Point2D的值”时,编译器就会理解你的意思。

答案 2 :(得分:0)

我们需要提供&gt;&gt;的运算符重载和&lt;&lt;如果要打印并输入用户定义的对象,则为运算符。

g ++库已超载&lt;&lt;和&gt;&gt;基本内置数据类型的运算符。 g ++编译器不提供默认值&lt;&lt;和&gt;&gt;运算符函数与=运算符重载函数不同。

这里你的对象Point2D不为g ++编译器所知,或者签名不匹配,因此它给出了这个错误。