运算符 <<(操作数类型 std::ostream)不匹配 C++ OOP 和 Point

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22714089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:10:40  来源:igfitidea点击:

no match for operator << (operand types std::ostream) c++ OOP and Point

c++oopc++11

提问by Ahmad Adnan

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!

我正在尝试显示我通过成员函数创建的 Point 类的 p 对象。我已将 Point p 作为参数传递给我的程序的 void displayPoint(Point p) 成员函数。但是我的程序中出现以下编译错误!

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

D:\OOP 赋值 #01\point.cpp[Error] 不匹配 'operator<<'(操作数类型是 'std::ostream {aka std::basic_ostream}' 和 '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(){
}

采纳答案by taocp

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

You have not overloaded <<operator to output the object of class Pointdirectly. 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.

您没有重载<<运算符直接输出类的对象Point。所以你不能那样做。您可以添加重载operator<<或调用相应的 get 函数来获取Point.

For example, using get functions:

例如,使用 get 函数:

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

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

您可以查看有关重载的运算符重载 C++operator<<

回答by fredoverflow

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

std::cout << myPoint只是operator<<(std::cout, myPoint).

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

所以你必须operator<<根据你的需要为你的班级超载:

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

回答by 4pie0

You have not defined

你还没有定义

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

This function is needed whenever you want to put a Pointinto std::ostreamusing <<as in:

每当您想将 aPoint放入std::ostreamusing时都需要此函数<<

 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 herefor some examples of overload for a very similar class as yours Point.

您可以在此处查看与您的 Point 非常相似的类的一些重载示例。