簡體   English   中英

使用 bool function 匹配 C++ 中的總和

[英]Using bool function for matching sums in C++

我編寫了這個程序,該程序應該解決數字從 1 到 11 的輪子,但我無法找出導致此 Linker 錯誤的原因。 我相信除了使用 integer 數組的 Boolean function 之外,我在代碼主體中的其他所有內容都可以正常工作,但是出現的錯誤如下。 我不確定我在 function 上做錯了什么。 我已經在代碼的開頭使用原型聲明了它,我使用其正確名稱調用 function 並正確調用數組。 有人可以幫我弄清楚代碼有什么問題嗎?

對“matchingSums(int)”的未定義引用……重定位被截斷以適應:R_X86_64_PC32 針對未定義符號

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <time.h>
using namespace std;


const int MAX_CIRCLES = 11;
const int CENTER_CIRCLE_INDEX = 10;
bool matchingSums(int);
void fillTheWheel(int []);
void displayWheelContents(int []);
void randomizeTheContents(int, int []);
int nbrOfItems, firstSum, nextSum;
int main(void)
{

srand(time(NULL));

int wheel[MAX_CIRCLES];

int numInt = 0;
fillTheWheel(wheel);

while (!matchingSums(*wheel))
{

numInt++;

randomizeTheContents(MAX_CIRCLES, wheel);
}
cout << "After " << numInt << " unsuccessful attempts, the following solution was found:" << endl;
displayWheelContents(wheel);
}


void fillTheWheel(int wheel[])
{

for (int fillTheWheelIndex = 0; fillTheWheelIndex < MAX_CIRCLES; fillTheWheelIndex++)
{
wheel[fillTheWheelIndex] = (fillTheWheelIndex + 1);
}
}
void displayWheelContents(int wheel[])
{

cout << "* Outside circles (clockwise from the top):" << endl << " " << endl;

for (int wheelIndex = 0; wheelIndex < MAX_CIRCLES; wheelIndex++)
{
// Print each value in a column width of 4 as shown in the example-program-execution.txt file
cout << setw(4) << wheel[wheelIndex] << " ";
}

cout << " " << endl << " " << endl << "*Center circle: " << wheel[CENTER_CIRCLE_INDEX] << endl;
}

void randomizeTheContents(int nbrOfItems, int table[])
{

for (int indexA = 0; indexA < nbrOfItems; indexA++)
{
int indexB = rand() % nbrOfItems;
int temp = table[indexA];
table[indexA] = table[indexB];
table[indexB] = temp;
}
}

bool matchingSums(int wheel[])
{
const int MAX_OUTER_CIRCLES = MAX_CIRCLES - 1;
const int OPPOSITE_SIDE_FACTOR = 5;
const int STARTING_INDEX = 0;
int firstSum;
int nextSum;
// Calculate the sum of the first pair of numbers
firstSum = wheel[STARTING_INDEX] + wheel[CENTER_CIRCLE_INDEX] 
+ wheel[STARTING_INDEX + OPPOSITE_SIDE_FACTOR];
// Compare the first sum to each of the sums of the other pairs
for (int i = 1; i < MAX_OUTER_CIRCLES/2; i++)
{
nextSum = wheel[i] + wheel[CENTER_CIRCLE_INDEX] + wheel[i + OPPOSITE_SIDE_FACTOR];
if (firstSum != nextSum)
return false; 
} // End for

return true;
} // End matchingSums

最初 function 是用int類型的參數聲明的,例如

bool matchingSums(int);

並在 main 中使用int類型的參數調用

while (!matchingSums(*wheel))

但是在它的定義中,它是用int []類型的參數聲明的,編譯器隱式地將其調整為int *類型

bool matchingSums(int wheel[])

所以編譯器發出一個錯誤,因為它沒有找到 function 的定義聲明如下

bool matchingSums(int);

matchingsums 的參數是int類型,但 matchingSums function 的定義接受int wheel[] ,一個 integer 類型的無大小數組。

您一定忽略了這一點,因為 function fillTheWheel 接受相同類型的參數,但被聲明和定義得很好。

將原型bool matchingSums(int)替換為bool matchingSums(int [])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM