c++ - c++ 友元函数的问题

标签 c++ eclipse friend

我在h 文件 中用一些友元函数定义了以下类:

#include <iostream>
#include <math.h>

namespace mtm {
static bool compareIsBigger(double a, double b) {
    const double EPSILON = 1e-10;
    return fabs(a - b) < EPSILON ? false : a > b;
}

static bool areSame(double a, double b) {
   const double EPSILON = 1e-10;
   return fabs(a - b) < EPSILON;
}
class Security  {
private:
    char* name;
    double stockValue;
    int stockAmount;
    double* quarterAnalyse;
    void operator =(const Security&) = delete;
    static bool nameIsInCaps(const char* name);
public:
    //// *other functions not relevant*
    friend bool operator==(const Security& s1, const Security& s2);
    friend bool operator!=(const Security& s1, const Security& s2);
    friend bool operator>=(const Security& s1, const Security& s2);
    friend bool operator<(const Security& s1, const Security& s2);
    friend bool operator>(const Security& s1, const Security& s2);
    friend bool operator<=(const Security& s1, const Security& s2);
    friend bool operator>=(const Security& s1, const Security& s2);
};
}

然后我在 c 文件 中实现了它们(当然包括标题):

#include "Security.h"
#include "exception.h"
#include <iostream>
#include <cstring>

using namespace mtm;

bool operator==(const Security &s1, const Security &s2) {
    if (areSame(s1.stockValue, s2.stockValue) && s1.stockAmount == s2.stockAmount
        && !strcmp(s1.name, s2.name))
        return true;
    return false;
}

bool operator!=(const Security &s1, const Security &s2) {
    if (!(s1 == s2))
        return true;
    return false;
}

bool operator<(const Security &s1, const Security &s2) {
    if (compareIsBigger(s1.stockValue, s2.stockValue))
        return true;
    if (areSame(s1.stockValue, s2.stockValue)) {
        if (s2.stockAmount > s1.stockAmount)
            return true;
        if (s2.stockAmount == s1.stockAmount) {
            if (strcmp(s2.name, s1.name) > 0)
                return true;
        }
    }
    return false;
}


bool operator>(const Security &s1, const Security &s2) {
    if (!(s1 < s2) && s1 != s2)
        return true;
    return false;
}


bool operator<=(const Security &s1, const Security &s2) {
    if (!(s1 > s2))
        return true;
    return false;
}

bool operator>=(const Security &s1, const Security &s2) {
    if (!(s1 < s2))
        return true;
    return false;
}

但是,对于这些实现中的每一个,编译器都会提示这些函数无法访问私有(private)部分!

这是为什么呢?我已将它们声明为友元函数!
如果有任何问题,我正在使用 eclipse。

这些是关于这个问题的编译结果:

Description Resource    Path    Location    Type
'char* mtm::Security::name' is private  Security.h  /MTM_HW4    line 32 C/C++ Problem
'double mtm::Security::stockValue' is private   Security.h  /MTM_HW4    line 33 C/C++ Problem
'int mtm::Security::stockAmount' is private Security.h  /MTM_HW4    line 34 C/C++ Problem
ambiguous overload for 'operator!=' (operand types are 'const mtm::Security' and 'const mtm::Security') Security.cpp    /MTM_HW4    line 183    C/C++ Problem
ambiguous overload for 'operator' (operand types are 'const mtm::Security' and 'const mtm::Security')  Security.cpp    /MTM_HW4    line 190    C/C++ Problem
within this context Security.cpp    /MTM_HW4    line 155    C/C++ Problem
within this context Security.cpp    /MTM_HW4    line 156    C/C++ Problem
within this context Security.cpp    /MTM_HW4    line 168    C/C++ Problem
within this context Security.cpp    /MTM_HW4    line 170    C/C++ Problem
within this context Security.cpp    /MTM_HW4    line 171    C/C++ Problem
within this context Security.cpp    /MTM_HW4    line 173    C/C++ Problem
within this context Security.cpp    /MTM_HW4    line 174    C/C++ Problem
bool mtm::operator!=(const mtm::Security&, const mtm::Security&)    Security.h  /MTM_HW4    line 60 C/C++ Problem
bool mtm::operator(const mtm::Security&, const mtm::Security&) Security.h  /MTM_HW4    line 63 C/C++ Problem
bool operator!=(const mtm::Security&, const mtm::Security&) Security.cpp    /MTM_HW4    line 161    C/C++ Problem
bool operator(const mtm::Security&, const mtm::Security&)  Security.cpp    /MTM_HW4    line 182    C/C++ Problem
candidates are: Security.cpp    /MTM_HW4    line 162    C/C++ Problem
candidates are: Security.cpp    /MTM_HW4    line 183    C/C++ Problem
candidates are: Security.cpp    /MTM_HW4    line 190    C/C++ Problem
candidates are: Security.cpp    /MTM_HW4    line 196    C/C++ Problem

最佳答案

您还需要在头文件中的类外部声明运算符重载。

关于c++ - c++ 友元函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30652137/

相关文章:

C++ "simple"具有任意参数和返回值的函数的函数回调

c++ - 多项系数代码总是给出相同的答案

c++ - 关于在 Visual Studio 2010 中使用 strcpy(),我不明白什么?

java - FindBugs插件Eclipse发现的错误

c++ - 成员函数 Outer::f() 不是类 Outer::Inner 的 friend 。为什么?

iOS 从我的好友列表中对使用某个特定应用程序的 facebook 好友进行排序

C++ 变量内存分配

eclipse - 贾瓦尔 (JNI) 不可用

c++ - operator<< 用于嵌套类

java - 如何用Java制作一个可移植应用程序(jar+sqlite)?