短短代码 一大堆问题啊
#include <iostream>
#include <fstream>
#include <string>
//#include <vector>
//#include <iosfwd>
using namespace std;
class BookMessage{
static int storage;
static int capacity;
string bookName;
int bookNum;
public:
BookMessage(string name,int num):bookName(name),bookNum(num){
storage=0;
capacity=0;
storage++;
capacity++;
}
~BookMessage(){
}
friend ostream& operator < <(ostream&,BookMessage&);
};
ofstream& operator < <(ofstream& os,const BookMessage& bkm){
os < <bkm.bookName < < " " < <bkm.bookNum < <endl;
return os;
}
void main(){
ofstream bookDate( "book.dat "),iso_base::app);
BookMessage book1( "history ",1001);
BookMessage book2( "maths ",1242);
BookMessage book3( "chemistry ",1239);
bookDate < <book1 < <book2 < <book3;
}
G:\CODE\homework\test1\csf.cpp(27) : error C2248: 'bookName ' : cannot access private member declared in class 'BookMessage '
G:\CODE\homework\test1\csf.cpp(12) : see declaration of 'bookName '
G:\CODE\homework\test1\csf.cpp(27) : error C2248: 'bookNum ' : cannot access private member declared in class 'BookMessage '
G:\CODE\homework\test1\csf.cpp(13) : see declaration of 'bookNum '
G:\CODE\homework\test1\csf.cpp(32) : error C2653: 'iso_base ' : is not a class or namespace name
G:\CODE\homework\test1\csf.cpp(32) : error C2065: 'app ' : undeclared identifier
c++ primer 说 打开写入的附加模式用 ios_base::app
但系统提示找不到
还有 把所有 private 改成 public 后
去掉 ios_base::app
链接 又会出现一对错误 帮忙看看
[解决办法]
#include <iostream>
#include <fstream>
#include <string>
//#include <vector>
//#include <iosfwd>
using namespace std;
class BookMessage{
int storage;//不要static
int capacity;
string bookName;
int bookNum;
public:
BookMessage(string name,int num):bookName(name),bookNum(num){
storage=0;
capacity=0;
storage++;
capacity++;
}
~BookMessage(){
}
friend// ostream& operator < <(ostream&,BookMessage&);
ofstream& operator < <(ofstream& os,const BookMessage& bkm){
os < <bkm.bookName < < " " < <bkm.bookNum < <endl;
return os;
}
};
int main(){
ofstream bookDate( "book.dat ",ios_base::app);//ios_base,不是iso_app,注意括号匹配
BookMessage book1( "history ",1001);
BookMessage book2( "maths ",1242);
BookMessage book3( "chemistry ",1239);
bookDate < <book1 < <book2 < <book3;
return 0;
}
[解决办法]
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/test1.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
test1.exe - 2 error(s), 0 warning(s)
VC6 编译出现了写错误
//----------------------
你工程是Application 吧...?
是console 程序..
换成console 建工程
[解决办法]
#include <iostream>
#include <fstream>
#include <string>
//#include <vector>
//#include <iosfwd>
using namespace std;
class BookMessage {
public:
BookMessage(string name, int num) :
bookName(name), bookNum(num) {
//storage=0;
//capacity=0;
storage++;
capacity++;
}
~BookMessage() {
}
friend ofstream& operator < <(ofstream&, const BookMessage&);
private:
string bookName;
int bookNum;
static int storage;
static int capacity;
};
int BookMessage::storage = 0;
int BookMessage::capacity = 0;
ofstream& operator < <(ofstream& os, const BookMessage& bkm) {
os < < bkm.bookName < < " " < < bkm.bookNum < < endl;
return os;
}
int main() {
ofstream bookDate( "book.dat ", ios_base::app);
BookMessage book1( "history ", 1001);
BookMessage book2( "maths ", 1242);
BookMessage book3( "chemistry ", 1239);
bookDate < < book1 < < book2 < < book3;
}