Raptrex Raptrex - 1 year ago 70
C++ Question

Visual C++ saying void function needs to return a value

Visual C++ is saying my void function needs a return value

I compiled this on my mac, and it worked perfectly, but now I am trying to compile this with Visual c++ (using windows 7)

Heres the build log:

Command Lines Creating temporary file
with contents [ /Od /D "WIN32" /D
/D "UNICODE" /Gm /EHsc /RTC1 /MDd
/Fo"Debug\" /Fd"Debug\vc90.pdb" /W3
/c /ZI /TP ".\magicsquare.cpp" ]
Creating command line "cl.exe
/nologo /errorReport:prompt"

Output Window Compiling...
: error C4716: 'check' : must return a

Results Build log was saved at
magicsquare - 1 error(s), 0 warning(s)

my function header and function

void **check (int **, int);

void **check(int **matrix, int size)
//check if first row and last row are the same
int rsum = 0, rsum2 = 0;
bool rowflag = false;
for(int i = 0; i < size; i++)
rsum += *(*(matrix + 0) +i);
rsum2 += *(*(matrix + size - 1) +i);

//check if first column and last column are the same
int csum = 0, csum2= 0;
bool columnflag = false;
for(int i = 0; i < size; i++)
csum += *(*(matrix + i) + 0);
csum2 += *(*(matrix + i) + size - 1);

//check if diagonals are the same
int diagonal = 0, diagonal2 = 0;
bool diagonalflag = false;
for(int i = 0; i < size; i++)
diagonal += *(*(matrix + i) + i);

int m = 0;
int n = size - 1;
while (m <= size - 1)
diagonal2 += *(*(matrix + m) + n);

//if row, column, diagonal are the same
if (rsum == rsum2 && rsum2 == csum && csum == csum2 && csum2 == diagonal && diagonal == diagonal2)
cout << "This is a Magic Square\n" << endl;
cout << "This is not a Magic Square\n" << endl;

heres the entire code if needed

Answer Source

Your function is returning a (void **) which is a pointer to a void pointer. To make a void function simply declare it as:

void check(int** matrix, int size);

Your original code will compile with a warning in C but not in C++. Try this in Visual Studio 2008. Rename your file extension to .c instead of .cpp to force C compilation instead of C++ compilation. It will compile with a warning. But beware, if you ever used the return value of check, it would be garbage.

This link has more details: http://pdhut.50megs.com/vczone/articles/diffc/diffc.htm

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download