大家傍晚好,c++ new 停止工作,该怎么处理
大家傍晚好,c++ new 停止工作
------解决方案--------------------
为什么要用new呢,你在这儿只是一个局部变量而已。
------解决方案--------------------
------解决方案--------------------
string的构造函数不接受字符类型。
代码应该改成这样
- 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呢,你在这儿只是一个局部变量而已。
------解决方案--------------------
------解决方案--------------------
string的构造函数不接受字符类型。
代码应该改成这样
- C/C++ code
class Book { public: Book():ISBN("\0"),name("\0"),price(0){};//注意双引号 void modify(); void show(); string ISBN; string name; float price; };