Raptrex Raptrex - 3 months ago 10
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
"c:\Users\Jonathan\Documents\Visual
Studio
2008\Projects\magicsquare\Debug\RSP00000822923000.rsp"
with contents [ /Od /D "WIN32" /D
"_DEBUG" /D "_CONSOLE" /D "_UNICODE"
/D "UNICODE" /Gm /EHsc /RTC1 /MDd
/Fo"Debug\" /Fd"Debug\vc90.pdb" /W3
/c /ZI /TP ".\magicsquare.cpp" ]
Creating command line "cl.exe
@"c:\Users\Jonathan\Documents\Visual
Studio
2008\Projects\magicsquare\Debug\RSP00000822923000.rsp"
/nologo /errorReport:prompt"

Output Window Compiling...
magicsquare.cpp
c:\users\jonathan\documents\visual
studio
2008\projects\magicsquare\magicsquare.cpp(224)
: error C4716: 'check' : must return a
value

Results Build log was saved at
"file://c:\Users\Jonathan\Documents\Visual
Studio
2008\Projects\magicsquare\Debug\BuildLog.htm"
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);
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;
else
cout << "This is not a Magic Square\n" << endl;
}


heres the entire code if needed
http://pastie.org/691402

Answer

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

Comments