前辈们 指点一下

笑话 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;

}
谢谢!
...全文
124 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
笑话 2012-10-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
上面就是说<<他不能操作你自己写的类型了,
[/Quote]
能不能说的具体点?
longburulin 2012-10-17
  • 打赏
  • 举报
回复
上面就是说<<他不能操作你自己写的类型了,
longburulin 2012-10-17
  • 打赏
  • 举报
回复
<< is an overloaded operator that that can understand many different data types.

But it doesn't understand a parameter of type cString which is a class written by you.

In such situations you can overload a cast operator and use that cast in the line with cout.



In your class, create an operator like this -




operator char*()
{
return data;
}
Now you can use cout like this -


cout<<"String a="<< (char*)a << endl;

笑话 2012-10-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
String是自己写的,没重载<<。为何不用标准库的string。

class String{
...
ostream& operator<<(ostream& o){ o << "自己的东西"; }
...
};
[/Quote]
没有 我正在学习写字符串的东西 自己写个 您看看那出问题了?
audi2 2012-10-17
  • 打赏
  • 举报
回复
String是自己写的,没重载<<。为何不用标准库的string。

class String{
...
ostream& operator<<(ostream& o){ o << "自己的东西"; }
...
};

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧