c++中友元函数的问题

issue with friend functions in c++

本文关键字:问题 函数 友元 c++      更新时间:2023-10-16

我在和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;
}

然而,对于每一个实现,编译器都抱怨这些函数不能访问私有部分!

为什么?我已经将它们声明为友元函数!
如果有什么关系的话,我正在用eclipse。

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

<>之前描述资源路径位置类型'char* mtm::Security::name'是private Security.h/MTM_HW4 line 32 C/c++问题'double mtm::Security::stockValue'是private Security.h/MTM_HW4 line 33 C/c++问题'int mtm::Security::stockAmount'是私有的Security.h/MTM_HW4 line 34 C/c++问题"operator!"的歧义重载='(操作数类型为'const mtm::Security'和'const mtm::Security') Security.cpp/MTM_HW4 line 183 C/c++问题(操作数类型为'const mtm::Security'和'const mtm::Security') Security.cpp/MTM_HW4 line 190 C/c++问题Security.cpp/MTM_HW4 line 155 C/c++问题Security.cpp/MTM_HW4 line 156 C/c++ ProblemSecurity.cpp/MTM_HW4 line 168 C/c++问题Security.cpp/MTM_HW4 line 170 C/c++问题Security.cpp/MTM_HW4 line 171 C/c++问题Security.cpp/MTM_HW4 line 173 C/c++问题Security.cpp/MTM_HW4 line 174 C/c++问题bool mtm:运营商!=(const mtm::Security&, const mtm::Security&) Security.h/MTM_HW4 line 60 C/c++ Problembool mtm::operator(const mtm::Security&, const mtm::Security&) Security.h/MTM_HW4 line 63 C/c++ Problembool运营商!=(const mtm::Security&, const mtm::Security&) Security.cpp/MTM_HW4 line 161 C/c++问题bool operator(const mtm::Security&, const mtm::Security&) Security.cpp/MTM_HW4 line 182 C/c++ Problem候选代码为:Security.cpp/MTM_HW4 line 162 C/c++ Problem候选代码为:Security.cpp/MTM_HW4 line 183 C/c++ Problem候选代码为:Security.cpp/MTM_HW4 line 190 C/c++ Problem候选代码为:Security.cpp/MTM_HW4 line 196 C/c++ Problem

还需要在头文件的类外部声明操作符重载。