gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:05:12 24 4
gpt4 key购买 nike

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

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

这是构建日志:

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)

我的函数头和函数

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

最佳答案

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

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

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

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

关于c++ - Visual C++ 说 void 函数需要返回一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1705614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com