PDA

View Full Version : Help with converting from void to int.


qoo111
2007.04.18, 12:12 AM
I created this program;however, I got 6 errors. All the errors said this.

C:\Program Files\Microsoft Visual Studio\MyProjects\PA33\pa3.cpp(114) : error C2440: '=' : cannot convert from 'void' to 'int'

If anyone know how to do this, please help me.

program is about dynamic array.

#include <sstream>
#include <iostream>

using namespace std;

int ** NewArray2D(int nRow, int nCol) //This function uses the new operator to create a 2D array of ints with nRow rows and nCol columns.
{
int **b;
b = new int* [nRow];

for(int i = 0; i < nCol; ++i)
{
b[i] = new int[nCol];
for (int j = 0; j < nCol; ++j)
b[i][j] = 0;
}
cout<<endl;

return b;
}

void DelArray2D(int **&array2D, int nRow) //This function uses the delete operator to delete all the memory allocated for array2D and then sets array2D to 0.
{
int **b = array2D;

for (int i = 0; i < nRow; ++i)
delete [] b[i];
delete [] b;

b = 0;
}

void InitArray2D(int **array2D, int nRow, int nCol, int val) //This function sets every element of array2D to the value val.
{
int **b = array2D;

for (int j = 0; j < nRow; j++)
{
for(int k = 0; k < nCol; k++)
{
b[j][k] = val;
}
}
cout<<endl;
}

int **CopyArray2D(int **a2D, int **b2D, int nRow, int nCol) //This function copies array b2D over array a2D, which means that all elements of b2D are copied over corresponding elements of a2D.
{
int **a = a2D;
int **b = b2D;

for(int i = 0; i < nRow; i++)
{
for(int j = 0; j < nCol; j++)
{
b[i][j] = a[i][j];
}
}
return b;
}

int **AddArray2D(int **a2D, int **b2D, int nRow, int nCol) //This function adds corresponding elements of arrays a2D and b2D and assigns the values to corresponding elements of a new array c2D, which the function creates for the purpose.
{
int **a = a2D;
int **b = b2D;
int **c;

c = new int* [nRow];

for(int i = 0; i < nRow; i++)
{
c[i] = new int[nCol];
for(int j = 0; j < nCol; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
return c;
}

string ToString(int ** a2D, int nRow, int nCol) //This function creates a C++ string from the values of the elements of array a2D.
{
stringstream ss;

for(int i = 0; i < nRow; i++)
{
for(int j = 0; j < nCol; j++)
{
ss<<a2D[i][j]<<" ";
}
ss<<endl;
}
return ss.str();
}

int main()
{
int i;
int j;
int x = 0;
int y = 0;

cout << "Please enter the number of rows you want in your array: ";
cin >> x;

cout << "Please enter the number of columns you want in your array: ";
cin >> y;

int **a2d = NewArray2D(x,y);

cout << "Array a2d before calling function InitArray2D:" << endl;
cout << ToString(a2d,x,y) << endl;

**a2d = InitArray2D(a2d, x, y, 8);

cout << "Array a2d after calling function InitArray2D:" << endl;
cout << ToString(a2d,x,y) << endl;

int ** b2d = NewArray2D(x,y);

**b2d = InitArray2D(b2d, x, y, 0);

cout << "Array b2d before calling function CopyArray2D:" << endl;
cout << ToString(b2d,x,y) << endl;

b2d = CopyArray2D(a2d, b2d, x, y);
cout << endl;

cout << "Array b2d after calling function CopyArray2D:"<<endl;
cout << ToString(b2d,x,y) << endl;

int ** c2d = NewArray2D(x,y);
**c2d = InitArray2D(c2d, x, y, 0);

cout <<"Array c2d before calling function AddArray2D:"<<endl;
cout <<ToString(c2d,x,y) << endl;

c2d = AddArray2D(a2d, b2d, x, y);

cout << endl;
cout << "Array c2d after calling function AddArray2D:"<<endl;
cout << ToString(c2d,x,y) << endl;

**a2d = DelArray2D(a2d,x);
**b2d = DelArray2D(b2d,x);
**c2d = DelArray2D(c2d,x);

cout << ToString(a2d,x,y) << endl;
cout << ToString(b2d,x,y) << endl;
cout << ToString(c2d,x,y) << endl;

return 0;
}

OneSadCookie
2007.04.18, 12:17 AM
Your functions like DelArray2D don't return anything, but you're pretending they do...

qoo111
2007.04.18, 12:21 AM
Would you explain more?
I really don't get it.

OneSadCookie
2007.04.18, 12:36 AM
void DelArray2D(int **&array2D, int nRow)
**a2d = DelArray2D(a2d,x);

qoo111
2007.04.18, 12:42 AM
one more quetion.

**a2d = InitArray2D(a2d, x, y, 8);
**b2d = InitArray2D(b2d, x, y, 0);
**c2d = InitArray2D(c2d, x, y, 0);

it still aprears as error.

ThemsAllTook
2007.04.18, 09:41 AM
In order to have a value returned from a function, you need to declare the function as returning that type, and to put a return statement at the end of the function. You can't do something like "variable = myFunction();" when myFunction() doesn't return anything (which is what's meant by giving it a "void" return type).