2014-03-28 56 views
3

我試圖顯示我已通過成員函數創建Point類的p對象。 我已經通過了點P在虛空displayPoint(點P)我的計劃的成員函數的參數。 但我在我的程序中收到以下編譯錯誤!沒有匹配的operator <<(操作數類型的std :: ostream的)C++ OOP和點

d:\ OOP作業#01 \ point.cpp [錯誤]敵不過 '運算< <'(操作數的類型是 '的std :: ostream的{又名的std :: basic_ostream}' 和 '點')這裏下面

是我的代碼!

#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
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 Overloading C++約超載operator<<

2

您還沒有定義的,只要你想要把一個Pointstd::ostream使用<<

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

需要此功能:

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過載的一些例子一個非常相似的類作爲您的點。

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; 
} 
相關問題