tags:

views:

609

answers:

5

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

+2  A: 

That's not a void function, it's a void ** function. That means you're required to return a pointer to a void pointer.

Adam Maras
+13  A: 

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

m-sharp
how did this compile correctly on my mac then?
Raptrex
@Raptrex, It doesn't. Nothing works correctly on a Mac.
strager
ouch, any other reasons?
Raptrex
That's another question: "I have a function that should return a ptr. It doesn't and my compiler doesn't complain. Why?"
Bill Forster
Interesting: with `#include <iostream>` and `using namespace std;`, with no warnings options, it does compile cleanly on MacOS 10.5.8 with G++ 4.0.1 (`g++ -c xxx.cpp`). Add '-Wall' and I get:`g++ -Wall -c xxx.cpp``xxx.cpp: In function ‘void** check(int**, int)’:``xxx.cpp:10: warning: unused variable ‘rowflag’``xxx.cpp:19: warning: unused variable ‘columnflag’``xxx.cpp:28: warning: unused variable ‘diagonalflag’``xxx.cpp:46: warning: control reaches end of non-void function`I'm not sure why G++ is allowing no returned value from a non-void function - moral: always use '-Wall'.
Jonathan Leffler
@Raptrex: Neither C nor C++ compilers are required to detect missing `return` statements. Missing return is not an error in C/C++. (BTW, the above answer is incorrect to claim that it will not compile in C++). Which means that this is a Quality-of-Implemetation issue. Some compiler go that extra mile to detect missing returns, some don't. You Mac compiler is apparently of a lazy variety, so it didn't detect it. Also, it is possible that your Mac compiler has a switch that forces it to detect this kind of problem, you just forgot to turn it on.
AndreyT
Apple truly massacred the beauty of GCC...
LiraNuna
+2  A: 

That function is not void, is void**. Meaning it must return a pointer to a void pointer.

Remus Rusanu
+2  A: 

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.

Stephen Newell
i used g++ -o magicsquare magicsquare.cpp
Raptrex
See my annotation to one other answer: G++ 4.0.1 on MacOS X 10.5.8 only gives a warning with '-Wall'.
Jonathan Leffler
@Jonathan Leffler: same thing I noticed on Linux (g++ 4.3.4)
Stephen Newell
+2  A: 

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 **.

Phillip Ngan