C++的error C2679

来源:百度知道 编辑:UC知道 时间:2024/06/15 11:30:22
#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指令引用的0x004

我给你改了下代码,尽量不要再结构体内定义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