我错在那里啊?

5204711353 2005-02-24 11:13:44
#include <iostream.h>
class stock
{
public:
friend ostream & operator<<(ostream &os,char *s);
};
ostream & operator<<(ostream & os,char *s)
{
os<<s;
return os;
}
int main()
{
cout<<"asdas";
return 0;
}
...全文
151 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
fjliningcd 2005-02-24
  • 打赏
  • 举报
回复
#include <iostream.h>
class stock
{
private:
int i;
public:
friend int operator +(stock a,stock b);
};
int operator +(stock a,stock b)
{
int tmp;
tmp=a.i+b.i;
return tmp;
}
int main()
{
stock a,b;
int c;
...
c=a+b;
cout<<c;
return 0;
}
这样修改就对了
fjliningcd 2005-02-24
  • 打赏
  • 举报
回复
所以friend int operator +(int a,int b);中至少有一个当前类的类型
fjliningcd 2005-02-24
  • 打赏
  • 举报
回复
这个friend int operator +(int a,int b);是对类对象操作符进行重载
可是在这个函数确实对int类型的运算符+进行重载。
5204711353 2005-02-24
  • 打赏
  • 举报
回复
我现在明白拉~
#include <iostream.h>
class stock
{
public:
friend ostream & operator<<(ostream &os,char *s);
};
ostream & operator<<(ostream & os,char *s)
{
int a=3;
os<<a;
return os;
}
int main()
{
cout<<"asdas";
return 0;
}
我把他改成这样就对拉~刚才那个是无限递归~
那么这个为什么错误呢?
#include <iostream.h>
class stock
{
public:
friend int operator +(int a,int b);
};
int operator +(int a,int b)
{
a=a+2;
return a;
}
int main()
{
int a,b;
a=a+b;
cout<<a;
return 0;
}
照goodluckyxl说int +被重载过~导致程序不知道用那个int +重载函数
但是cout<<"asdas",
这个<<也被重载过~我也写拉个<<重载函数
为什么却可以正确运行呢~
照goodluckyxl说被因为<<被重载过不可以运行啊~
5204711353 2005-02-24
  • 打赏
  • 举报
回复
#include <iostream.h>
class stock
{
public:
friend ostream & operator<<(ostream &os,char *s);
};
ostream & operator<<(ostream & os,char *s)
{
os<<s;
return os;
}
int main()
{
cout<<"asdas";
return 0;
}
这个怎么是无限递归呢~
我想问题出在
重载<<符号
因为<<符号已经被重载过拉~
本来库函数就有这种类型
现在我在重载个一摸一样的类型导致程序不知道用那种函数去重载吧~
5204711353 2005-02-24
  • 打赏
  • 举报
回复
是不是有2个+号都被重载拉~
导致程序不知道用那个重载函数呢?
goodluckyxl 2005-02-24
  • 打赏
  • 举报
回复
int + 已经重载过了
sherry 2005-02-24
  • 打赏
  • 举报
回复
a 和 b 都没有初始化的值 运算符+号的重载 是在类里面
外面根本没有调用。
5204711353 2005-02-24
  • 打赏
  • 举报
回复
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
c:\my documents\cpp1.cpp(5) : error C2803: 'operator +' must have at least one formal parameter of class type
c:\my documents\cpp1.cpp(8) : error C2803: 'operator +' must have at least one formal parameter of class type
Error executing cl.exe.

Cpp1.exe - 2 error(s), 0 warning(s)
insulator 2005-02-24
  • 打赏
  • 举报
回复
把错误提示贴出来啊
5204711353 2005-02-24
  • 打赏
  • 举报
回复
#include <iostream.h>
class stock
{
public:
friend int operator +(int a,int b);
};
int operator +(int a,int b)
{
a=a+2;
return a;
}
int main()
{
int a,b;
a=a+b;
cout<<a;
return 0;
}
我把他改成这样又错误~为什么啊?
5204711353 2005-02-24
  • 打赏
  • 举报
回复
我还是不明白我错在那里啊?
goodluckyxl 2005-02-24
  • 打赏
  • 举报
回复
friend ostream & operator<<(ostream &os,char *s);
公开对char*输出后
系统将会递归调用<<运算符号
导致不断调用自身operator << char*这个函数
出错
晨星 2005-02-24
  • 打赏
  • 举报
回复
ostream & operator<<(ostream & os,char *s)
{
os<<s;
return os;
}
怎么感觉是无穷递归啊?
zhudonhua 2005-02-24
  • 打赏
  • 举报
回复
cout<<"asdas";
只是调用系统内置的标准输入输出输出函数(就和int+int)一样;
#include <iostream>
#include<conio.h>
using namespace std;
class stock
{
public:
friend ostream & operator<<(ostream &os,stock& p);
stock(char *P)
{
pName=P;
}
private:
char* pName;
};
ostream & operator<<(ostream & os,stock& p)
{
os<<p.pName;
return os;
}
int main()
{
stock p("asdas");
cout<<p;
getch();
return 0;
}
goodluckyxl 2005-02-24
  • 打赏
  • 举报
回复
<<只有ostream& ostream::operator<<(const char * s)
重载成立ostream& operator<<(ostream& os, const char * s)可以的
但是如果一个参数输出的话肯定是和你 +operator 一样
系统本身已经提供了库函数
起了冲突
TomDebug 2005-02-24
  • 打赏
  • 举报
回复
int operator +(int a,int b)
{
a=a+2;
return a;
}
这样做有什么意思吗?还不如直接写一个函数得了,重载主要是对类进行重载'operator +' must have at least one formal parameter of class type就是这个意思!
bing_huo 2005-02-24
  • 打赏
  • 举报
回复
其实同样的 char* 的operator<< 一样不用重载~~~~~
io(王飞) 2005-02-24
  • 打赏
  • 举报
回复
int的+“操作符”不用你再重载了。'operator +' must have at least one formal parameter of class type——没有比这个说的更明白了:+操作符必须至少有一个类做形参,不能两个操作数都是基本类型。

64,671

社区成员

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

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