前辈们 指点一下
笑话 2012-10-16 11:12:08 各位前辈 程序错误是:
binary '<<' : no operator defined which takes a right-hand operand of type 'const class String' (or there is no acceptable conversion)
我苦看了一天了 快崩溃了 求救!谢谢啦!
出错处是:主函数的第二行那个<<;
这是 book.cpp:
# 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(float&Sprice){price = Sprice;}
void SetTotal(const String&T, const String&A, const String&N, float P)
{
title = T;
number = N;
author = A;
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<<"书名:"<<love.GetTitle()<<"作者:"<<love.GetAuthor()<<"编号:"<<love.GetNumber()<<"价格:"<<love.GetPrice()<<endl;
love.SetTotal("hate", "Rose", "002", 56.7f);
cout<<"书名:"<<love.GetTitle()<<"作者:"<<love.GetAuthor()<<"编号:"<<love.GetNumber()<<"价格:"<<love.GetPrice()<<endl;
book hate;
hate = love;
cout<<"书名:"<<hate.GetTitle()<<"作者:"<<hate.GetAuthor()<<"编号:"<<hate.GetNumber()<<"价格:"<<hate.GetPrice()<<endl;
if (love.GetNumber()==hate.GetNumber())
cout<<"两本书的编号相同."<<endl;
else
cout<<"两本书的编号不同."<<endl;
hate.SetNumber("003");
if (love.GetNumber()==hate.GetNumber())
cout<<"两本书的编号相同."<<endl;
else
cout<<"两本书的编号不同."<<endl;
String LoveAddHate = love.GetAuthor() + hate.GetAuthor();
book LoveHate;
LoveHate.SetAuthor(LoveAndHate);
LoveHate.SetTitle("新书");
cout<<LoveHate.GetTitle()<<"的作者:"<<LoveHate.GetAuthor()<<endl;
return 0;
}
这是String.h:
# include <iostream>
using namespace std;
class String
{
public:
String();
~String(){delete []str; len = 0;};
String(const char *const ch);
int getlen()const{return len;}
friend istream&operator>>(istream&o, String&str)
{
o>>str.str;
return o;
}
friend ostream&operator<<(ostream&o, String&str)
{
o<<str.str;
return o;
}
friend bool operator<(const String&str1, const String &str2)
{
if (strcmp(str1.str, str2.str)<0)
return 1;
else
return 0;
}
friend bool operator>(const String&str1, const String &str2)
{
if (strcmp(str1.str, str2.str)>0)
return 1;
else
return 0;
}
friend bool operator==(const String&str1, const String &str2)
{
if (strcmp(str1.str, str2.str)==0)
return 1;
else
return 0;
}
char &operator[](unsigned short length);
char operator[](unsigned short length)const;
String(const String&rs);
String&operator=(const String&s);
String operator+(const String&s);
void operator+=(const String&s);
private:
String(unsigned short);
unsigned short len;
char *str;
};
String String::operator+(const String&s)
{
cout<<"operator+函数执行"<<endl;
int total = len + s.getlen();
String temp(total);
int i, j;
for (i=0; i<len; i++)
{
temp[i] = str[i];
}
for (j=0; j<s.getlen(); j++)
{
temp[i++] = s[j];
}
return temp;
}
void String::operator+=(const String&s)
{
cout<<"operator+=函数执行"<<endl;
int total = len + s.getlen();
String temp(total);
int i, j;
for (i=0; i<len; i++)
{
temp[i] = str[i];
}
for (j=0; j<s.getlen(); j++)
{
temp[i++] = s[j];
}
*this = temp;
}
String::String()
{
cout<<"不带参数的构造函数"<<endl;
len = 0;
str = new char[1];
str[0] = '\0';
}
String::String(const char*const ch)
{
cout<<"带一个参数的构造函数"<<endl;
len = strlen(ch);
str = new char[len+1];
for(int i=0; i<len; i++)
str[i] = ch[i];
str[len] = '\0';
}
char & String::operator[](unsigned short length)
{
cout<<"operator[]执行"<<endl;
if (length > len)
return str[len-1];
else
return str[length];
}
char String::operator[](unsigned short length)const
{
cout<<"operator[]const 执行"<<endl;
if (length>len)
return str[len-1];
else
return str[length];
}
String::String(const String&rs)
{
cout<<"复制构造函数执行"<<endl;
len = rs.getlen();
str = new char[len+1];
for(int i=0; i<len; i++)
str[i] = rs[i];
str[len] = '\0';
}
String::String(unsigned short length)
{
cout<<"带一个int参数的构造函数"<<endl;
len = length;
str = new char[len+1];
for(int i=0; i<=len; i++)
str[i] = '\0';
}
String &String::operator=(const String&s)
{
cout<<"operator=执行"<<endl;
if (this == &s)
return *this;
delete []str;
len = s.getlen();
str = new char[len+1];
for(int i=0; i<len; i++)
str[i] = s[i];
str[len] = '\0';
return *this;
}
谢谢!