Hi,
i am getting frustrated with my code. please, please, please help me.
i wrote a class eArray with different functions and i have all of them working but 2.
for the first i am trying to get the nth fibonacci number.
the second one, i am trying to add two equal sized eArrays where both are two multi digit numbers.
the operation is of the form P+=Q.
i am getting some errors like:

Error 1 error C2784: 'std::_String_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_iterator<_Elem,_Traits,_Alloc>' from 'eArray' h:\ece 304\ece304w1fp1(earray)\ece304w1fp1(earrray)\ece304w1fp1(earrray)\code.cpp 213
please help me. any help is greatly appreciated.
Below is my posted code:

#include <iostream>
using namespace std;
void compLfact(int);
void copyFrom();
void addTo();
void lfib(int);

//class Definition
class eArray{
public:
	//Constructors
	eArray();
	eArray(int);

	//accessor function to find the state of the object
	int getSize();
	//Set the elements to some value
	void set(int);
	void set(int, int);
	//Get the value of an element
	int get(int);
	//Print functions
	void dump();//print everything
	void dumpR();
	void vMpyC(int);//vector multiply by an integer constant
	void aMpyC(int);//arithmetic multiply by an integer
	void expandBy(int);
	void trimTo(int);
	
private://
	int size; //current size of the array
	int* data;//pointer to the array
	void init(int);
};

//class Implementation
eArray::eArray(){
	init(4); //default size = 4 elements
}
eArray::eArray(int s){
	init(s);//whatever value u want
}
void eArray::init(int n){
	size = n;
	data = new int[n];//dynamically declared array

}
void eArray::set(int v){
	for(int i=0; i<size; i++)
		data[i]=v;
}
void eArray::set(int index, int val){
	if (index < 0 || index >= size){
		cout<<"Error******"<<endl;
		cout<<"Invalid index --- Operation aborted"<<endl;
	}
	else data[index] = val;	
}
int eArray::getSize(){
	return size;//tells the no. of active elements there are.
}
int eArray::get(int index){
	if (index < 0 || index >= size){
		cout<<"Error******"<<endl;
		cout<<"Invalid index --- Operation aborted"<<endl;
		return -1;//if the index is wrong
	}
	else return data[index];
}
void eArray::dump(){
	int i;
	for(i=0; i<size; i++){
		if ((data[i] !=0))
break;
	}
		while (i<size)
			cout<<data[i++]<<" ";
			cout<<endl;
	}


void eArray::dumpR(){
	for(int i=size-1; i>=0; i--)
		cout<<data[i]<<" ";
	cout<<endl;
}
void eArray::vMpyC(int c){
	for(int i = 0; i < size; i++)
		data[i] *=c;//multiply each component of the array by constant c
}
void eArray::aMpyC(int c){
int i,carry,temp;
	for(i=size-1,carry=0; i > -1; i--){
		temp=data[i]*c + carry;
		data[i]= temp%10;
		carry=temp/10;

	}
}


//This member function expands the size of the array by adding 
//n more elements to it.
void eArray::expandBy(int n){
int news=size+n;//news means new size
int *temp = new int[news];//create a temp array
int i;
//copy the old data into temp
	for(i=0; i<size; i++)
		temp[i]=data[i];
	//set the newly added elements to zero
	for(i=size; i<news; i++)
		temp[i]=0;
	// we don't need the old data anymore 
	//so we delete them
	delete []data;
	data=temp; //point data to temp
	size=news;//update the size
}
//This function trims the vector by removing all but first
//n components
void eArray::trimTo(int n){
int *temp, i;
	if(n > size){
		cout<<"Error in size spec****"<<endl;
		cout<<"Operation is aborted"<<endl;
	}
	else {
		temp = new int[n];
		//copy the first n numbers into temp
		for (i=0; i<n; i++)
			temp[i]=data[i];
		//Now the old data isn't needed
		delete []data;
		//make data points to the temp
		data=temp;
		size=n;//update size
	}
}
/*void eArray::lfib(int f){
	int i, x, y;
	x = 1;
	y = 1;
	for(i=1; i<=f; i++){
		x = x + y;
		y = y + x;
		cout<<x<<endl;
		cout<<y<<endl;
	}

}*/




void main(){
	eArray x(8);
	x.set(10);
	cout<<x.getSize()<<endl;
	cout<<x.get(4)<<endl;//4th element is 10
	x.set(4,7);
	cout<<x.get(4)<<endl;
eArray y;
y.set(5);
y.dump();
int i;
	for(i=1; i<5; i++)
		y.set(i-1, i);
	y.dumpR();
	y.vMpyC(10);
	y.dump();
eArray z(5);
	z.set(0, 1);
	z.set(1, 7);
	z.set(2, 8);
	z.set(3, 2);
	z.set(4, 4);
	z.dump();
	z.aMpyC(4);
	z.dump();
	y.expandBy(4);//increase the size of the vector y by
//4 more components. size is 4, after expansion,
//size will be 8.
	cout<<y.getSize()<<endl;
	y.dump();
	y.trimTo(2);//to keep 2 numbers instead of 8 numbers
	y.dump();
	int n;
	cout<<"Input n:"; cin>>n;
	compLfact(n);
	copyFrom();
	lfib(5);

	
}
void compLfact(int n){
	eArray d(20);//create an expandable array of 20 numbers.
	int i;
		d.set(0);//set all the numbers to zero.
		d.set(19,1);//factorial of zero is one.
		for(i=2; i<=n; i++)
			d.aMpyC(i);
		cout<<"The answer is"<<" ";
			d.dump();
		cout<<endl;
}
void lfib(int a){
	int i;
	eArray x(500), y(500), ans(500);
	x=y=1;
	for(i = 1; i<=a; i++){
		ans = x + y;
		x = y;
		y = ans;
	}

}

	

void copyFrom(){
	int i;
	eArray P(5);
	P.set(0, 2);
	P.set(1, 3);
	P.set(2, 4);
	P.set(3, 5);
	P.set(4, 6);
	cout<<" "<<endl;
	cout<<"P elements:";
	P.dump();
	cout<<endl;
	eArray Q(5);
	for(i=0; i<=5; i++)
		Q = P;
	cout<<"After P is copied into Q, Q is:";
	Q.dump();
	cout<<endl;
	

}
void addTo(){
	int i;
	eArray P(5);
	eArray Q(5);
	P.set(0,10);
	P.set(1,20);
	P.set(2,30);
	P.set(3,40);
	P.set(4,50);
	for(i=0; i<=5; i++)
	Q.set(i-1, i);
	P+=Q;

	cout<<"The sum is"<<" ";
	P.dump();
	cout<<endl;
}

With such a large file, it would be helpful to have line numbers to find where the error message is pointing. When posting code like this, use the form

[code=c++] void lfib(int a){ int i; }

[/code]
which will make your code look like:

void lfib(int a){
	int i;
	eArray x(500), y(500), ans(500);
	x=y=1;
	for(i = 1; i<=a; i++){


		ans = x + y;
		x = y;
		y = ans;
	}

}

which is the area the compile problem occurs; several actually. As noted, you have not defined what the addition operator ( + ) means in terms of an eArray object, nor have you created a copy constructor to handle the assignment operation ( = ).

In line 4, you should be using your set( int, int ) method to assign values. Lines 6, 7, 8 should use the set( ) and get( ) methods to access discrete values. I don't think you meant to add or assign the entire arrays. And, you need some indexing variable to move along through the eArray objects.

When the loop is completed, what do you do with the answer? Wouldn't it make sense to return it to the caller? That's how this function is usually written.

And please don't start additional threads on the same topic. You should have continued this in your earlier posting.

hi,
thank you for replying. i finally figured out a way to add two arrays and using eArray.
i also wrote the fibonacci code. However, when i debug and print the fibonacci number, i get some numbers like this:

8
10
7
5 5 5 5
4 3 2 1
10 20 30 40
1 7 8 2 4
7 1 2 9 6
8
10 20 30 40 0 0 0 0
10 20
Input n:5
-6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6
-6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -
6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6 -6
-6 -6 -6 -6 -6 -6 -6 -6 -6 -5 -9 -1 -8 0 -8 -8 -4 -8 -8 8
After a is copied to v, v becomes 9 3 4 5 6

After a is added to v, v becomes 8 6 9 1 2

>>it prints the 5th fibonacci number at the end of the gabbage.
when i try to print the 10th fibonacci number which is 89, i get 9 at the end of the gabbage.
Help me please. The eArray class is still the same with some more functions added:

#include <iostream>
using namespace std;
void compLfact(int);
void compLfib(int);

//class Definition
class eArray{
public:
	//Constructors
	eArray();
	eArray(int);//constructor cos the name of the class is d same
	//as that of the constructor. they also don't need return types.
	//the two constructors are overloaded cos the have the same name.
	//accessor function to find the state of the object
	int getSize();
	//Set the elements to some value
	void set(int);//check notes
	void set(int, int);//check notes
	//Get the value of an element
	int get(int);
	//Print functions
	void dump();//print everything
	void dumpR();
	void vMpyC(int);//vector multiply by an integer constant
	void aMpyC(int);//arithmetic multiply by an integer
	void expandBy(int);
	void trimTo(int);
	void copyTo(eArray);
	void addTo(eArray);
	
private://only the class designer can access the class
	int size; //current size of the array
	int* data;//pointer to the array
	void init(int);
};

//class Implementation
eArray::eArray(){
	init(4); //default size = 4 elements
}
eArray::eArray(int s){
	init(s);//whatever value u want
}
void eArray::init(int n){
	size = n;
	data = new int[n];//dynamically declared array
	//size of the array can change; non-static
}
void eArray::set(int v){
	for(int i=0; i<size; i++)
		data[i]=v;
}
void eArray::set(int index, int val){
	if (index < 0 || index >= size){
		cout<<"Error******"<<endl;
		cout<<"Invalid index --- Operation aborted"<<endl;
	}
	else data[index] = val;	
}
int eArray::getSize(){
	return size;//tells the no. of active elements there are.
}
int eArray::get(int index){
	if (index < 0 || index >= size){
		cout<<"Error******"<<endl;
		cout<<"Invalid index --- Operation aborted"<<endl;
		return -1;//if the index is wrong
	}
	else return data[index];
}
void eArray::dump(){
	int i;
	for(i=0; i<size; i++){
		if ((data[i] !=0))
break;
	}
		while (i<size)
			cout<<data[i++]<<" ";
			cout<<endl;
	}


void eArray::dumpR(){
	for(int i=size-1; i>=0; i--)
		cout<<data[i]<<" ";
	cout<<endl;
}
void eArray::vMpyC(int c){
	for(int i = 0; i < size; i++)
		data[i] *=c;//multiply each component of the array by constant c
}
void eArray::aMpyC(int c){
int i,carry,temp;
	for(i=size-1,carry=0; i > -1; i--){
		temp=data[i]*c + carry;
		data[i]= temp%10;
		carry=temp/10;

	}
}


//This member function expands the size of the array by adding 
//n more elements to it.
void eArray::expandBy(int n){
int news=size+n;//news means new size
int *temp = new int[news];//create a temp array
int i;
//copy the old data into temp
	for(i=0; i<size; i++)
		temp[i]=data[i];
	//set the newly added elements to zero
	for(i=size; i<news; i++)
		temp[i]=0;
	// we don't need the old data anymore 
	//so we delete them
	delete []data;
	data=temp; //point data to temp
	size=news;//update the size
}
//This function trims the vector by removing all but first
//n components
void eArray::trimTo(int n){
int *temp, i;
	if(n > size){
		cout<<"Error in size spec****"<<endl;
		cout<<"Operation is aborted"<<endl;
	}
	else {
		temp = new int[n];
		//copy the first n numbers into temp
		for (i=0; i<n; i++)
			temp[i]=data[i];
		//Now the old data isn't needed
		delete []data;
		//make data points to the temp
		data=temp;
		size=n;//update size
	}
}

void eArray::copyTo(eArray t){
	int i;
for(i=0; i<size; i++)
	t.data[i] = data[i];
}

void eArray::addTo(eArray t){
	int i, temp, carry;
	for(i=size-1,carry=0; i > -1; i--){
		temp=data[i] + t.get(i) + carry;
		data[i]= temp%10;
		carry=temp/10;
	}
}

	

void main(){
	eArray x(8);
	x.set(10);
	cout<<x.getSize()<<endl;
	cout<<x.get(4)<<endl;//4th element is 10
	x.set(4,7);
	cout<<x.get(4)<<endl;
eArray y;
y.set(5);
y.dump();
int i;
	for(i=1; i<5; i++)
		y.set(i-1, i);
	y.dumpR();
	y.vMpyC(10);
	y.dump();
eArray z(5);
	z.set(0, 1);
	z.set(1, 7);
	z.set(2, 8);
	z.set(3, 2);
	z.set(4, 4);
	z.dump();
	z.aMpyC(4);
	z.dump();
	y.expandBy(4);//increase the size of the vector y by
	//4 more components. size is 4, after expansion,
	//size will be 8.
	cout<<y.getSize()<<endl;
	y.dump();
	y.trimTo(2);//to keep 2 numbers instead of 8 numbers
	y.dump();
	int n;
	cout<<"Input n:"; cin>>n;
	//compLfact(n);
	compLfib(n);
	eArray a(5);
	eArray v(5);
	a.set(0, 9);
	a.set(1, 3);
	a.set(2, 4);
	a.set(3, 5);
	a.set(4, 6);
	cout<<"After a is copied to v, v becomes"<<" ";
	a.copyTo(v);
	a.dump();
	cout<<endl;
	cout<<"After a is added to v, v becomes"<<" ";
	a.addTo(v);
	a.dump();
	cout<<endl;
	
}
void compLfact(int n){
	eArray d(20);//create an expandable array of 20 numbers.
	int i;
		d.set(0);//set all the numbers to zero.
		d.set(19,1);//factorial of zero is one.
		for(i=2; i<=n; i++)
			d.aMpyC(i);
		cout<<"The answer is"<<" ";
			d.dump();
		cout<<endl;
}
void compLfib(int n){
	int i;
	eArray a(100); 
	eArray b(100);
	eArray c(100);
	a.set(99, 1);
	b.set(99, 1);
	c.set(99, 1);
	for(i=2; i<=n; i++){
		c.addTo(a);
		a.set(0);
		b.copyTo(a);
		b.set(0);
		c.copyTo(b);
	}
	c.dump();



}

Thanks in advance for your help.

This article has been dead for over six months. Start a new discussion instead.