Visual C++说void函数需要返回一个值

发布于 2024-08-10 20:38:19 字数 2184 浏览 6 评论 0原文

Visual C++ 说我的 void 函数需要一个返回值

我在我的 mac 上编译了它,它工作得很好,但现在我尝试用 Visual c++ 编译它(使用 windows 7)

继承日志:

命令行创建临时文件 “c:\用户\乔纳森\文档\视觉 工作室 2008\项目\magicsquare\调试\RSP00000822923000.rsp" 包含内容 [ /Od /D "WIN32" /D “_DEBUG”/D“_CONSOLE”/D“_UNICODE” /D“UNICODE”/Gm /EHsc /RTC1 /MDd /Fo"调试\" /Fd"调试\vc90.pdb" /W3 /c /ZI /TP ".\magicsquare.cpp" ] 创建命令行“cl.exe @"c:\Users\Jonathan\Documents\Visual 工作室 2008\项目\magicsquare\调试\RSP00000822923000.rsp" /nologo /errorReport:提示"

输出窗口编译... 魔方.cpp c:\用户\乔纳森\文档\视觉 工作室 2008\项目\magicsquare\magicsquare.cpp(224) :错误 C4716:“检查”:必须返回 值

结果构建日志保存在 “文件://c:\Users\Jonathan\Documents\Visual 工作室 2008\项目\magicsquare\调试\BuildLog.htm” magicsquare - 1 个错误,0 个警告

我的函数头和函数

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;
}

继承了整个代码(如果需要) http://pastie.org/691402

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无名指的心愿 2024-08-17 20:38:19

您的函数返回一个 (void **),它是指向 void 指针的指针。要创建 void 函数,只需将其声明为:

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

您的原始代码将在 C 中编译时带有警告,但在 C++ 中则不会。在 Visual Studio 2008 中尝试此操作。将文件扩展名重命名为 .c 而不是 .cpp,以强制进行 C 编译而不是 C++ 编译。它将编译并带有警告。但要注意,如果你曾经使用过 check 的返回值,那将是垃圾。

此链接有更多详细信息:
http://pdhut.50megs.com/vczone/articles/diffc/diffc.htm

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

留蓝 2024-08-17 20:38:19

这不是一个 void 函数,而是一个 void ** 函数。这意味着您需要返回一个指向 void 指针的指针。

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

东走西顾 2024-08-17 20:38:19

该函数不是void,是void**。这意味着它必须返回一个指向 void 指针的指针。

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

や莫失莫忘 2024-08-17 20:38:19

就像其他人指出的那样,您的返回类型不正确。您不会在函数中返回任何内容,因此只需删除 ** 即可。

出于好奇,您在 Mac 上编译时是否收到任何警告? g++(在我的 Linux 机器上)只给出带有 -Wall 的警告。

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.

水水月牙 2024-08-17 20:38:19

去掉退货单上的**。即签名应该是:

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

查看您的 Pastie.org 代码示例,我的猜测是您复制并粘贴了其他函数,但忘记删除 **。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文