在 c++ 中使用字符串时出错。error C2679:二进制'<<':未定义采用类型为"类"的右操作数的运算符
Error when using strings in c++.error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class
我第一次使用不同的函数创建C++程序,但每当我尝试使用字符串时,就会发生这种情况!
我只发布相关部分。
#include<string>
#include<iostream.h>
#include<math.h>
#include<cctype>
using namespace std;
..... (Skipping irrelevant parts)
<Variables go here.>
..... (Skipping irrelevant parts)
if (tolower(from) == 'a')
{
unit = "Seconds";
secondsto();
}
else if (tolower(from) == 'b')
{
unit = "Minutes";
minutesto();
}
else if (tolower(from) == 'c')
{
unit = "Hours";
hoursto();
}
else if (tolower(from) == 'd')
{
unit = "Days";
daysto();
}
/* else if (tolower(from) == 'e')
{
unit = "Weeks";
weeksto();
}*/
else if (tolower(from) == 'f')
{
unit = "Months";
monthsto();
}
else if (tolower(from) == 'g')
{
unit = "Years";
yearsto();
}
..... (Skipping irrelevant parts)
if (tolower(from) == 'a')
{
unit = "Seconds";
secondsto();
}
else if (tolower(from) == 'b')
{
unit = "Minutes";
minutesto();
}
else if (tolower(from) == 'c')
{
unit = "Hours";
hoursto();
}
else if (tolower(from) == 'd')
{
unit = "Days";
daysto();
}
/* else if (tolower(from) == 'e')
{
unit = "Weeks";
weeksto();
}*/
else if (tolower(from) == 'f')
{
unit = "Months";
monthsto();
}
else if (tolower(from) == 'g')
{
unit = "Years";
yearsto();
}
...... (Skipping)
cout << original_value << " " << unit << " --> " << value << " " << new_unit;
错误正是
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no a
cceptable conversion)
我几乎尝试了我能找到的所有其他解决方案,但都没有成功。
不要在程序中包含不推荐使用的iostream.h
,而是包含<iostream>
。此外,switch
将比级联if/else
:更可读,并消除代码重复
switch ( tolower(from) ) {
case 'a' :
unit = "Seconds";
secondsto();
break;
case 'b' :
unit = "Minutes";
minutesto();
break;
...
}
您还可以消除对tolower()
函数的多次调用。
您需要更换
#include <iostream.h>
带有
#include <iostream>
你也应该尽量避免
using namespace std;
并使用
using std::cout;
或
std::cout << original_value << " " << unit << " --> " << value << " " << new_unit;
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- std::pair的默认构造函数<>将基本类型(int等)设置为零