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
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
That's not a void
function, it's a void **
function. That means you're required to return a pointer to a void
pointer.
That function is not void, is void**. Meaning it must return a pointer to a void pointer.
Like everybody else pointed out, your return type is incorrect. You're not returning anything in your function, so just remove the ** and you'll be good to go.
Out of curiosity, did you get any warnings when compiling on your mac? g++ (on my Linux box) only gives a warning with -Wall.
Take out the ** on the return. i.e. the signature should be:
void check(int **matrix, int size);
Looking at your pastie.org code sample, my guess is that you copied and pasted the other functions but forgot to remove the **.
精彩评论