c ++ friend function - operator overloading istream>> [英] c++ friend function - operator overloading istream >>

查看:133
本文介绍了c ++ friend function - operator overloading istream>>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于朋友的功能,以及重载<<和>>。从我的理解,我认为朋友函数可以(应该)直接访问私有成员变量。但是在这种情况下,编译器只接受我的.cxx文件,当我使用get函数来获取每个私有变量。



这里是我的头文件

  class BigNum 
public:

// Constructors和DESTRUCTORS
BigNum );
BigNum(int num,size_t optional_base = 10);
BigNum(const char strin [],size_t optional_base = 10);

//会员函数
size_t get_digit(size_t index)const;
size_t get_used()const;
size_t get_capacity()const;
size_t get_base()const;
bool get_sign()const;

//友好函数
friend std :: ostream& operator<<(std :: ostream& os,const BigNum& bignum);
friend std :: istream& operator>>(std :: istream& is,BigNum& bignum);

private:
size_t base;
size_t * digits;
bool positive;
size_t used;

这是我的对应的.cxx文件的朋友函数的实现

  #includefile.h
#include< cstdlib>
#include< iostream>
#include< string>
#include< cstring>

using namespace std;

std :: ostream&运算符<<<<(std :: ostream& os,const BigNum& bignum)
{
if(bignum.get_sign()== false)
os< ' - ';

for(size_t i = 0; i os<< bignum.get_digit(bignum.get_used() - i - 1);

return os;
}

std :: istream& operator>>>(std :: istream& is,BigNum& bignum)
{
for(size_t i = 0; i 是>> bignum.digits [i];

return is;
}

因此在这方面上面的friend运算符正确编译。但是为什么我的操作符>>可以直接访问一个私有变量(>> bignum.digits [i]),但其余的私有变量需要通过get functions来检索



下面,当我尝试写这方面的重载操作符(我认为朋友函数应该适当地调用私有变量):

  std :: ostream&运算符<<<<(std :: ostream& os,const BigNum& bignum)
{
if(bignum.positive == false)
os< ' - ';

for(size_t i = 0; i os<< bignum.digits [used - i-1];

return os;
}

std :: istream& operator>>(std :: istream& is,BigNum& bignum)
{
for(size_t i = 0; i 是>> bignum.digits [i];

return is;
}

我获得以下错误。

  BigNum2.cxx:在函数`std :: ostream& 
csci2270_hw1B :: operator<<<(std :: ostream& const csci2270_hw1B :: BigNum&)':
BigNum2.cxx:201:error:`used'undeclared(first use this function)
BigNum2.cxx:201:error:(对于
,每个未声明的标识符仅报告一次。)
BigNum2.cxx:在`std :: istream&
csci2270_hw1B :: operator>>(std :: istream& csci2270_hw1B :: BigNum&)':
BigNum2.cxx:208:error: / code>

我使用的编译器是g ++(版本3.3.1)。



我更新了代码,所以bignum对象可以访问私有变量。我做了下面的朋友操作符重载<<编译好。感谢评论,这是一个菜鸟的错误。

  std :: ostream&运算符<<<<(std :: ostream& os,const BigNum& bignum)
{
if(bignum.positive == false)
os< ' - ';

for(size_t i = 0; i os<< bignum.digits [bignum.used - i - 1];

return os;但是编译器仍然会为>>运算符

生成错误。



BigNum2.cxx:在函数 std :: istream&
csci2270_hw1B :: operator>>(std :: istream& csci2270_hw1B :: BigNum&)':
BigNum2.cxx:208:error:语法错误
令牌



>>应该读入一个数字,私有成员变量'used'应该记录数组的长度。我仍然有点困惑为什么编译器接受

  std :: istream& operator>>(std :: istream& is,BigNum& bignum)
{
for(size_t i = 0; i 是>> bignum.digits [i];

return is;
}

而不是:

  std :: istream& operator>>(std :: istream& is,BigNum& bignum)
{
for(size_t i = 0; i 是>> bignum.digits [i];

return is;
}

有任何想法吗?感谢。

解决方案

朋友函数可以访问类的私有数据,得到一个这个指向使自动,所以每一次访问类数据(私人或其他)必须合格。例如:

  os<< bignum.digits [used-i-1]; 

必须为:

  os<< bignum.digits [bignum.used  -  i  -  1]; 


My question is in regards to friend functions as well as overloading the << and >>. From my understanding I thought friend functions could (and should) access private member variables directly. However in the case I have here the compiler would only accept my .cxx file when I used "get" functions to obtain each private variable.

Here is my header file

class BigNum 
public:

// CONSTRUCTORS and DESTRUCTORS
    BigNum();                            
    BigNum(int num, size_t optional_base = 10);                         
    BigNum(const char strin[], size_t optional_base = 10);

// MEMBER FUNCTIONS
    size_t get_digit(size_t index) const;
    size_t get_used() const;
    size_t get_capacity() const;
    size_t get_base() const;
    bool get_sign() const;

// FRIEND FUNCTIONS
    friend std::ostream& operator<<(std::ostream &os, const BigNum &bignum);
    friend std::istream& operator>>(std::istream &is, BigNum &bignum);

private:
    size_t base;            
    size_t *digits;          
    bool positive;          
    size_t used;              

Here is my corresponding .cxx file with the implementations for the friend functions

#include "file.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

std::ostream& operator <<(std::ostream &os, const BigNum &bignum)
{
if (bignum.get_sign() == false)
    os << '-';

for (size_t i = 0; i < bignum.get_used(); ++i)
    os << bignum.get_digit(bignum.get_used() - i - 1);

return os;
}

std::istream& operator >>(std::istream &is, BigNum &bignum)
{
for (size_t i = 0; i < bignum.get_used(); ++i)
    is >> bignum.digits[i];

return is;
}

So in this regard the above friend operators compiled correctly. However why is it that my operator >> can access one private variable directly (is >> bignum.digits[i]) but the rest of the private variables need to be retrieved by 'get functions'

Below, when I try to write the overload operators in this regard (how I thought friend functions should properly call private variables):

std::ostream& operator <<(std::ostream &os, const BigNum &bignum)
{
if (bignum.positive == false)
    os << '-';

for (size_t i = 0; i < bignum.used; ++i)
    os << bignum.digits[used - i - 1];

return os;
}

std::istream& operator >>(std::istream &is, BigNum &bignum)
{
for (size_t i = 0; i < bignum.used); ++i)
    is >> bignum.digits[i];

return is;
}

I obtain the following errors.

BigNum2.cxx: In function `std::ostream&
   csci2270_hw1B::operator<<(std::ostream&, const csci2270_hw1B::BigNum&)':
BigNum2.cxx:201: error: `used' undeclared (first use this function)
BigNum2.cxx:201: error: (Each undeclared identifier is reported only once for
   each function it appears in.)
BigNum2.cxx: In function `std::istream&
   csci2270_hw1B::operator>>(std::istream&, csci2270_hw1B::BigNum&)':
BigNum2.cxx:208: error: syntax error before `)' token

The compiler I am using is g++ (Version 3.3.1). Any help is appreciated, thank you.

Revised:

I updated the code so the bignum object could access the private variables. I did the following to the friend operator overloading << and it compiled fine. Thanks for the comments, that was a rookie mistake.

std::ostream& operator <<(std::ostream &os, const BigNum &bignum)
{
if (bignum.positive == false)
    os << '-';

for (size_t i = 0; i < bignum.used; ++i)
    os << bignum.digits[bignum.used - i - 1];

return os;
}

However the compiler is still producing errors for the >> operator

BigNum2.cxx: In function std::istream& csci2270_hw1B::operator>>(std::istream&, csci2270_hw1B::BigNum&)': BigNum2.cxx:208: error: syntax error before)' token

The >> is supposed to read in a number and the private member variable 'used' is supposed to record the length of the array. I am still somewhat confused on why the compiler accepts

std::istream& operator >>(std::istream &is, BigNum &bignum)
{
for (size_t i = 0; i < bignum.get_used()); ++i)
    is >> bignum.digits[i];

return is;
}

as opposed to:

std::istream& operator >>(std::istream &is, BigNum &bignum)
{
for (size_t i = 0; i < bignum.used); ++i)
    is >> bignum.digits[i];

return is;
}

Any thoughts? thanks.

解决方案

A friend function has access to the class' private data, but it does not get a this point to make that automatic, so every access to the class data (private or otherwise) has to be qualified. For example this:

os << bignum.digits[used - i - 1];

needs to be:

os << bignum.digits[bignum.used - i - 1];

这篇关于c ++ friend function - operator overloading istream&gt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆