请在标记为重复之前阅读
我正在重载运算符>>和<<用于读取具有实部r和虚部i的复数;
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class complex
{
int r,i;
public:
complex()
{ i=r=0;}
friend istream& operator>>(istream&, complex&);
friend ostream& operator<<(ostream&,complex&);
};
istream& operator>>(ifstream &din, complex &x)
{
din>>x.r;
din>>x.i;
return din;
}
ostream& operator<<(ostream &dout, complex &x)
{
dout<<x.r<<x.i;
return dout;
}
void main()
{
clrscr();
complex x;
cin>>x;
cout<<x;
}
Run Code Online (Sandbox Code Playgroud)
错误是代码部分无法访问r和i
din>>x.r;
din>>x.i;
错误是r和i是私有的,因此无法访问不是正常的朋友功能能够访问私有变量.为什么只有>>失败?
注意:<<运算符工作正常.只有>>失败
c++ operator-overloading cin private-members friend-function
所以我试图重载<<运算符.从我可以看到的所有来源,语法是正确的,但eclipse不喜欢它.
我收到了一些错误: Polynomial :: PrivateStruct*Polynomial :: head是私有的
并且: struct Polynomial :: PrivateStruct是私有的.
我想保持这个结构私有,以隐藏实现细节.
std::ostream& operator<<(std::ostream& outputStream, Polynomial& rhs)
{
Polynomial::PrivateStruct *p = rhs.head;
//implementation details
return outputStream;
}
Run Code Online (Sandbox Code Playgroud)
和声明:
friend std::ostream& operator<<(std::ostream& outputStream, const Polynomial& rhs);
Run Code Online (Sandbox Code Playgroud) 我使用友元函数重载了预增量运算符.在重载的friend函数中,变量的值显示正确.但是这个值没有显示在显示功能中,为什么呢?
#include <iostream>
using namespace std;
class Rectangle {
public:
int breadth;
public:
void read();
void display();
friend void operator ++(Rectangle r1);
};
void Rectangle::read()
{
cout << "Enter the breadth of the Rectangle: ";
cin >> breadth;
}
void operator++(Rectangle r1)
{
++r1.breadth;
cout<<r1.breadth<<endl; //correct result
}
void Rectangle::display()
{
cout<<breadth<<endl; // not showing pre-incremented value, why ???
}
int main()
{
cout<<"Unary Operator using Friend Function \n";
Rectangle r1;
r1.read();
++r1;
cout << "\n breadth of Rectangle after …Run Code Online (Sandbox Code Playgroud) 我最近开始学习C++,做一些简单的类/朋友函数练习,我试图做的是,通过使用友元函数从用户获取2个数字,然后再使用友元函数,再乘以2数字并在屏幕上显示.假设我按顺序输入了2,3,4,5.预期输出为6和20,但我只能在屏幕上看到0和0.
#include<iostream>
using namespace std;
class iluvcpp {
int no_1, no_2;
public:
iluvcpp(){}
~iluvcpp(){}
friend void inputno(iluvcpp obj);
friend int multiply(iluvcpp obj);
}multi_1, multi_2;
void inputno(iluvcpp obj) {
cout <<"Enter no 1: ";
cin >> obj.no_1;
cout <<"Enter no 2: ";
cin >> obj.no_2;
}
int multiply(iluvcpp obj) {
return ((obj.no_2)*(obj.no_1));
}
int main() {
inputno(multi_1);
inputno(multi_2);
cout << multiply(multi_1) <<" "<< multiply(multi_2);
cout << endl;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我希望这是一个连贯的问题...我有一个单例类定义,例如:
#include A.h
class Singleton
{
public:
// If I change this to a return by pointer, it works fine. But reference gives me problems.
static Singleton &getInstance();
Singleton(Singleton const&) = delete;
void operator=(Singleton const&) = delete;
~Singleton();
friend void A::friendMethodinA();
private:
Singleton();
void methodOnlyNeededByA();
}
Run Code Online (Sandbox Code Playgroud)
类的定义是:
Singleton &Singleton::getInstance()
{
static Singleton instance;
return instance;
}
Singleton::~Singleton() {}
Singleton::Singleton() {}
void Singleton::methodOnlyNeededByA() { // method body. }
Run Code Online (Sandbox Code Playgroud)
我的A类声明是:
#pragma once
class Singleton;
class A
{
public:
void friendMethodinA();
private:
// …Run Code Online (Sandbox Code Playgroud) 我试图理解如何将函数定义为friend function影响其在内存(RAM)中的位置.
例如,每个类都有一个包含其所有方法和函数的表.此外,虚拟功能放在vtable.
friend功能在哪里?
我担心的原因是由于[递归]函数已经通过我的c ++代码中的多个线程调用了很多次,最终我得到了"v'table腐败运行时异常".这是内存损坏的标志(正如我在这里看到的那样).
此外,当将此函数声明为常规的类外函数时,异常仍然存在.
friend然而,当声明该功能时(这是一个糟糕的设计,但为了实验),该异常不再弹出.
因此,我的问题是关于朋友功能的记忆位置.