我遇到了我的operator<<重载问题,无论我做什么都无法访问它所属类的私有变量,因为它会说变量作为编译器错误是私有的.这是我目前的代码:
#include "library.h"
#include "Book.h"
using namespace cs52;
Library::Library(){
myNumberOfBooksSeenSoFar=0;
}
//skipping most of the functions here for space
Library operator << ( ostream &out, const Library & l ){
int i=myNumberOfBooksSeenSoFar;
while(i<=0)
{
cout<< "Book ";
cout<<i;
cout<< "in library is:";
cout<< l.myBooks[i].getTitle();
cout<< ", ";
cout<< l.myBooks[i].getAuthor();
}
return (out);
}
Run Code Online (Sandbox Code Playgroud)
而函数原型和私有变量library.h都是
#ifndef LIBRARY_H
#define LIBRARY_H
#define BookNotFound 1
#include "Book.h"
#include <iostream>
#include <cstdlib>
using namespace std;
namespace cs52{
class Library{
public:
Library(); …Run Code Online (Sandbox Code Playgroud) 所以我现在正在学习c ++类中的继承,并试图将所有术语都放在一边.我理解受保护和私有之间的区别.但是当你创造一个功能时,朋友与公开它不是一回事吗?
他们用来教导继承的例子是形状,所以:
class shape{
struct circle
struct rectangle
struct line
}
Run Code Online (Sandbox Code Playgroud)
这样的事情,说法之间有什么区别
class circle: shape
class circle: public shape
class circle: private shape (don't actually know if this one is possible)
Run Code Online (Sandbox Code Playgroud)
最后,关键字虚拟意味着/做/用于什么?谢谢
我的私有构造函数中有一个问题,如下所示:
Lane.hpp:
namespace sim_mob
{
class B
{
friend class A;
private:
B(int){}
};
}
Run Code Online (Sandbox Code Playgroud)
xmll.hpp:
#include"Lane.hpp"
namespace geo
{
class A
{
public:
A()
{
sim_mob::B b(2);
}
};
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include"xmll.hpp"
int main()
{
geo::A a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
命令:$ g ++ main.cpp
In file included from main.cpp:2:0:
Lane.hpp: In constructor ‘geo::A::A()’:
Lane.hpp:10:5: error: ‘sim_mob::B::B(int)’ is private
xmll.hpp:9:23: error: within this context
Run Code Online (Sandbox Code Playgroud)
关键是如果构造函数中没有任何参数,我就不会收到此错误.我可以知道为什么我会得到这个以及如何解决它?非常感谢
我有一个类 A,我需要从中访问类 B 的受保护成员,其方式与在 C++ 中使用朋友关键字的方式相同。但是,内部修饰符不适合我的需要。B 类将需要创建 A 类的实例,修改其私有数据,并返回对该类的引用。这些 A 类成员需要对原始调用者保持私有。
public class A
{
protected int x;
}
public class B
{
public static A CreateClassA()
{
A x = new A();
x.x = 5; // ERROR : No privilege
return x;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在调试一个与第三方库交互的应用程序,其代码不可用,只有头文件和 .so 可用。现在我可以将它加载到调试器中并检查在第三方库中声明的类的私有成员的变量值,但是由于对象的数量很大,我想创建一些机制来在控制台上打印它,我可以稍后分析。我想出了这样的事情
第三方头文件
class A
{
private:
int i;
};
Run Code Online (Sandbox Code Playgroud)
我没有在上面的课程中包含额外的细节
调试打印机.cpp
#include <thirdpartheaders>
template <typename T> class debugprinter
{
friend class T;
public :
void printonconsole()
{
T a;
std::cout << std::endl << a.i << std::endl;
return;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我试图在上面编译,但似乎我不能将未定义的类型 T 声明为我的模板类的朋友并得到这个错误
错误:无法从 x::acc() 访问 i
现在我可以通过创建非模板调试打印机来解决这个问题,但出于好奇,有没有一种方法可以创建一个模板类,它会成为它的输入类型参数的朋友?
谢谢
这是C++中非常疯狂的定点运算实现.请不要评论这是多么糟糕和毫无意义.
如您所见,小数部分有一个基本类型T和一些二进制数字N.需要FixedNum<int, A> + FixedNum<int, B>进行评估.FixedNum<int, MAX(A, B)>.以下是我尝试实现它的方法.但是,海湾合作委员会表示,x最后一行的任务是错误的,因为x受到保护.怎么了?
#define MAX(a,b) (((a)>(b))?(a):(b))
template <typename T, int N>
class FixedNum
{
public:
template <int N2>
friend FixedNum& operator+(const FixedNum& f, const FixedNum& g);
template <int N2>
friend FixedNum& operator+=(const FixedNum& f, const FixedNum& g);
FixedNum(T x): x (x) {}
T num() const { return x; }
int point() const { return N; }
protected:
T x; …Run Code Online (Sandbox Code Playgroud) 我试图在两个类之间创建一个朋友关系.以下是示例:
class A
{
public:
int b;
private:
friend class B;
int a;
};
class B
{
public:
A abc;
};
int main ()
{
B b;
b.abc.b = -1;
b.abc.a = 0;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它在编译时出现以下错误:
test.cpp:在函数'int main()'中:test.cpp:20:9:错误:'int A :: a'在此上下文中是私有的b.abc.a = 0; ^ test.cpp:7:7:注意:在这里声明私有int a; ^
任何帮助将非常感激.
我使用以下语法在结构中重载插入运算符(<<):
struct Address{
string street;
string cross;
int suite;
friend ostream &operator <<(ostream &oss, const Address &other){
oss<<"street: "<<other.street<<"cross: "<<other.cross<<"suite: "<<other.suite;
return oss;
}
};
Run Code Online (Sandbox Code Playgroud)
我看到,只有当我将该函数声明为struct'Address'的朋友时才会编译我的代码.根据我的理解,当需要访问类的私有成员时,友元函数很有用.但是,因为在结构中所有成员都是公共的,所以不应该将'<<'运算符声明为朋友.
有人可以澄清是否需要在这里声明'<<'运算符作为结构'地址'的朋友?
这段代码编译失败:
class P {
//public:
class C {
friend std::ostream& operator<<(std::ostream &os, const C &c);
};
};
std::ostream& operator<<(std::ostream &os, const P::C &c) {
return os;
}
Run Code Online (Sandbox Code Playgroud)
错误:
test.cpp:12:53: error: 'C' is a private member of 'P'
std::ostream& operator<<(std::ostream &os, const P::C &c) {
^
test.cpp:6:9: note: implicitly declared private here
class C {
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
取消注释public:会使该代码能够编译。它显然可以转移到班级本身。
operator<<但是在 cpp 文件中为私有成员类定义此类的正确方法是什么?
我有一个类SomeClass,我希望实现一个重载==来比较这个类的两个实例。
重载==不使用任何SomeClass私有成员。因此,它不必是friend.
我如何使它成为非会员、非好友功能?
目前,这就是我的代码的样子:
某个类.h
#ifndef SOMECLASS_H
#define SOMECLASS_H
class SomeClass
{
public:
// Other class declarations, constructors
friend bool operator==(const SomeClass a, const SomeClass b);
};
Run Code Online (Sandbox Code Playgroud)
某个类.cpp
#include "someclass.h"
// Other stuff
bool operator==(const SomeClass a, const SomeClass b) {
// do some comparison and return true/false
}
Run Code Online (Sandbox Code Playgroud) friend ×10
c++ ×9
class ×3
private ×2
templates ×2
c# ×1
c++11 ×1
c++17 ×1
constructor ×1
inheritance ×1
polymorphism ×1
stream ×1
struct ×1
virtual ×1