[英]How can I get to my functions properly by friendship?
I have a abstract class like:我有一个抽象类,如:
class Polygon {
public:
vector < int > sides;
virtual void circumreference()=0;
virtual void print() = 0;
void SaveAndRead() {
save(sides);
load(sides);
}
friend void save(std::vector < int >&);
friend void load(std::vector < int >&);
};
save and load are out of class Polygon.保存和加载不在类多边形中。 I am trying to call function save and load in function SaveAndRead(), but I have errors like " save: ID not found" ," load: ID not found".
我正在尝试在函数 SaveAndRead() 中调用函数 save 和 load,但是我遇到了诸如“save: ID not found”、“load: ID not found”之类的错误。 Could somebody help me out with this?
有人可以帮我解决这个问题吗? I will be grateful!
我会很感激!
I understand that the save and load functions lay outside in an other class.我知道保存和加载函数位于其他类中。 In that case you should add the name of that class to your function definition.
在这种情况下,您应该将该类的名称添加到您的函数定义中。
friend void StoreFunctionClass::save(std::vector<int>&);
You cannot replace the regular declaration of save
and load
by only declaring them as friends of Polygon
.您不能仅通过将它们声明为
Polygon
朋友来替换save
和load
的常规声明。 That is, you must also declare them before Polygon
.也就是说,您还必须在
Polygon
之前声明它们。
Then, in order for your whole program to compile, you need to define save
and load
in a .cpp file.然后,为了编译整个程序,您需要在 .cpp 文件中定义
save
和load
。
Also you probably want save
at least to take a const
reference.此外,您可能至少希望
save
以获取const
引用。
//polygon.hpp
#include <vector>
void save(const std::vector < int >&);
void load(std::vector < int >&);
class Polygon {
public:
std::vector < int > sides;
virtual void circumreference()=0;
virtual void print() = 0;
void SaveAndRead() {
save(sides);
load(sides);
}
friend void save(const std::vector < int >&);
friend void load(std::vector < int >&);
};
// polygon.cpp
#include "polygon.hpp"
void save(const std::vector < int >&){
// Code for save
}
void load(std::vector < int >&){
// Code for load
}
Non related aspects:非相关方面:
load
is used to initialize its argument, you probably want to return the vector by value.load
初始化其参数,您可能希望按值返回向量。save
and load
seems to be good candidate to be actual members of Polygon
. save
和load
似乎很适合成为Polygon
实际成员。 Friendship rarely is a good design choice. More about the use of friend
:更多关于
friend
的使用:
One typical use of friend
is to implement iterators or any class intended to offer a specific "view" of another class. friend
一个典型用途是实现迭代器或任何旨在提供另一个类的特定“视图”的类。 You want to give full access to this "view" class as it is conceptually part of the class it gives access to.您希望授予对这个“视图”类的完全访问权限,因为它在概念上是它提供访问权限的类的一部分。
Some people also like to overload operator<<
for streams using friend
instead of defining a print
public method called by the operator<<
overload, which is acceptable.有些人还喜欢使用
friend
为流重载operator<<
,而不是定义一个由operator<<
重载调用的print
公共方法,这是可以接受的。
Finally, commutative operators such as operator+
can be implemented either as a public member taking the other instance as argument or as a free friend function taking the two instances as arguments.最后,诸如
operator+
类的可交换运算operator+
可以实现为将另一个实例作为参数的公共成员,也可以作为将两个实例作为参数的自由友元函数来实现。 The first approach is more concise while the second reflects the symmetry of the operator better.第一种方法更简洁,而第二种方法更好地反映了算子的对称性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.