| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 519 人关注过本帖
标题:sifangli1@126.com
只看楼主 加入收藏
sifangli
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-11-11
收藏
 问题点数:0 回复次数:1 
sifangli1@126.com
请高手帮个忙  下面的程序为什么运行不了?我的是vc++6.0  
改了第一二行为#include<iosteam.h>也不行
#include<iostream>
using namespace std;
class M
{public:
  M();
  friend operator+(M &,M &);
  friend ostream& operator<<(ostream&,M&);
  friend istream& operator>>(istream&,M&);
private:
    int mat[2][3];
};
M::M()
{for(int i=0;i<2;i++)
 for(int j=0;j<3;j++)
     mat[i][j]=0;
}
M operator+(M&a,M&b)
{M c;
for(int i=0;i<2;i++)
  for(int j=0;j<3;j++)
  {
    c.mat[i][j]=a.[i][j]+b.[i][j];
  }
return c;
}
istream& operator>>(istream &in,M &m)
{cout<<"input valu of matrix &m"<<endl;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
in>>m.mat[i][j];
return in;
}

 ostream& operator<<(ostream&out,M&m)
 {for(int i=0;i<2;i++)
 for(int j=0;j<3;j++)
 {
     out<<m.mat[i][j]<<" ";
     out<<endl;
 }
 return out;
 }

 int main()
 {M a,b,c;
 cin>>a;
 cin>>b;
 cout<<endl<<"M.a:"<<endl<<a<<endl;
 cout<<endl<<:M.b:"<<endl<<b<<endl;
 c=a+b;
 cout<<endl<<"M c=M a+M b:"<<endl<<c<<endl;
 return 0;
 }
搜索更多相关主题的帖子: private include public friend 
2008-11-11 13:24
ma3587
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2008-6-17
收藏
得分:0 
程序代码:
#include<iostream>
using namespace std;

class M
{
public:
    M();
    friend M operator + (M &,M &);  //这里要加返回值
    friend ostream& operator<<(ostream&,M&);
    friend istream& operator>>(istream&,M&);
private:
    int mat[2][3];
};
M::M()
{
    for (int i=0;i<2;i++)
        for (int j=0;j<3;j++)
            mat[i][j]=0;
}
M operator+(M&a,M&b)
{
    M c;
    for (int i=0;i<2;i++)
        for (int j=0;j<3;j++)
        {
            c.mat[i][j] = a.mat[i][j]+b.mat[i][j];   //这里原来有错误
        }
        return c;
}
istream& operator>>(istream &in,M &m)
{
    cout<<"input valu of matrix &m"<<endl;
    for (int i=0;i<2;i++)
        for (int j=0;j<3;j++)
            in>>m.mat[i][j];
        return in;
}

ostream& operator<<(ostream&out,M&m)
{
    for (int i=0;i<2;i++)
        for (int j=0;j<3;j++)
        {
            out<<m.mat[i][j]<<" ";
            out<<endl;
        }
        return out;
}

int main()
{
    M a,b,c;
    cin>>a;
    cin>>b;
    cout<<endl<<"M.a:"<<endl<<a<<endl;
    cout<<endl<<"M.b:"<<endl<<b<<endl;
    c=a+b;
    cout<<endl<<"M c=M a+M b:"<<endl<<c<<endl;
    return 0;
}
2008-11-11 15:15
快速回复:sifangli1@126.com
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014218 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved