我是 C++(和一般编码)的新手,正在学习数组。分配的目的是让用户输入方形参数和数字来填充每一行。我的程序的工作是获取用户输入的数字并根据行、列和对角线验证总和。
嗯,最初当我在 main 中编写程序时,它是有效的。但是,我必须让程序在 bool 函数中运行,如果失败则返回 false。对于我的生活,我想不出一种方法来让它发挥作用。我不断收到 1 个 Unresolved external 问题的 fatal error 。有人可以帮帮我吗?我已经用尽了所有资源。
谢谢。
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
bool validateSums(int, int);
int main()
{
int row,
square[10][10];
bool match;
cout << "Enter the size of your square by the # of rows (e.g. 3 would yield a 3x3 square).\n"
<< "Please keep the # to 10 or below." << endl;
cin >> row;
if (row >= 1 && row <= 10)
{
cout << "Your square will be " << row << " x " << row << " big." << endl;
for (int index = 0; index < row; index++)
{
cout << "List " << row << " numbers for row " << (index + 1)
<< " separated by a space." << endl;
for (int colindex = 0; colindex < row; colindex++)
{
cin >> square[index][colindex];
}
}
cout << "You listed \n";
for (int index = 0; index < row; index++)
{
for (int colindex = 0; colindex < row; colindex++)
{
cout << setw(2) << square[index][colindex] << " ";
}
cout << endl;
}
match = validateSums(row, square);
cout << match << endl;
}
else
{
cout << "You must enter a # between 1-10." << endl;
}
return 0;
}
bool validateSums(int row, int square[][10])
{
//summation of 1st row
int total = 0,
compareTotal = 0;
for (int colindex = 0; colindex < row; colindex++)
{
total += square[0][colindex];
}
//calculation of sum for rest of rows while comparing to total
for (int index = 1; index < row; index++)
{
for (int colindex = 0; colindex < row; colindex++)
{
compareTotal += square[index][colindex];
}
if (compareTotal != total)
{
cout << "The rows entered do not match." << endl;
break;
return false;
}
else
{
compareTotal = 0;
}
}
//summation of 1st column
total = 0;
for (int index = 0; index < row; index++)
{
total += square[index][0];
}
cout << "Sum of column 1 is " << total << endl;
//calculation of sum for rest of columns while comparing to total
compareTotal = 0;
for (int colindex = 1; colindex < row; colindex++)
{
for (int index = 0; index < row; index++)
{
compareTotal += square[index][colindex];
}
if (compareTotal != total)
{
cout << "The columns entered do not match." << endl;
break;
return false;
}
else
{
compareTotal = 0;
}
}
//summation of 1st diagonal
total = 0;
for (int index = 0; index < row; index++)
{
total += square[index][index];
}
cout << "Sum of diagonal 1 is " << total << endl;
//calculation of sum of the other diagonal
compareTotal = 0;
for (int index = 0; index < row; index++)
{
compareTotal += square[index][row - 1 - index];
}
if (compareTotal != total)
{
cout << "The diagonals entered do not match." << endl;
return false;
}
}
错误消息(从评论中粘贴):
1>------ Build started: Project: ConsoleApplication18, Configuration: Debug Win32 ------
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Sasha\Documents\CS 161\Assignment 4\ConsoleApplication18\Debug\ConsoleApplication18.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
最佳答案
你说
bool validateSums(int, int);
然后
bool validateSums(int row, int square[][10])
这些是不一样的。编译器和链接器试图找到采用 2 个整数的 validateSums 函数。您没有提供 - 因此出现错误消息
不想阅读您所有的代码我不知道您实际需要哪一个:int,int 或 int,[][10]。
关于c++ - 数组程序不能调用bool函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26984452/