C++流运算符的问题
C++ Issue with Stream Operators
我对 Cpp 很陌生,在声明输入流时遇到了一些问题。我不断收到一个错误,"错误:与'std::cin>> new_contact中的'运算符>>不匹配。Person::Name',以及一大堆其他事情,这些事情都与滥用它看起来像>>运算符有关。下面是代码,以及正在使用的每个结构的结构。
#include <iostream>
#include <map>
#include <string>
#include <vector>
using std::string;
using std::vector;
using std::endl;
using std::map;
using std::cout;
using std::cin;
typedef struct {
string Name;
map<string,int> Number;
} Person;
void addContact(vector<Person> Contacts) {
Person new_contact;
char ans;
string name;
cout << "What is the name of your new contact (first name)? " << endl;
cin >> new_contact.Name;
cout << "Do you want to enter a number for your contact? " << endl;
cin >> ans;
while (ans == 'y') {
cout << "What type of number are you entering? " << endl; //ex CELL, FAX, HOME, WORK, etc.
cin >> name;
cout << "What is the number of your contact? " << endl;
cin >> new_contact.Number[name];
cout << "do you want to enter another number? " << endl;
cin >> ans;
}
Contacts.push_back(new_contact);
}
请让我知道我哪里出了问题,因为在将我遇到的错误与人们遇到的以前的错误进行比较时,我没有看到任何明显的问题(大多数遇到此问题的人似乎将 endl 命令放在 cin 流的末尾,但是,我没有这样做(。提前感谢!
编辑:现在,我在尝试重新检查"answers"变量时陷入无限循环。该函数只是无限循环交替的"你想输入另一个数字吗?"和"你输入什么类型的数字?",而我没有输入。
cin >> new_contact.Name
需要重载operator>>
才能vector<char>
,而 不存在。
最简单的解决方法是更改Name
成员的类型:
string Name;
相关文章:
- 一个关于在C++中重载布尔运算符的问题
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- 运算符继承和 cpp 核心准则 c.128 的问题
- 在C++中使用重载提取运算符时出现问题
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- 关于条件块的问题与&&运算符有关
- 重载运算符与添加问题
- 加、乘、除、减复数的问题 C++ - 运算符重载
- 我需要这方面的帮助C++在使用逻辑运算符时是/否问题
- 0xC0000005:访问冲突读取位置 0x00000000. 重载 == 运算符的问题
- 如何解决不明确的运算符过载问题?
- 屏幕插入运算符<<的运算符过载问题
- 如何通过使用 2 位或更多数字的 XOR 运算符来执行此操作C++问题
- 在 C++ 的自定义运算符中删除与删除[](不同于常见的删除与删除[]问题)
- 用户定义的文本运算符(在原始模式下)存在问题
- 关于重载 -> 运算符中 const 关键字的特定位置的问题
- 字符 * 未从重载运算符或内存管理问题正确返回
- constexpr 运算符重载使用参数的问题
- c++中重载输入运算符的问题
- 有趣的问题运算符重载