地址XXXXXX上的访问冲突.写地址XXXXXX:(( [英] Access violation at address XXXXXX. Write of address XXXXXX :((

查看:240
本文介绍了地址XXXXXX上的访问冲突.写地址XXXXXX:((的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的代码时
它说在地址XXXXXX上的访问冲突.地址XXXXXX
的写入 依序确认其
d.plus(a,b)
怎么了 plz 请帮助:(

when i run my code
it says Access violation at address XXXXXX. Write of address XXXXXX
en line that its
d.plus(a,b)
whats wrong plz Pleases help:(

#include <iostream>
#include <conio.h>


class Polynomial
{
public:
int array[100][100];
//==================================================
Polynomial::Polynomial() // constructor
{
for ( int i = 0; i < 100; i++ )
   for( int j=0;j<100;j++)
                           {
array[i][j] = 0;
}
}
//================================================
void set ( int a , int b,int c ) //set
{
array[a][b] = c;
}
//===================================================
Polynomial plus ( Polynomial x,Polynomial y )
{
Polynomial c1;

for ( int i=0; i<100; ++i ) {
    for(int j=0; j<100 ;++j){
 c1.array[i][j] = x.array[i][j] + y.array[i][j];
         }}

 return c1;
}
//===========================================

void print()
{
for (int i=99; i>= 0; i-- ) {
for(int j= 99; j>=0;--j){
if ( array[i][j] != 0 ) {
cout <<"+("<< array[i][j] <<")"<< "x^" << i <<"y^"<<j;
				}
			}
		}
	}
};

int main(){

Polynomial a, b, d,f;
a.set(2,2,3);
b.set(2,3,2);
d.plus(a,b);
getch();
}

推荐答案

我测试了代码,但未获得异常.

但是,您松开了计算结果;做这样的事情:
I tested the code and did not get the exception.

You loose the result of calculation though; do something like that:
f = d.plus(a,b);
_getch(); // not deprecated getch() 



但是代码很糟糕.特别是,不要硬编码此"100",尤其是在不同的地方使用它时.如果要将这100更改为其他内容,如何提供支持?整个想法是错误的.例如,您应该通过构造函数传递等级.携带许多未使用的元素毫无意义.您确实需要动态分配数组.另外,此类中的多项式".多项式表达式可以在计算机代数系统(CAS)中进行运算,例如在符号计算中进行.

请参阅以下有关返回类实例的有用概述: http://www.informit.com /articles/article.aspx?p=25033&seqNum=3 [ ^ ].

—SA



The code is pretty bad though; in particular, you should not hard-code this ''100'', especially if you use it in different places. How can you support it if you want to change this 100 to something else? The whole idea is wrong; you should pass the rank via constructor, for example. It''s pointless to carry many unused elements. You really need to allocate the arrays dynamically. Also, what''s "polynomial" in this class. A polynomial expressions can be operated in Computer Algebra Systems (CAS), for example, in symbolic calculations.

Please see this useful overview of returning the class instance: http://www.informit.com/articles/article.aspx?p=25033&seqNum=3[^].

—SA


使用i ++代替++ i和j ++代替++ j.
当您运行循环(只要i或j小于100),但您使用++ i或++ j提升i和j时,最后一轮,i或j变为100,并且您访问了不't存在(因为单元格的范围是0到99).
但是,如果您使用i ++和j ++,则当它们达到100时,程序将退出循环,而它们的值不会为100.
Use i++ instead of ++i and j++ instead of ++j.
When you run a loop as long as i or j are smaller than 100, but you promote i and j using ++i or ++j, the last round, i or j becomes 100 and you access a cell in the array that doesn''t exists (since the range of cells is 0 to 99).
But if you use i++ and j++, then when they get to 100, the program exits the loop without them having the value of 100.


这篇关于地址XXXXXX上的访问冲突.写地址XXXXXX:((的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆