gpt4 book ai didi

C++ 将 ostream 运算符重载为友元会导致错误 : variable is private within this context

转载 作者:行者123 更新时间:2023-11-27 22:46:34 26 4
gpt4 key购买 nike

<分区>

我无法捕捉到这个错误,这一定很容易。我有一个头文件 (snim.h):

#ifndef SNIM_CLASS_HH_
#define SNIM_CLASS_HH_

#include <iostream>

namespace snim {

class SnimModel {

int communitySize; // Total size of the community


public:
SnimModel(int c) : communitySize(c) {};

friend std::ostream& operator<<(std::ostream&, const SnimModel&);
};


} /* end namespace */

#endif

和一个实现文件:

#include "snim.h"

using namespace snim;


std::ostream& operator<<(std::ostream& os, const SnimModel& s) {

os << "[Total Size]\n[";
os << s.communitySize << "]\n";

return os;
};

因此,当我尝试编译时,它给出了

 In function ‘std::ostream& operator<<(std::ostream&, const snim::SnimModel&)’:

snim.cpp:9:11: error: ‘int snim::SnimModel::communitySize’ is private within this context
os << s.communitySize << "]\n";

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com