Help with converting from void to int.

qoo111
Unregistered
 
Post: #1
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.
Code:
#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;
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Your functions like DelArray2D don't return anything, but you're pretending they do...
Quote this message in a reply
qoo111
Unregistered
 
Post: #3
Would you explain more?
I really don't get it.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
Code:
void DelArray2D(int **&array2D, int nRow)
Code:
**a2d = DelArray2D(a2d,x);
Quote this message in a reply
qoo111
Unregistered
 
Post: #5
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.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
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).
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  dereferencing void pointers Duane 15 7,423 May 4, 2005 05:15 PM
Last Post: Puzzler183