#include <iostream>
using namespace std;

class sMatrix
{
public:
	sMatrix(int,int);
	sMatrix (sMatrix&);
	int getR();
	int getC();
	bool rValid(double);
	bool cValid(double);
	void setEl(double,double,double);
	double getEl(int i,int j);
	int getVal();
	void print();
	void add(sMatrix&);
private:
	int nr, nc;	//number of row and cols
	int nent;	//number of entries
	int nmax;	//the max number of entries
	double *data;	//store the data, the size of the storage is nmax*3
};

sMatrix::sMatrix(int rr,int cc)
{
	nr=rr;
	nc=cc;
	nent=0;
	nmax=(nr*nc)/2;
	data=new double [3*nmax];
	for (int i=0; i<3*nmax; i++)
		data [i]=0.0;
}

int sMatrix::getR()
{
	return nr;
}

int sMatrix::getC()
{
	return nc;
}

bool sMatrix::rValid(double r)
{
	if (r<0 || r>=nr)
		return false;
	return true;
}

bool sMatrix::cValid(double r)
{
	if (r<0 || r>=nc)
		return false;
	return true;
}

void sMatrix::setEl(double r,double c, double val)
{
	int i;
	if (rValid(r) && cValid(c))
	{
		i=3*nent;
		data[i]=r;
		data[i+1]=c;
		data[i+2]=val;
		nent++;
	}
}

void sMatrix::print()
{
	int i;
	for (i=0;i<3*nent;i+=3)
	{
		cout<<"<"<<data[i]<<",";
		cout<<""<<data[i+1]<<",";
		cout<<""<<data[i+2]<<">"<<endl;
	}
}


int sMatrix::getVal()
{
	int i,j;
	for (j=0;nent--;j++)
	{
		i=j*3;
		if (nr==data[i] && nc==data[i+1])
		{
			return data[i+2];
		}
	}
	return 0;	
}


void sMatrix::add(sMatrix &N)
{
	int i,j;
	for (i=0;i<=nr;i++)
	{	
		for (j=0;j<=nc;j++)
		{
			double Val1=getVal();
			double Val2=N.getVal();
			setEl(i,j,Val1+Val2);			
		}
	}
}

void main ()
{
	sMatrix M(3,3);
	M.setEl(0,0,3);
	M.setEl(1,1,4);
	M.setEl(2,0,2.4);
	M.print();
	sMatrix N(3,3);
	N.setEl(0,0,0);
	N.setEl(0,0,0);
	N.setEl(0,0,0);
	N.print();
	M.add(N); //prints everything before this point
	M.print();
}

I was told theres an issue with my getVal function. I doesn't take in arguments? I'm at a complete loss. the complier prints to a certain point and then crashes.

Firstly use int main its easier to find errors and secondly M is an object and N is an object you can not use them like that reconfigure your classes

sMatrix M(3,3); // declare M object
	M.setEl(0,0,3);
	M.setEl(1,1,4);
	M.setEl(2,0,2.4);
	M.print();
	sMatrix N(3,3);//declare N object
	N.setEl(0,0,0);
	N.setEl(0,0,0);
	N.setEl(0,0,0);
	N.print();
	M.add(N); //M object member add (object N) is a syntax error as there is no builtin function for two objects of //the same class to talk to each other ( As far as i know (i would google it if i were you but reconfiguring your classes should fix //the problem))
	M.print();

Firstly use int main

...because it's complying with the standard and won't compile on many modern compilers

there is no builtin function for two objects of //the same class to talk to each other

This is not true. It's a bit counterintuitive but objects of the same class can access the private variables of the other.

wait are you saying int main won't compile on modern compilers or void main because both will compile under gcc and mingw i don't consider "vc ver (insert newest version number here" the definition of modern even though its newer ( and on vc 2008 i know it compiles not sure about 2010 or whatever the new one is) than mingw and gcc is almost always up to date. and wouldn't that defeat the purpose of private members?

i was just recommending it op can use whatever personally though i like to know if my program can see or if its blind ( 0 and 1 respectively)

I have mingw 3.4.2 (I have a later 4.4 version but there is a quirk in it with the iostreams and -ansi so I put it away for a bit)
I tried compiling:

void main()
{

}
maintest.cpp:2: error: `main' must return `int'

and that was without any switches to make it more strict. It's really just wrong to have it void but that idea took hold in people and then they wrote books. :)

wouldn't that defeat the purpose of private members?

I'm sure there's a "grand scheme" kind of reasoning for it but I do not know it offhand. I'll try looking a little later.

What do I write? Since posting this thread I still haven't come up with anything. C++ is completely new to me.

huh i just got the same error on mine as well weird of course then again i was using a bit old compiler heh . borland 5.5 i think does it though and the version of gcc and mingw i had before did it.

side note: this is the second time ive seen some one try to use another member in another member i guess i could see the use but really though you could just make another class and friend it or make a derived class. :)

@webdragon89:
go here and here
www.cprogramming.com //go here for some neat and usefull C functions that you can use in C .
www.cplusplus.com //go here first

they are great resources infact i still check them to make sure i havent forgotten anything :)

From your explanation, it looks like the problem could be the getVal function.

Could you try and re-write the for loop in getVal so that there is a proper exit condition here?

for (j=0;nent--;j++)

I am not having access to a compiler and hence can't say whether it is valid or not.

Part of the problem is this line in getVal: for (j=0;nent--;j++) You don't have a stop condition on the for loop -- I think you wanted j<nent .

Also change the return type of getVal to double since that's the datatype of "data."

This doesn't solve everything but it should get you a little further.

What still bothers me is that you aren't passing any indicies to getVal.

One more observation: When you are adding the matrices together you are calling setEl. Each time this is incrementing nent even though the matrix is already full. So in some form or another you'll have to integrate that count updating in another part of the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.