no match for operator << (operand types std:

2019-03-31 11:50发布

问题:

This question already has an answer here:

  • no match for ‘operator<<’ in ‘std::operator 4 answers

I am trying to display p object of Point class that I have created through member function . I have passed Point p as argument in void displayPoint(Point p) member function of my program. But I am getting the following compilation error in my program!

D:\OOP Assignment # 01\point.cpp[Error] no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'Point')

Here below is my code !!!

#ifndef POINT_H
#define POINT_H

using namespace std;     // For string usage

class Point
{
public:
    Point();                                    // Default Constructor
    Point(double, double, int);                // Three argument constructor
    void initialize(double, double, int);    
    void shift(Point p, int keyPress);                      // Shift the first point 

    void setValue(int value);
    int getValue() const;
    void setX();
    double getX() const;
    void setY();
    double gety() const;

    void AddPointValue(Point p2);             /*This function add the TWO points  
    successfully reach on second true point co-ordinates*/

    void displayPoint(Point p);                     //This will use to display value
    bool checkCoordinates();
     bool checkTime();                        // Check time remaining
private:
     double x;
    double y;
    int value;

};

#endif

IMPLEMENTATION FILE

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <string>
#include <time.h>
#include <stdio.h>
#include "point.h"

using namespace std;

Point::Point()    // Default Constructor
{
    x = 0.0;
    y = 0.0;
    value = 0;
}
Point::Point(double x1, double y1, int value1){
    x = x1;
    y = y1;
    value = value1;

}

void Point::initialize(double init_x, double init_y, int init_value)
{
    x = init_x;
    y = init_y;
    value = init_value;
}

void Point::shift(Point p, int keyPress){
    Point maxSize;
    Point minSize;
    maxSize.x=80;
    maxSize.y=40;
    switch(keyPress)
    {
        case (VK_LEFT):     // increment the x coord 
            p.x += 1;    
            if(p.x < minSize.x) p.x = minSize.x;
            break;
        case (VK_RIGHT):   // decrement the x coord
            p.x -= 1;
            if(p.x > maxSize.x) p.x = maxSize.x;
            break;
        case (VK_UP):    // decrement the y coord
            p.y -= 1;
            if(p.y < minSize.y) p.y = minSize.y;
            break;
        case (VK_DOWN):    // increment the y coord
            p.y += 1;
            if(p.y > maxSize.y) p.y = maxSize.y;
            break; 
}

void Point::setValue(int value){
    value = 0;
}

int Point::getValue() const{
    return value;
}


void Point::setX(){
     x = 0.0;
}

double Point::getX() const{
    return x;
}

void Point::setY(){
    y = 0.0;
}
double Point::gety() const{
    return y;
}

void Point::displayPoint(Point p){
    cout << p;      // ERROR OCCURING HERE!!!
}

void Point::AddPointValue(Point p2){
}

bool Point::checkTime(){
}

回答1:

 void Point::displayPoint(Point p){
       cout << p;      // ERROR OCCURING HERE!!!
 }

You have not overloaded << operator to output the object of class Point directly. So you can't do that. You can either add an overloaded operator<< or call corresponding get functions to get the data members of Point.

For example, using get functions:

void Point::displayPoint(Point p){
     cout << p.getX() << " " << p.gety() << endl; 
 }

You can take a look at Operator Overloading C++ about overloading operator<<.



回答2:

std::cout << myPoint is just syntactic sugar for operator<<(std::cout, myPoint).

So you have to overload operator<< for your class according to your needs:

std::ostream& operator<<(std::ostream& os, const Point& p)
{
    os << p.getX() << "/" << p.getY();
    return os;
}


回答3:

You have not defined

std::ostream& operator<<( std::ostream&, const Point&);

This function is needed whenever you want to put a Point into std::ostream using << as in:

 void Point::displayPoint(Point p){
       cout << p;      // operator<< must be overloaded to make this work
 }

Possible implementation of this method for your purpose is:

std::ostream& operator<<( std::ostream& s, const Point& p)
{
    s << p.getX() << ", " << p.getY();
    return s;
}

You can look here for some examples of overload for a very similar class as yours Point.



标签: c++ oop c++11