- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 C++ 的新手,正在阅读 Stroustrup 的书,但我对类的运算符重载有疑问。我有这 3 个文件:
我像往常一样有两个文件,在标题中我有定义:
#ifndef EQUATIONS_H
#define EQUATIONS_H
#include<vector>
namespace equations {
//solve second degree equation
class eqSecDeg {
private:
double a;
double b;
double c;
std::vector<double> solArray;
double getDelta(double a, double b, double c);
public:
eqSecDeg(const double valA, const double valB, const double valC);
double getDelta();
std::vector<double> getSolutions();
//thrown with the error
class EquationError {
private:
char* msg;
int error_number;
public:
char* getMsg() { return msg; }
EquationError(char* a = "Error.", int err = 0) {
msg = a;
error_number = err;
};
};
//std::ostream& operator << (const std::ostream& x, eqSecDeg& A);
// ^ is commented but it gives the error that it should be declared with only 1 parameter (?)
};
}
#endif
在这里你可以看到dqsecdegree.cpp的实现:
#include<math.h>
#include "dqsecdegree.h"
namespace equations {
double eqSecDeg::getDelta(double a, double b, double c) {
return (b*b)-(4*a*c);
}
double eqSecDeg::getDelta() {
return (b*b)-(4*a*c);
}
//constructor
eqSecDeg::eqSecDeg(const double valA, const double valB, const double valC) {
if (valA == 0) {
throw EquationError("Parameter 'a' cannot be zero.", 1);
}
a = valA;
b = valB;
c = valC;
}
std::vector<double> eqSecDeg::getSolutions() {
double delta = getDelta(a,b,c);
if (delta >= 0) {
//x1 real and complex
solArray.push_back( (-b+sqrt(delta))/(2*a) );
solArray.push_back(0);
//x2 real and complex
solArray.push_back( (-b-sqrt(delta))/(2*a) );
solArray.push_back(0);
} else {
delta *= -1;
//x1 real and complex
solArray.push_back( -b/(2*a) );
solArray.push_back( (sqrt(delta)/(2*a)) );
//x2 real and complex
solArray.push_back( -b/(2*a) );
solArray.push_back( -(sqrt(delta)/(2*a)) );
}
return solArray;
}
std::ostream& eqSecDeg::operator << (std::ostream& x, eqSecDeg& A) {
return x << "something here";
}
}
SO 和 google 上有很多答案,但我找不到正确的答案。我试图了解如何使用运算符重载。我想在头文件中声明运算符重载及其在 cpp 上的实现是正确的。
friend
关键字只提供对所有成员的访问权限,而不会使该方法成为类的一部分。当我使用该关键字时,编译器告诉我没有 int/double/char/whatever 的 ostream 输出的有效定义。 我应该在哪里定义运算符重载?我无法理解我必须在哪里(以及如何)实现它。有什么建议吗?
不是很相关,但主要是做这样的事情:
//a, b, c taken from the cin istream
equations::eqSecDeg solver(a,b,c);
std::vector<double> soluzioni = solver.getSolutions();
//here I'd like to call this
std::cout << solver;
最佳答案
在 .h 文件中将函数声明为非成员函数。
std::ostream& eqSecDeg::operator<<(std::ostream& x, eqSecDeg const& A);
像您一样在 .cpp 文件中实现函数。更新它以使用 const&
而不是非常量 &
。
在任何你喜欢的地方使用它。
eqSecDeg A;
...
std::cout << A << std::endl;
关于分离的 cpp 上的 C++ ostream 重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43287596/
我正在开发一个小型图书馆,我需要做的一件事是让访问者访问一些数据并返回结果。 在一些较旧的 C++ 代码中,访问者需要声明一个 typedef return_type .例如,boost::stati
我不明白这个 ostream 函数声明是什么意思: ostream& operator<< (ostream& (*pf)(ostream&)); (特别是 (*pf)(ostream&) 部分)。我
我正在寻找一种将内容从一个 ostream 复制到另一个的方法。我有以下代码: std::ostringsteam oss; oss << "stack overflow"; { //do s
我有一个装满小雕像的游戏板。 分配: board = new Figure*[boardSize]; for (int i = 0; i > > board(boardSize, vector>(b
代码: cout SomeStream &operator SomeStream &operator (*this) << val; //Trouble in there! std::co
我正在对我的项目进行一些类型检查。下面的例子 using namespace std; cout ::value ? "TRUE":"FALSE" ) (); } 格式正确(source)。 std:
我到处搜索,但没有找到答案,所以如果这是重复的,请原谅。 我有一些非常古老的 C++ 代码,我正试图将它们轻松地转换为这个千年。代码仍然在 Visual Studio 6 中编译并且需要继续这样做,但
在我的 C++ 代码中,我不断地将不同的值写入文件。我的问题是,考虑到文件已成功打开这一事实,如果在任何情况下 write 或 << 会失败。我是否需要检查每次调用 write 或 << 以确保它已正
这个问题在这里已经有了答案: Calling a function overloaded in several namespaces from inside one namespace (3 个答案
我只是在写一些代码来吐出一个 wave header。我开始输入: file > 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com
我有一个派生自 std::ostream 的类(用于日志记录)。剥离下来,它看起来像这样: class bsgs : public std::ostream { public: bsgs(cons
我有以下代码: class A { public: ... C *func() { ... } void func2() { ... } ... }; class B
我在一个抽象类的头文件中找到了这个函数: virtual ostream & print( ostream & out ) const; 谁能告诉我这是什么函数以及如何在派生类中声明它?据我所知,它似
我想写一个函数输出一些东西到 ostream传入并返回流,如下所示: std::ostream& MyPrint(int val, std::ostream* out) { *out << val
偶尔我会写一个类(T 说)并尝试覆盖 std::ostream& operator<<(std::ostream&, const T&) 但它不适用于某些类。这是一个(简化的)类的示例,它对我不起作用
我一直在尝试创建一个程序来为一组已定义的进程实现实时调度算法。使用 g++ 编译时出现错误,其中指出: RTSprocess.h:在函数“std::ostream& operator #include
因此,我得到了一个带有起始代码的任务来实现一个链表(我已经成功地完成了一个未排序的双向链表)并且在给定头文件的起始代码中有一个友元声明似乎有允许我使用 cout 打印链表的目标陈述。这是头文件;请注意
我正在努力 cout using namespace std; ostream& Print(ostream& out) { out using namespace std; ostream&
为了序列化任何对象(即对于没有全局 ostream& operator std::ostream& operator std::string serialize_any(const T& val) {
我宁愿实现一个non-friend函数,直接将函数标记为virtual。 但我现在想确保一组特定的类实现重载 friend std::ostream& operator << (std::ostrea
我是一名优秀的程序员,十分优秀!