C++ C++;,如何从文件中读取数据并插入容器:映射
我在用信息填充容器地图时遇到问题。我用operator>>读取信息,但在我的二级CDealer中,它给了我这些错误<代码>没有运算符“>>”与这些操作数相匹配和C++ C++;,如何从文件中读取数据并插入容器:映射,c++,c++11,stl,containers,C++,C++11,Stl,Containers,我在用信息填充容器地图时遇到问题。我用operator>>读取信息,但在我的二级CDealer中,它给了我这些错误没有运算符“>>”与这些操作数相匹配和二进制“>>”:未找到接受“const CStock”类型的右操作数的运算符(或这些是不可接受的转换) 我有三个班:CStock、CDealer,他们有地图库存和CShop 如果有人能帮助我,我将非常感激 这是CStock中的my operators> ostream &operator << (ostream &
二进制“>>”:未找到接受“const CStock”类型的右操作数的运算符(或这些是不可接受的转换)
我有三个班:CStock、CDealer,他们有地图库存拥有矢量交易商的code>和CShop代码>
如果有人能帮助我,我将非常感激
这是CStock中的my operators>
ostream &operator << (ostream &toStream, const CStock &S){
toStream << "Name Stock: " << S.m_strName_stock << " Producer: " << S.m_strProducer <<
"Date: " << S.Date;
return toStream;
}
istream &operator >> (istream &fromStream, CStock &S)
{return fromStream >> S.m_strName_stock >> S.m_strProducer >> S.Date;}
ostream&operator>S.Date;}
这是CDealer中的my operators>
`
ostream&operator D.m_;
while(it!=D.Stock.end())
{fromStream>>it->first>>it->second.first>>it->second.second;}
从溪流返回
}
这是带参数的构造函数:文件名和这些tho运算符
CShop::CShop(常量字符串和fname)
{
CDealer c;
fstream文件(fname,ios::in);
if(File.is_open())
{
文件>>m_strNameShop;
文件>>m_rcity;
while(文件>>c)
{
经销商。推回(新CDealer(c));
}
File.close();
}
其他的
抛出“错误!”;
}
ostream&算子问题
问题可归结为以下情况:
#include <map>
int main(void) {
std::map<int, int> val;
auto it = val.begin();
it->first = 10;
}
我们有一个map
包含对
std::pair<const CStock, pair<unsigned, double>>
aconst CStock
并导致报告的错误
这一切都是有意义的,因为map
是按键排序的,因此如果您可以随时更改键,那么map
很快就会不连贯
解决方案
没有简单的解决办法。你不能这样做。您必须制作项目,然后将其放置在映射中
,或者使用emplace
函数族中的一个函数直接在映射中构建项目。一旦它们进入映射
,您就可以更改值,但不能更改键。您可以删除该项,从而删除密钥,然后使用其他密钥重新插入,但首先为什么要这样做?我建议重新思考您的逻辑。实现istream&operator>>(istream&fromStream,CDealer&D)
考虑不周全
该函数的目标是从输入流读取数据并充实CDealer
对象的内容。通常,操作符
功能是串联工作的,因此可以使用codecomperator>
读回使用操作符
编写的内容
从这个角度来看,即使是操作符也欢迎使用StackOverflow。请退房,并提供一张支票。实际上,这里有很多代码——其中大部分与您的问题完全无关。如果有一个简短的程序,直接指出问题,这会有所帮助。很好,但它给了我这个错误:严重性代码描述项目文件行抑制状态错误(活动)E0304没有重载函数的实例“std::map::insert[with _Kty=const CStock,_Ty=std::pair,_Pr=std::less,_Alloc=std::allocator]”符合论点list@Kristiyan,您必须为键
、val1
和val2
使用正确的类型。似乎键的类型是CStock
,val1
的类型是int
,val2
的类型是double
@R Sahu非常感谢!你太棒了!
#include"CDealer.h"
#include"CShop.h"
#include<iostream>
#include<string>
#include <stdlib.h>
#include<vector>
using namespace std;
int main()
{
CShop SS1("data.txt");
cout << SS1;
system("pause");
return 0;
}
#include <map>
int main(void) {
std::map<int, int> val;
auto it = val.begin();
it->first = 10;
}
map<CStock, pair<unsigned, double>>
std::pair<const CStock, pair<unsigned, double>>
fromStream >> it->first >> it->second.first >> it->second.second;
std::ostream& operator<<(std::ostream &toStream, CDealer cosnt& D)
{
// Write the name of the dealer, without any decorative text.
// Add a space character to the output so you can read the name back.
toStream << D.m_strName_Dealer << " ";
// Do the same for name of the agent and address.
toStream << D.m_strAgent << " ";
toStream << D.m_strAddress << " ";
// Write the number of items in Stock first.
// This will be necessary when reading the data.
toStream << D.Stock.size() << " ";
// Now write the Stock items, which spaces between each field you write.
map<CStock, pair<unsigned, double>>::const_iterator it = D.Stock.begin();
while (it != D.Stock.end())
{
toStream << it->first << " " << it->second.first << " " << it->second.second << " ";
}
return toStream;
}
std::istream &operator>>(std::istream &fromStream, CDealer &D)
{
// Read the name of the dealer.
fromStream >> D.m_strName_Dealer;
// Read the name of the agent.
fromStream >> D.m_strAgent;
// Read the address.
fromStream >> D.m_strAddress;
// Read the number of items.
size_t num;
fromStream >> num;
// Now read the Stock items.
for ( size_t i = 0; i < num; ++i )
{
// Obtained the types for key, val1, and val2 from
// the error message posted in a comment by OP.
CStock key;
int val1;
double val2;
fromStream >> key >> val1 >> valu2;
// Add the item to Stock.
D.Stock.insert(std::make_pair(key, std::make_pair(val1, val2)));
}
return fromStream
}