| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 539 人关注过本帖
标题:[求助]这个程序有问题。建立对象时有问题还是声明类时有问题?
只看楼主 加入收藏
xinghe69
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-10-28
收藏
 问题点数:0 回复次数:3 
[求助]这个程序有问题。建立对象时有问题还是声明类时有问题?

// 求助各位大虾。这个程序建立对象时有问题还是声明类时有问题?
//程序目的是求矩阵的和。
// 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();

}

搜索更多相关主题的帖子: 声明 对象 时有 
2006-11-19 10:39
maoguoqing
Rank: 6Rank: 6
来 自:重庆
等 级:贵宾
威 望:28
帖 子:2980
专家分:19
注 册:2005-12-5
收藏
得分:0 
以下是引用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();

}


天行健,君子以自强不息!!QQ:68660681
2006-11-19 12:31
freshman42
Rank: 1
等 级:新手上路
威 望:1
帖 子:94
专家分:0
注 册:2005-12-4
收藏
得分:0 

都有问题
构造函数错了,应该是
[CODE]
matrix::matrix(int xx[3][3],int yy[3][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
x[i][j]=xx[i][j];
y[i][j]=yy[i][j];
}
}

[/CODE]
还有是
[CODE]
matrix add(m,n); //不要[3][3]不然就是一个数了 而不是传递数组了
[/CODE]

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]; //到这j=3,数组越界了
}
}


2006-11-19 12:34
xinghe69
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-10-28
收藏
得分:0 

// 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();

}


2006-11-19 14:37
快速回复:[求助]这个程序有问题。建立对象时有问题还是声明类时有问题?
数据加载中...
 
   



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

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