运算符 << 不匹配(操作数类型 std::ostream) C++ OOP 和点
no match for operator << (operand types std::ostream) c++ OOP and Point
我试图显示我通过成员函数创建的点类的p对象。我已经传递点p作为参数在void displayPoint(点p)成员函数我的程序。但是我在我的程序中得到以下编译错误!
D:OOP Assignment # 01 Point .cpp[Error] no match for 'operator<<'(操作数类型为'std::ostream{又称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(){
}
void Point::displayPoint(Point p){
cout << p; // ERROR OCCURING HERE!!!
}
没有重载<<
操作符来直接输出Point
类的对象。所以你不能这么做。您可以添加一个重载的operator<<
或调用相应的get函数来获取Point
的数据成员。
void Point::displayPoint(Point p){
cout << p.getX() << " " << p.gety() << endl;
}
operator<<
的内容。您还没有定义
std::ostream& operator<<( std::ostream&, const Point&);
当您想使用<<
将Point
放入std::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;
}
您可以在这里查看与您的Point非常相似的类的一些重载示例。
std::cout << myPoint
只是operator<<(std::cout, myPoint)
的语法糖。
所以你必须根据你的需要重载你的类的operator<<
:
std::ostream& operator<<(std::ostream& os, const Point& p)
{
os << p.getX() << "/" << p.getY();
return os;
}
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- ostream过载时的缓冲区冲洗
- 需要从 istream 和 ostream 派生 iostream
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- "ostream &os"有什么用?
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 我的运算符重载是否有效<<(流插入)左操作数不是 ostream
- C++表达SFINAE和ostream操纵器
- 在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
- oStream 不打印添加两个 valarray 的结果(使用运算符重载)
- C++运算符<<调用::ostream而不是std::osttream
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- C++ostream:没有运算符匹配<<&应在'&'代币
- 重载运算符<<用于ostream语法
- 重载ostream&时转换错误无效;operator< & lt;
- 运算符<<(ostream&os,..)用于模板类
- std::ostream&operator< & lt; (std:: ostream&压力,压力& &;val)
- 上Ostream & lt; & lt;重载的困惑