简体   繁体   English

我怎样才能通过友谊正确到达我的功能?

[英]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朋友来替换saveload的常规声明。 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 文件中定义saveload

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:非相关方面:

  • If 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 . saveload似乎很适合成为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.

相关问题 我怎样才能让我的柜台正常工作? - How can I get my counter to work properly? 我应该如何声明我的对象才能正确使用这些函数? - How should i declare my objects in order to use the functions properly? 如何使用我的函数填充向量? - How can I populate a vector with my functions? 我怎样才能修复我的程序的计算并让它正常运行? - How can I fix my program's calculation and get it to run properly? C++,在 class 指针向量上使用 std::sort - 或者我无法正确命名我的函数 - C++, using std::sort on a vector of class pointers - OR I can't name my functions properly 输入非整数值时,我的二次方程式代码会终止,如何使其正确循环? - My Quadratic equation code terminates when I enter a non integer value, how can I get it to loop properly? 如何让我的代码在 Visual Studio 上正确运行 - How do I get my code to properly run on Visual Studio 如何显示 gdb 以正确显示我的变量? - How can I show gdb to show my variables properly? 如何正确解析我的文件? (使用中断/继续) - How can I properly parse my file? (Using break/continue) 静态初始化器的上下文是什么,我如何声明与它的友谊? - what is the context of a static initializator, and how do I declare a friendship to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM