写友元函数时出现的错误--- 1 IntelliSense: class "point" 没有成员 "operator<<"

博客内容讲述了在C++编程中遇到的一个错误:IntelliSense提示point类没有成员'operator<<'。作者分析了错误原因,并提供了修复方案。错误源于在实现友元函数`ostream& operator<<(ostream& o, point& p)`时,不应该使用类的成员访问操作符`::`。正确的实现方式是将友元函数作为独立函数定义,不与任何类绑定。" 6786145,1167829,Hibernate连接配置与数据库连接池,"['数据库连接池', 'Hibernate', 'schema', 'c3p0', 'session']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误代码如下:


#ifndef _Point_h_
#define _Point_h_
#include "stdafx.h"
#include<iostream>


using namespace std;


class point
{
public:
point(double m,double n);
~point(){};
point& operator ++();
point operator ++(int);
point& operator --();
point operator --(int);
    friend ostream& operator << (ostream& o,point& p);




private:
double x;
double y;


};


#endif



类的实现:


#include "stdafx.h"
#include"Point.h"
#include<iostream>
#include<ostream>
using namespace std; 


//函数的实现


point::point(double m,double n)
{  x=m;
   y=n;
}


point& point:: operator ++()            //如果返回值不是应用,就不能进行取址运算,只是一个临时变量。++a=5
{
++x;
++y;
return *this;
}
point point:: operator ++(int)
{
point old=(*this);
++(*this);
return old;
}


point& point:: operator --()
{
--x;
--y;
return *this;
}


point point:: operator --(int)
{
point old=(*this);
--(*this);
return old;
}


 ostream& point:: operator <<(ostream& o,point& p)
{
   
.....................
}


注意:frinend 关键字函数  并不是类的成员函数 ,所以在友元函数时不能像成员函数一样用类的::来限定;

          所以 要把友元函数的实现改为:

          ostream&   operator <<(ostream& o,point& p)
{
   
.....................
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值