C++的error C2679

#include<iostream>
#include<fstream>
using namespace std;
struct Date //日期
{int year; int moon; int day;};

struct commodity //定义商品结构体
{string name; float number; string place; float price;
int amount; int sold; Date date;};

const n=100;
struct commodity goods[n];

int main()
{ int i;
ifstream infile("supermarket.dat",ios::in);
for(i=0;i<n;i++) infile>>goods[i].place;
infile.close();
return 0;}

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 acceptable conversion)

那个错误是什么意思啊?“infile>>goods[i].place”这一句改成“infile>>goods[i].number”就没事的,为什么?
最主要的是:如何解决这个问题?

按照你的做法,为什么运行的时候会出现“应用程序错误。 0x00401efa指令引用的0x00472044内存。该内存不能为 written.?

我给你改了下代码,尽量不要再结构体内定义string型的变量,虽然它的功能很强大,因为有些编译器是通不过的,我给你一个通用的,所有编译器都可以通过(改的地方都注释了)
// 12121.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<fstream>

const int N = 50;//这里定义一个字符数组的最大可容纳的字符数
using namespace std;
struct Date //日期
{int year; int moon; int day;};

struct commodity //定义商品结构体
{char name[N]; float number; char place[N]; float price;
int amount; int sold; Date date;};

const int n=100;//注意你的那里的const的用法,你那用错了
struct commodity goods[n];

int main()
{
int i = 0;
int count = 0;
ifstream infile("wuqianhu.txt");//以读的方式打开文件

if(!infile.is_open())//对文件是否打开进行判断
{
printf("Can't open the file!\n");
exit(-1);
}

while(infile.good ())//这里的函数可以判断一切非法输入,而且还可以判断是否到了文件尾部,切记
{
++count;//记录总共提取了多少个结构体,初始化为0
infile>>goods[i].name;//循环里的代码表示一下读出一个结构体,因为各个属性的类型不一样
infile>>goods[i].number;
infile>>goods[i].place;
infile>>goods[i].price;
infile>>goods[i].amount;
infile>>goods[i].sold;
infile>>goods[i].date.year;
infile>>goods[i].date.moon;
infile>>goods[i].date.day;
i++;//这里是让循环持续下去
}

for(i = 0; i < count;i++)//这个循环显示提取的内容
{
cout << goods[i].name << " ";
cout << goods[i].number << " ";
cout << goods[i].place << " ";
cout << goods[i].price << " ";
cout << goods[i].amount << " ";
cout << goods[i].sold << " ";
cout << goods[i].date.year << " ";
cout << goods[i].date.moon << " ";
cout << goods[i].date.day << " ";
cout << endl;
}

infile.close();

getchar();//这里用提示输入一个字符,否则看不到结果控制台对话框就小时了
return 0;
}

你的错误:
这个是典型的匹配问题,本来infile是由istream派生出来的,它和cin一样可以直接用>>,但是它主要是针对文件的操作,而且它是以空格为分界符的(这一点记住最重要),但是你的是一个结构体,里面类型都不一样,所以一次要取一个结构体,只有一个属性一个属性的提取出来,就是代码里的第一个for循环,再者你的文件里面的内容要格式一致,我给了你一个样板:wuqianhu.txt这是里面的内容:
tang 78.7 shanghai 90.4 67 34 2009 11 23 yang 89.3 jingzhou 90.2 23 342 2009 3 13
我这里运行是对的,对文件的有些函数要很清楚,祝楼主好运~~
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-08-12
goods[i].place是string型变量,不能直接用>>输入的,你得重载>>运算符才行

#include<iostream>
#include<fstream>
using namespace std;
struct Date //日期
{int year; int moon; int day;};

struct commodity //定义商品结构体
{string name; float number; string place; float price;
int amount; int sold; Date date;
};

const n=100;
struct commodity goods[n];

int main()
{
int i;
ifstream infile("supermarket.dat",ios::in);
for(i=0;i<n;i++)
{
char *s=(char *)(goods[i].place).c_str();
//这里转换string到char*
infile.getline(s,100,'\n');
//这里读入字符串,结束符我定义成回车('\n'),你可以直接改。
}
infile.close();
return 0;
}
第2个回答  2009-08-13
成员变量的问题
相似回答