不匹配运营商<< (操作数类型std :: ostream)c ++ OOP和Point

时间:2014-03-28 13:22:09

标签: c++ oop c++11

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

D:\ OOP分配#01 \ point.cpp [错误]不匹配'运营商<<<' (操作数类型是' std :: ostream {aka std :: basic_ostream}'' Point')

以下是我的代码!!!

#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

实施文件

#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(){
}

3 个答案:

答案 0 :(得分:3)

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

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

例如,使用get函数:

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

您可以查看有关超载operator<<的{​​{3}}。

答案 1 :(得分:2)

您尚未定义

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

如果您想使用Pointstd::ostream放入<<,则需要此功能,如下所示:

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

为此目的可能实现此方法的是:

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

您可以查看here以获取与您的Point非常相似的类的重载示例。

答案 2 :(得分:2)

std::cout << myPoint只是operator<<(std::cout, myPoint)的语法糖。

因此,您必须根据自己的需要为课程重载operator<<

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