C++ 错误C2679:二进制'&燃气轮机&燃气轮机';:未找到接受类型为';的右操作数的运算符;常量字符[4]';(或没有可接受的转换) ;22
首先,对不起我的英语不好。英语不是我的主要语言,我的写作能力太差了 我找了一些关于这个问题的话题。我有一些类似的话题,但我找不到我的答案。[举例:,] 我只是发现问题是我不能在istream中使用像“+”这样的东西,因为它不是一个变量,所以我不能使用它。但我想以如下形式输入我的类值:a+bj,这样我就可以用fstream将其导出到txt文件中。现在我不知道怎么做 我的代码:C++ 错误C2679:二进制'&燃气轮机&燃气轮机';:未找到接受类型为';的右操作数的运算符;常量字符[4]';(或没有可接受的转换) ;22,c++,C++,首先,对不起我的英语不好。英语不是我的主要语言,我的写作能力太差了 我找了一些关于这个问题的话题。我有一些类似的话题,但我找不到我的答案。[举例:,] 我只是发现问题是我不能在istream中使用像“+”这样的东西,因为它不是一个变量,所以我不能使用它。但我想以如下形式输入我的类值:a+bj,这样我就可以用fstream将其导出到txt文件中。现在我不知道怎么做 我的代码: #include "stdafx.h" #include "iostream" #include "fstream" us
#include "stdafx.h"
#include "iostream"
#include "fstream"
using namespace std;
class Complex
{
public:
Complex( double = 0.0, double = 0.0);
Complex(const Complex &);
Complex(const Complex *);
Complex operator+( const Complex & ) const;
Complex operator-( const Complex & ) const;
Complex operator*( const Complex & ) const;
Complex operator/( const Complex & ) const;
Complex operator=( const Complex & );
friend istream &operator>>(istream & in, const Complex & Com){
in >> Com.real >> " + " >> Com.imaginary >> "j\n"; //Error occur here
return in;
}
friend ostream &operator<<(ostream & out, const Complex & Com){
out << Com.real << " + " << Com.imaginary << "j\n";
return out;
}
void print() const;
private:
double real;
double imaginary;
};
#包括“stdafx.h”
#包括“iostream”
#包括“fstream”
使用名称空间std;
阶级情结
{
公众:
复合(双=0.0,双=0.0);
复杂(常数复杂&);
复合物(常数复合物*);
复数运算符+(常数复数&)常数;
复算子-(const Complex&)const;
复数运算符*(常数复数&)常数;
复数运算符/(常数复数&)常数;
复数运算符=(常数复数&);
friend istream&operator>>(istream&in、const Complex&Com){
在>>Com.real>>“+”>>Com.virtual>>“j\n”;//此处发生错误
返回;
}
friend ostream&operator为什么要重新发明轮子?标准库已经提供了
和流支持
#include <iostream>
#include <complex>
int main()
{
std::complex<double> c;
std::cin >> c;
std::cout << c;
}
#包括
#包括
int main()
{
std::复合物c;
标准:cin>>c;
std::cout为什么要重新发明轮子?标准库已经提供了
和流支持
#include <iostream>
#include <complex>
int main()
{
std::complex<double> c;
std::cin >> c;
std::cout << c;
}
#包括
#包括
int main()
{
std::复合物c;
标准:cin>>c;
std::cout接受字符串文本的>
操作符没有重载(字符串文本是字符的常量数组,例如对于3个字符的字符串文本,常量字符[4]
),因此不能使用这样的模式匹配
如果您想进行模式匹配,那么您可以使用到特定的。不存在接受字符串文本的>
操作符重载(字符串文本是字符的常量数组,例如对于3个字符的字符串文本,常量字符[4]
),因此不能像那样使用模式匹配
如果您想进行模式匹配,那么您可以使用特定的。从std::istream
格式提取与C的sscanf
不同,因为您不能指定文本的文本片段进行“匹配”或“跳过”。您只能将数据提取到变量中,并在事后对其进行验证
因此,std::cin>>“+”
和std::cin>>“j\n”
完全无效
你可以让它工作,不过
#include <iostream>
#include <cstring>
#include <stdexcept>
struct eat
{
explicit eat(const char* in)
: str(in)
, len(std::strlen(in))
{}
template <size_t N>
explicit eat(const char (&in)[N])
: str(in)
, len(N)
{}
private:
const char* str;
size_t len;
friend std::istream& operator>>(std::istream&, eat);
};
std::istream& operator>>(std::istream& is, eat e)
{
char c;
for (size_t i = 0; i < e.len; i++) {
if (!is.get(c) || c != e.str[i]) {
is.setstate(std::ios::failbit);
break;
}
}
return is;
}
int main()
{
int x = 0, y = 0;
std::cin >> x >> eat(" + ") >> y >> eat("j");
if (!std::cin)
throw std::runtime_error("Input was invalid!");
std::cout << "Real part: " << x << "; imaginary part: " << y << '\n';
}
// Input: 3 + 4j
// Output: Real part: 3; imaginary part: 4
#包括
#包括
#包括
结构吃
{
显式eat(常量字符*in)
:str(in)
,len(std::strlen(in))
{}
模板
显式eat(常量字符(&in)[N])
:str(in)
,len(N)
{}
私人:
常量字符*str;
尺寸透镜;
friend std::istream&operator>>(std::istream&eat);
};
std::istream&operator>>(std::istream&is,eat e)
{
字符c;
对于(大小i=0;i>x>>吃(“+”>>y>>吃(“j”);
如果(!std::cin)
抛出std::runtime_错误(“输入无效!”);
std::cout从std::istream
中进行格式化提取与C的sscanf
不同,因为您不能指定文本的文本片段进行“匹配”或“跳过”。您只能将数据提取到变量中,并在事后对其进行验证
因此,std::cin>>“+”
和std::cin>>“j\n”
完全无效
你可以让它工作,不过
#include <iostream>
#include <cstring>
#include <stdexcept>
struct eat
{
explicit eat(const char* in)
: str(in)
, len(std::strlen(in))
{}
template <size_t N>
explicit eat(const char (&in)[N])
: str(in)
, len(N)
{}
private:
const char* str;
size_t len;
friend std::istream& operator>>(std::istream&, eat);
};
std::istream& operator>>(std::istream& is, eat e)
{
char c;
for (size_t i = 0; i < e.len; i++) {
if (!is.get(c) || c != e.str[i]) {
is.setstate(std::ios::failbit);
break;
}
}
return is;
}
int main()
{
int x = 0, y = 0;
std::cin >> x >> eat(" + ") >> y >> eat("j");
if (!std::cin)
throw std::runtime_error("Input was invalid!");
std::cout << "Real part: " << x << "; imaginary part: " << y << '\n';
}
// Input: 3 + 4j
// Output: Real part: 3; imaginary part: 4
#包括
#包括
#包括
结构吃
{
显式eat(常量字符*in)
:str(in)
,len(std::strlen(in))
{}
模板
显式eat(常量字符(&in)[N])
:str(in)
,len(N)
{}
私人:
常量字符*str;
尺寸透镜;
friend std::istream&operator>>(std::istream&eat);
};
std::istream&operator>>(std::istream&is,eat e)
{
字符c;
对于(大小i=0;i>x>>吃(“+”>>y>>吃(“j”);
如果(!std::cin)
抛出std::runtime_错误(“输入无效!”);
std::cout错误是因为“+”不能作为istream
如果数据是使用cout写出的,那么错误是因为“+”不能作为istream
如果使用<代码> CUT> P>编写数据,所有现有的答案都是好的;我将添加另一种(最简单的)方法来做同样的事情。您可以想象“读”和“写”应该有大致相同的实现,只改变数据流的“方向”。不幸的是,这不能在C++中工作。
但是,这将在C语言中使用
printf
和scanf
:
// Hypothetical "print" function; you don't need to use it
void print(FILE* out, const Complex & Com){
fprintf(out, "%f + %fj\n", com.real, com.imaginary);
}
如果将printf
替换为scanf
,则会得到如下结果:
void read(FILE* in, Complex & Com){
fscanf(in, "%lf + %lfj\n", &com.real, &com.imaginary);
}
然而,这方面存在一些问题:
- 这对你学习C语言毫无帮助++
- 检测错误并非小事
- 它使用
文件*
,而不是istream
可以修复第三个缺陷-使用getline
读取输入行,使用sscanf
解析输入行:
friend istream &operator>>(istream & in, const Complex & Com){
std::string str;
std::getline(in, str);
sscanf(str.c_str(), "%lf + %lfj\n", &com.real, &com.imaginary);
return in;
}
所有现有的答案都是好的;我将添加另一种(最简单的?)方法来做同样的事情