c++ - 从 C++ 文件中读取和写入复数(从 C 翻译)?
问题描述
我有一个大问题。我得到一个任务,我应该用 C++ 编写一个程序,我以前用 C 编写过这个程序。这个 C++ 程序必须从文件中读取复数并写入它们。我必须创建一个类,实部和虚部必须是私有的。我还必须创建运算符 << 和 >>。我不知道我应该怎么做。这是我的输入文件 data.txt
(2.3,5) (7.12,-2.91) (7.99,8)
(5,3.25)
我的输出文件应该是 result.txt
2.3, 5
7.12 -2.91
在 CI 中使用这些函数
void readZ(FILE *wp, double *a, double *b)
{
char c;
assert(fscanf(wp,"%c%lg%c%lg%c%c",&c,a,&c,b,&c,&c));
}
int writeZ(FILE *wp, double complex z)
{
fprintf(wp, "%.2f, %.2f\n", creal(z), cimag(z));
return 1;
}
现在我必须在 C++ 中执行此操作这是我的代码 main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "read.h"
using namespace std;
int main(int argc, char* argv[])
{
ifstream inFile;
ostream outFile;
char c;
ifstream read(argv[1]);
if (!read)
{ cerr << "Open error: " << argv[1] << endl; exit(1);}
ofstream write(argv[2]);
if(!write) { cerr << "Open error: " << argv[2] << endl; exit(2);}
while(read.get(c))
write.put(c);
Complex_num x1;
read >> x1;
write << x1;
inFile.close();
outFile.close();
return 0;
}
读.h
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
class Complex_num {
double real, imag;
public:
Complex_num(){
real=0;
imag=0;
}
friend std::ostream& operator<<(std::ostream&, const Complex_num&)
{
cout << real << '.' << imag << ',' << ' ';
}
friend std::istream& operator>>(std::istream&, Complex_num&){
char c;
cin>>c>>&real>>c>>&imag>>c>>c;
}
};
读取.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "read.h"
using namespace std;
Complex_num::Complex_num(double r, double i)
{
real=r;
imag=i;
}
我有很多错误,我什至不知道我应该做什么
g++ -c read.cpp -Wall
In file included from read.cpp:4:0:
read.h: In function ‘std::ostream& operator<<(std::ostream&, const Complex_num&)’:
read.h:18:17: error: invalid use of non-static data member ‘Complex_num::real’
cout << real << '.' << imag << ',' << ' ';
^~~~
read.h:8:12: note: declared here
double real, imag;
^~~~
read.h:18:32: error: invalid use of non-static data member ‘Complex_num::imag’
cout << real << '.' << imag << ',' << ' ';
^~~~
read.h:8:18: note: declared here
double real, imag;
^~~~
read.h:19:5: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
read.h: In function ‘std::istream& operator>>(std::istream&, Complex_num&)’:
read.h:22:18: error: invalid use of non-static data member ‘Complex_num::real’
cin>>c>>&real>>c>>&imag>>c>>c;
^~~~
read.h:8:12: note: declared here
double real, imag;
^~~~
read.h:22:28: error: invalid use of non-static data member ‘Complex_num::imag’
cin>>c>>&real>>c>>&imag>>c>>c;
^~~~
read.h:8:18: note: declared here
double real, imag;
^~~~
read.h:23:5: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
read.cpp: At global scope:
read.cpp:8:1: error: prototype for ‘Complex_num::Complex_num(double, double)’ does not match any in class ‘Complex_num’
Complex_num::Complex_num(double r, double i)
^~~~~~~~~~~
In file included from read.cpp:4:0:
read.h:7:7: error: candidates are: constexpr Complex_num::Complex_num(Complex_num&&)
class Complex_num {
^~~~~~~~~~~
read.h:7:7: error: constexpr Complex_num::Complex_num(const Complex_num&)
read.h:10:5: error: Complex_num::Complex_num()
Complex_num(){
^~~~~~~~~~~
解决方案
read.h:18:17: error: invalid use of non-static data member ‘Complex_num::real’
cout << real << '.' << imag << ',' << ' ';
您的朋友运算符函数不是成员函数,因此它们无权访问成员变量。您应该使用它们的参数:
friend std::ostream& operator<<(std::ostream& os, const Complex_num& c)
{
os << c.real << '.' << c.imag << ',' << ' ';
return os;
}
friend std::istream& operator>>(std::istream& is, Complex_num& cn){
char c;
is>>c>>cn.real>>c>>cn.imag>>c>>c;
return is;
}
推荐阅读
- python-2.7 - multiprocessing.Manager().Queue() TypeError: can't pickle thread.lock objects
- wcf - WCF 服务调用错误消息无法为具有权限的 SSL/TLS 建立安全通道
- javascript - 如何避免嵌套映射以从嵌套数组中获取我需要的值?
- javascript - 我正在尝试使“更多”按钮显示附加信息而不加载整个页面
- java - 为什么在我的代码中无法识别滑动?
- elasticsearch - “匹配”查询以及“应该”子句在 Elasticsearch 中提供超过所需的匹配结果
- c# - AWS HTTP Post 示例的签名密钥应该是什么?
- android - 如何使用 Google API SettingsClient 禁用设备位置?
- c# - C# .NET 中的快速数据操作:RRGGBB 到位图/C# 视频/图像处理
- java - Spring Boot HttpServletResponse.sendRedirect() 在 iOS 上不起作用