求给位帮忙看看这图书类 哪儿错了...
#include "String.h"
class book
{
public:
book();
book(char*,char*,char*,float);
const String& GetTitle()const{return title;}
const String& GetAuthor()const{return author;}
const String& GetNumber()const{return number;}
float GetPrice()const{return price;}
void SetTitle(const string&Stitle)
{title=Stitle;}
void SetAuthor(const string&SAuthor)
{author=Sauthor;}
void SetNumber(const string&Snumber)
{number=Snumber;}
void SetPrice(const string&Sprice)
{price=Sprice;}
void setTotal(const string&T,const String&N,const String&A,float P)
{
title=T;
author=A;
number=N;
price=P;
}
private:
String title;
String author;
String number;
float price;
};
book::book():title(""),author(""),number(""),price(0)
{
}
book::book(char*Title,char*Author,char*Number,float Price):title(Title),author(Author),number(Number),price(Price)
int main()
{
book love("love","Jacky","001",34.1f);
cout<<"book name: "<<love.GetTitle()<<"book author: "<<love.GetAuthor()<<"book nunmber: "<<love.GetNumber()<<"book price: "<<love.Price()<<endl;
return 0;
}
运行错误:
D:\VC++\faith\MyProjects\3\3.cpp(239) : error C2143: syntax error : missing ';' before '&'
D:\VC++\faith\MyProjects\3\3.cpp(239) : error C2501: 'GetTitle' : missing storage-class or type specifiers
D:\VC++\faith\MyProjects\3\3.cpp(239) : warning C4183: 'GetTitle': member function definition looks like a ctor, but name does not match enclosing class
D:\VC++\faith\MyProjects\3\3.cpp(241) : error C2143: syntax error : missing ';' before '&'
D:\VC++\faith\MyProjects\3\3.cpp(241) : error C2734: 'String' : const object must be initialized if not extern
D:\VC++\faith\MyProjects\3\3.cpp(241) : error C2086: 'String' : redefinition
D:\VC++\faith\MyProjects\3\3.cpp(241) : error C2501: 'GetAuthor' : missing storage-class or type specifiers
D:\VC++\faith\MyProjects\3\3.cpp(241) : warning C4183: 'GetAuthor': member function definition looks like a ctor, but name does not match enclosing class
D:\VC++\faith\MyProjects\3\3.cpp(243) : error C2143: syntax error : missing ';' before '&'
D:\VC++\faith\MyProjects\3\3.cpp(243) : error C2734: 'String' : const object must be initialized if not extern
D:\VC++\faith\MyProjects\3\3.cpp(243) : error C2086: 'String' : redefinition
D:\VC++\faith\MyProjects\3\3.cpp(243) : error C2501: 'GetNumber' : missing storage-class or type specifiers
D:\VC++\faith\MyProjects\3\3.cpp(243) : warning C4183: 'GetNumber': member function definition looks like a ctor, but name does not match enclosing class
D:\VC++\faith\MyProjects\3\3.cpp(247) : error C2143: syntax error : missing ',' before '&'
D:\VC++\faith\MyProjects\3\3.cpp(247) : error C2059: syntax error : '&'
D:\VC++\faith\MyProjects\3\3.cpp(250) : error C2143: syntax error : missing ',' before '&'
D:\VC++\faith\MyProjects\3\3.cpp(250) : error C2059: syntax error : '&'
D:\VC++\faith\MyProjects\3\3.cpp(253) : error C2143: syntax error : missing ',' before '&'
D:\VC++\faith\MyProjects\3\3.cpp(253) : error C2059: syntax error : '&'
D:\VC++\faith\MyProjects\3\3.cpp(256) : error C2143: syntax error : missing ',' before '&'
D:\VC++\faith\MyProjects\3\3.cpp(256) : error C2059: syntax error : '&'
D:\VC++\faith\MyProjects\3\3.cpp(259) : error C2143: syntax error : missing ',' before '&'
D:\VC++\faith\MyProjects\3\3.cpp(259) : error C2059: syntax error : '&'
D:\VC++\faith\MyProjects\3\3.cpp(267) : error C2146: syntax error : missing ';' before identifier 'title'
D:\VC++\faith\MyProjects\3\3.cpp(267) : error C2501: 'String' : missing storage-class or type specifiers
D:\VC++\faith\MyProjects\3\3.cpp(267) : error C2501: 'title' : missing storage-class or type specifiers
D:\VC++\faith\MyProjects\3\3.cpp(268) : error C2146: syntax error : missing ';' before identifier 'author'
D:\VC++\faith\MyProjects\3\3.cpp(268) : error C2501: 'String' : missing storage-class or type specifiers
D:\VC++\faith\MyProjects\3\3.cpp(268) : error C2501: 'author' : missing storage-class or type specifiers
D:\VC++\faith\MyProjects\3\3.cpp(269) : error C2146: syntax error : missing ';' before identifier 'number'
D:\VC++\faith\MyProjects\3\3.cpp(269) : error C2501: 'String' : missing storage-class or type specifiers
D:\VC++\faith\MyProjects\3\3.cpp(269) : error C2501: 'number' : missing storage-class or type specifiers
D:\VC++\faith\MyProjects\3\3.cpp(274) : error C2758: 'String' : must be initialized in constructor base/member initializer list
D:\VC++\faith\MyProjects\3\3.cpp(239) : see declaration of 'String'
D:\VC++\faith\MyProjects\3\3.cpp(274) : error C2614: 'book' : illegal member initialization: 'number' is not a base or member
D:\VC++\faith\MyProjects\3\3.cpp(274) : error C2614: 'book' : illegal member initialization: 'author' is not a base or member
D:\VC++\faith\MyProjects\3\3.cpp(274) : error C2614: 'book' : illegal member initialization: 'title' is not a base or member
D:\VC++\faith\MyProjects\3\3.cpp(279) : error C2612: trailing 'division operator' illegal in base/member initializer list
D:\VC++\faith\MyProjects\3\3.cpp(279) : error C2758: 'String' : must be initialized in constructor base/member initializer list
D:\VC++\faith\MyProjects\3\3.cpp(239) : see declaration of 'String'
D:\VC++\faith\MyProjects\3\3.cpp(279) : error C2614: 'book' : illegal member initialization: 'number' is not a base or member
D:\VC++\faith\MyProjects\3\3.cpp(279) : error C2614: 'book' : illegal member initialization: 'author' is not a base or member
D:\VC++\faith\MyProjects\3\3.cpp(279) : error C2614: 'book' : illegal member initialization: 'title' is not a base or member
D:\VC++\faith\MyProjects\3\3.cpp(280) : error C2601: 'main' : local function definitions are illegal
D:\VC++\faith\MyProjects\3\3.cpp(286) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
3.obj - 40 error(s), 3 warning(s)
[解决办法]
什么错误?String.h是什么文件嘛?自己定义的么?你用的是什么编译器?
[解决办法]
- C/C++ code
#include <string>#include <iostream>using namespace std;class book{public: book(); book(char*,char*,char*,float); const string& GetTitle()const{return title;} const string& GetAuthor()const{return author;} const string& GetNumber()const{return number;} float GetPrice()const{return price;} void SetTitle(const string& Stitle) {title=Stitle;} void SetAuthor(const string& SAuthor) {author=SAuthor;} void SetNumber(const string& Snumber) {number=Snumber;} void SetPrice(const float& Sprice) {price=Sprice;} void setTotal(const string& T,const string& N,const string& A,float P) { title=T; author=A; number=N; price=P; }private: string title; string author; string number; float price;};book::book():title(""),author(""),number(""),price(0){}book::book(char*Title,char*Author,char*Number,float Price):title(Title),author(Author),number(Number),price(Price){}int main(){ book love("love","Jacky","001",34.1f); cout<<"book name: "<<love.GetTitle()<<"book author: "<<love.GetAuthor()<<"book nunmber: "<<love.GetNumber()<<"book price: "<<love.GetPrice()<<endl; return 0;}
