Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法访问重载中的友元类私有变量<<&引用;及&燃气轮机&燃气轮机&引用;(使用多个文件) 我刚刚学习C++中的朋友类和重载操作符,我遇到了一个问题,我不能访问朋友类中的私有变量。我几乎可以肯定,这与我们需要将每个文件分开(Header、.cpp和main)的事实有关。需要一些新鲜的眼睛_C++_Overloading_Friend - Fatal编程技术网

无法访问重载中的友元类私有变量<<&引用;及&燃气轮机&燃气轮机&引用;(使用多个文件) 我刚刚学习C++中的朋友类和重载操作符,我遇到了一个问题,我不能访问朋友类中的私有变量。我几乎可以肯定,这与我们需要将每个文件分开(Header、.cpp和main)的事实有关。需要一些新鲜的眼睛

无法访问重载中的友元类私有变量<<&引用;及&燃气轮机&燃气轮机&引用;(使用多个文件) 我刚刚学习C++中的朋友类和重载操作符,我遇到了一个问题,我不能访问朋友类中的私有变量。我几乎可以肯定,这与我们需要将每个文件分开(Header、.cpp和main)的事实有关。需要一些新鲜的眼睛,c++,overloading,friend,C++,Overloading,Friend,//百分含量 #ifndef PERCENT_H #define PERCENT_H #pragma once class Percent { // friend const Percent operator >>(Percent& first, Percent& second); public: friend bool operator ==(const Percent& first, const Percent&

//百分含量

#ifndef PERCENT_H
#define PERCENT_H

#pragma once

class Percent
{
    // friend const Percent operator >>(Percent& first, Percent& second);
public:
    friend bool operator ==(const Percent& first,
        const Percent& second);

    friend bool operator <(const Percent& first,
        const Percent& second);
    Percent();

    friend istream& operator >>(istream& inputStream,
        Percent& aPercent);

    friend ostream& operator <<(ostream& outputStream,
        const Percent& aPercent);
    //There will be other members and friends.
private:
    int value;
};
\ifndef百分比
#定义百分比
#布拉格语一次
班级百分比
{
//友元常量百分比运算符>>(百分比和第一、百分比和第二);
公众:
友元布尔运算符==(常数百分比和第一个,
常数百分比(秒);
friend bool运算符>(istream和inputStream,
百分比和百分比);
friend ostream&operator(istream&inputStream,
百分比和百分比)
{
字符百分比符号;
此处出错-无法访问值
inputStream>>aPercent.value;
输入流>>百分比符号;
返回输入流;
}

ostream&operator在头文件中,在顶部添加一个
#include
。并且,使用全名
std::istream
std::ostream
,而不是
istream
ostream
friend
声明与
Percent.c中定义的声明具有不同的功能pp
.FYI:
#ifndef PERCENT#u H#定义PERCENT#u H#endif
#pragma once
做同样的事情;你不需要两者都做。
#include <iostream>
#include "Percent.h"

using namespace std;

istream& operator >>(istream& inputStream,
    Percent& aPercent)
{
    char percentSign;
                        ERROR HERE - Cannot access value
    inputStream >> aPercent.value;
    inputStream >> percentSign;
    return inputStream;
}

ostream& operator <<(ostream& outputStream,
    const Percent& aPercent)
{
    outputStream << aPercent.value << '%';
    return outputStream;
}
#endif // !PERCENT_H