首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

大家傍晚好,c++ new 停止工作,该怎么处理

2012-04-07 
大家傍晚好,c++ new 停止工作C/C++ code#includeiostream#includefstream#includevector#includest

大家傍晚好,c++ new 停止工作

C/C++ code
#include<iostream>#include<fstream>#include<vector>#include<string>using namespace std;class Book{public:    Book():ISBN('\0'),name('\0'),price(0){};    void modify();    void show();    string ISBN;    string name;    float price;};void Book::modify(){    cin>>ISBN>>name>>price;}void Book::show(){    cout<<ISBN<<'\n'<<name<<'\n'<<price<<endl;}vector<Book>B;int Load(){    ifstream in;    int i=0;    in.open("bookdatabase.txt");    if (!in)    {    cerr <<"error: unable to open file"<< endl;    exit(0);    }    while(!in.eof())    {        Book *temp=new Book;        in.clear();        in>>temp->ISBN>>temp->name>>temp->price;        B.push_back(*temp);        i++;        delete temp;    }    return i;}int main(){    int i=0,j=0;    i=Load();    return 0;}


[解决办法]
为什么要用new呢,你在这儿只是一个局部变量而已。
[解决办法]
探讨
Book():ISBN('\0'),name('\0'),price(0){};
构造函数写错了。
Book(string ISBN = "", string name = "", float price = 0)
:ISBN(ISBN), name(name), price(price){}

[解决办法]
string的构造函数不接受字符类型。
代码应该改成这样
C/C++ code
class Book{public:    Book():ISBN("\0"),name("\0"),price(0){};//注意双引号    void modify();    void show();    string ISBN;    string name;    float price;}; 

热点排行