C++使用友元函数重载运算符
#include<iostream>//使用友元函数重载运算符//usingnamespacestd;constsize=3;classvector(){intv[siz...
#include<iostream>//使用友元函数重载运算符
//using namespace std;
const size=3;
class vector()
{
int v[size];
public:
vector();//构造空向量
vector(int *p);//构造函数2
friend vector operator * (int a,vector b);
friend vector operator * (vector a,int b);
friend istream & operator>>(istream &,vector &);
friend ostream & operator<<(ostream &,vector &);
};
vector::vector()
{
for(int i=0;i<size;i++)
v[i]=0;
}
vector::vector(int *p)
{for(int i=0;i<size;i++)
v[i]=p[i];}
vector operator*(int a,vector b)
{
vector c;
for(int i=0;i<size;i++)
c.v[i]=a*b.v[i];
return c;
}
vector operator*(vector a,int b)
{
vector c;
for(int i=0;i<size;i++)
c.v[i]=b*a.v[i];
return c;
}
istream&operator>>(istream& a,vector& b)
{
for(int i=0;i<size;i++)
a>>b.v[i];
return a;
}
ostream&operator<<(ostream& a,vector& b)
{
for(int i=0;i<size;i++)
a<<b.v[i];
return a;
}
int x[size]={2,4,5};
int main()
{
vector m;
vector n=x;//调用函数2
cout<<"enter elements of vectorm:"
cin>>m>>endl;//调用operator>>函数
cout<<"m="<<m<<endl;
vector p,q;
p=2*m;
q=n*2;
cout<<"p="<<p<<endl;
cout<<"q="<<q<<endl;
return 0;
}
运行老是有问题,这是书上的例题,我完全按书上的格式输入的 展开
//using namespace std;
const size=3;
class vector()
{
int v[size];
public:
vector();//构造空向量
vector(int *p);//构造函数2
friend vector operator * (int a,vector b);
friend vector operator * (vector a,int b);
friend istream & operator>>(istream &,vector &);
friend ostream & operator<<(ostream &,vector &);
};
vector::vector()
{
for(int i=0;i<size;i++)
v[i]=0;
}
vector::vector(int *p)
{for(int i=0;i<size;i++)
v[i]=p[i];}
vector operator*(int a,vector b)
{
vector c;
for(int i=0;i<size;i++)
c.v[i]=a*b.v[i];
return c;
}
vector operator*(vector a,int b)
{
vector c;
for(int i=0;i<size;i++)
c.v[i]=b*a.v[i];
return c;
}
istream&operator>>(istream& a,vector& b)
{
for(int i=0;i<size;i++)
a>>b.v[i];
return a;
}
ostream&operator<<(ostream& a,vector& b)
{
for(int i=0;i<size;i++)
a<<b.v[i];
return a;
}
int x[size]={2,4,5};
int main()
{
vector m;
vector n=x;//调用函数2
cout<<"enter elements of vectorm:"
cin>>m>>endl;//调用operator>>函数
cout<<"m="<<m<<endl;
vector p,q;
p=2*m;
q=n*2;
cout<<"p="<<p<<endl;
cout<<"q="<<q<<endl;
return 0;
}
运行老是有问题,这是书上的例题,我完全按书上的格式输入的 展开
4个回答
展开全部
请问你的开发工具是不是vc++6.0 这家伙有个BUG 就是友元函数编译不过去,是编译器的问题,解决办法:
1 把头文件改为include<iostream.h>这种情况下就不用声明命名空间了,就是去掉usingnamespace.
2在#include<iostream>//使用友元函数重载运算符
//using namespace std;后面先对类进行说明,在对他的友元函数进行说明就行了;
纯手打,望采纳
1 把头文件改为include<iostream.h>这种情况下就不用声明命名空间了,就是去掉usingnamespace.
2在#include<iostream>//使用友元函数重载运算符
//using namespace std;后面先对类进行说明,在对他的友元函数进行说明就行了;
纯手打,望采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>//使用友元函数重载运算符
using namespace std;
const int size =3;
class vector;
vector operator * (int a,vector b);
vector operator * (vector a,int b);
istream & operator>>(istream &,vector &);
ostream & operator<<(ostream &,vector &);
class vector
{
int v[size];
public:
vector(); //构造空向量
vector(int *p);//构造函数2
friend vector operator * (int a,vector b);
friend vector operator * (vector a,int b);
friend istream & operator>>(istream &,vector &);
friend ostream & operator<<(ostream &,vector &);
};
vector::vector() {
for (int i = 0; i < size; i++)
v[i] = 0;
}
vector::vector(int *p) {
for (int i = 0; i < size; i++)
v[i] = p[i];
}
vector operator*(int a, vector b) {
vector c;
for (int i = 0; i < size; i++)
c.v[i] = a * b.v[i];
return c;
}
vector operator*(vector a, int b) {
vector c;
for (int i = 0; i < size; i++)
c.v[i] = b * a.v[i];
return c;
}
istream&operator>>(istream& a, vector& b) {
for (int i = 0; i < size; i++)
a >> b.v[i];
return a;
}
ostream&operator<<(ostream& a, vector& b) {
for (int i = 0; i < size; i++)
a << b.v[i];
return a;
}
int x[size] = { 2, 4, 5 };
int main() {
vector m;
vector n(x); //调用函数2
cout << "enter elements of vectorm:" << endl;
cin >> m; //调用operator>>函数
cout << "m=" << m << endl;
vector p, q;
p = 2 * m;
q = n * 2;
cout << "p=" << p << endl;
cout << "q=" << q << endl;
return 0;
}
enter elements of vectorm:
1 2 3
m=123
p=246
q=4810
using namespace std;
const int size =3;
class vector;
vector operator * (int a,vector b);
vector operator * (vector a,int b);
istream & operator>>(istream &,vector &);
ostream & operator<<(ostream &,vector &);
class vector
{
int v[size];
public:
vector(); //构造空向量
vector(int *p);//构造函数2
friend vector operator * (int a,vector b);
friend vector operator * (vector a,int b);
friend istream & operator>>(istream &,vector &);
friend ostream & operator<<(ostream &,vector &);
};
vector::vector() {
for (int i = 0; i < size; i++)
v[i] = 0;
}
vector::vector(int *p) {
for (int i = 0; i < size; i++)
v[i] = p[i];
}
vector operator*(int a, vector b) {
vector c;
for (int i = 0; i < size; i++)
c.v[i] = a * b.v[i];
return c;
}
vector operator*(vector a, int b) {
vector c;
for (int i = 0; i < size; i++)
c.v[i] = b * a.v[i];
return c;
}
istream&operator>>(istream& a, vector& b) {
for (int i = 0; i < size; i++)
a >> b.v[i];
return a;
}
ostream&operator<<(ostream& a, vector& b) {
for (int i = 0; i < size; i++)
a << b.v[i];
return a;
}
int x[size] = { 2, 4, 5 };
int main() {
vector m;
vector n(x); //调用函数2
cout << "enter elements of vectorm:" << endl;
cin >> m; //调用operator>>函数
cout << "m=" << m << endl;
vector p, q;
p = 2 * m;
q = n * 2;
cout << "p=" << p << endl;
cout << "q=" << q << endl;
return 0;
}
enter elements of vectorm:
1 2 3
m=123
p=246
q=4810
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你能具体说是什么问题吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |