c++中友元函数的问题
issue with friend functions in c++
我在和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还需要在头文件的类外部声明操作符重载。
相关文章:
- Visual Studio中的函数声明和函数定义问题
- C++quit()函数中可能存在作用域问题
- 类似于strcat()的函数出现问题
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 类C++中的函数问题(LNK2019和LNK1120错误)
- c++binary_search函数排序数组(流行名称搜索)出现问题
- 关于简单C++函数(is_palindrome)的逻辑的问题
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- constexpr构造函数需要常量成员函数时出现问题
- 添加存储在向量中的大整数的函数出现问题
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 关于 c++ 函数中指针赋值的简单问题
- 如何在标头中声明(或定义)函数的问题
- 有关 c++ 构造函数的问题
- 模板类转换问题 - 无法推断调用的函数
- 从 C++ 中的函数返回数组地址问题
- 关于复制构造函数的一个棘手问题
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题