找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1479|回复: 3

[求助]这个程序有问题。建立对象时有问题还是声明类时有问题?

[复制链接]
发表于 2009-11-4 01:14:13 | 显示全部楼层 |阅读模式 IP:江苏扬州
// 求助各位大虾。这个程序建立对象时有问题还是声明类时有问题?
//程序目的是求矩阵的和。
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
class matrix
{
public:
int x[3][3];
int y[3][3];
public:
matrix(int xx[3][3],int yy[3][3]){x[3][3]=xx[3][3];y[3][3]=yy[3][3];};
~matrix(){};
void set(int xx[3][3],int yy[3][3]);
void sum();

};
void matrix::set(int xx[3][3],int yy[3][3])
{
x[3][3]=xx[3][3];
y[3][3]=yy[3][3];
}
void matrix::sum()
{
int i,j;
int c[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[i][j]=x[i][j]+y[i][j];
cout<<c[i][j];
}
}
void main()
{
int i,j;
int m[3][3];
int n[3][3];
cout<<"please enter the first matrix number\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>m[i][j];
}
cout<<"\nplease enter the second matrix number\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>n[i][j];
}

matrix add(m[3][3],n[3][3]);
add.sum();

}
发表于 2009-11-4 01:14:15 | 显示全部楼层 IP:江苏扬州
以下是引用xinghe69在2006-11-19 10:39:26的发言:

// 求助各位大虾。这个程序建立对象时有问题还是声明类时有问题?
//程序目的是求矩阵的和。
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
class matrix
{
public:
int x[3][3];
int y[3][3];
public:
matrix(int xx[3][3],int yy[3][3]){x[3][3]=xx[3][3];y[3][3]=yy[3][3];};
~matrix(){};
void set(int xx[3][3],int yy[3][3]);
void sum();

};
void matrix::set(int xx[3][3],int yy[3][3])
{
x[3][3]=xx[3][3];
y[3][3]=yy[3][3];//数组的复制不能这样做
}
void matrix::sum()
{
int i,j;
int c[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[i][j]=x[i][j]+y[i][j];
cout<<c[i][j];
}
}
void main()
{
int i,j;
int m[3][3];
int n[3][3];
cout<<"please enter the first matrix number\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>m[i][j];
}
cout<<"\nplease enter the second matrix number\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>n[i][j];
}

matrix add(m[3][3],n[3][3]);//matrix add(m,n);
add.sum();

}
回复

使用道具 举报

发表于 2009-11-4 01:14:16 | 显示全部楼层 IP:江苏扬州
都有问题
构造函数错了,应该是

  1. matrix::matrix(int xx[3][3],int yy[3][3])
  2. {
  3. for(int i=0;i<3;i++)
  4. for(int j=0;j<3;j++)
  5. {
  6. x[i][j]=xx[i][j];
  7. y[i][j]=yy[i][j];
  8. }
  9. }

复制代码

还有是

  1. matrix add(m,n); //不要[3][3]不然就是一个数了 而不是传递数组了
复制代码


void matrix::sum()
{
int i,j;
int c[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[j]=x[j]+y[j];
cout<<c[j]; //到这j=3,数组越界了
}
}
回复

使用道具 举报

发表于 2009-11-4 01:14:17 | 显示全部楼层 IP:江苏扬州
// test.cpp : Defines the entry point for the console application.
//最后经过同学帮忙
//改成这样就能运行了。 除了上边各位帮忙指出的问题。还有负值的问题。谢谢各位的帮忙
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "iostream.h"
class matrix
{
private:
int (*x)[3];
int (*y)[3];
public:
matrix(int (*xx)[3],int (*yy)[3])
{
x=xx;
y=yy;
}
~matrix(){};
// void set(int xx[3][3],int yy[3][3]);
void sum();

};
/*
void matrix::set(int xx[3][3],int yy[3][3])
{
x[3][3]=xx[3][3];
y[3][3]=yy[3][3];
}
*/
void matrix::sum()
{
int i,j;
int c[3][3];
int t=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=(*(*(x+i)+j)+*(*(y+i)+j));
}
}

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<c[i][j]<<endl;
}
}
}
void main()
{
int i,j;
int m[3][3];
int n[3][3];
cout<<"please enter the first matrix number\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>m[i][j];
}
cout<<"\nplease enter the second matrix number\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>n[i][j];
}


matrix add(m,n);
// add.set(m,n);
add.sum();

}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 01:31 , Processed in 0.172651 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表