DjMaxLETU
DjMaxLETU

Reputation: 41

Using bool function for matching sums in C++

I've written this program that's supposed to solve a wheel with numbers from 1 to 11 and I'm having trouble with figuring out what's causing this Linker error. I believe I have everything else working fine in the code body except for this Boolean function that uses an integer array, but the error that comes up says the following. I'm not sure what I've done wrong with the function. I've declared it using a prototype in the beginning of the code, I call the function using its proper name and calling the array correctly. Could someone please help me figure out what's wrong with the code?

undefined reference to 'matchingSums(int)' … relocation truncated to fit: R_X86_64_PC32 against undefined symbol

#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

Upvotes: 0

Views: 216

Answers (2)

sometimesLazy
sometimesLazy

Reputation: 54

The argument of matchingsums is of int type but the definition of the matchingSums function accepts int wheel[], an integer type of unsized array.

You must have overlooked this, since the function fillTheWheel accepts the same type of argument but was declared and defined just fine.

Replace the prototype bool matchingSums(int) to bool matchingSums(int []).

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

Initially the function is declared with a parameter of the type int like

bool matchingSums(int);

And called with an argument of the type int in main

while (!matchingSums(*wheel))

But in its definition it is declared with a parameter of the type int [] that is implicitly adjusted by the compiler to the type int * like

bool matchingSums(int wheel[])

So the compiler issues an error because it did not find the definition of the function declared like

bool matchingSums(int);

Upvotes: 2

Related Questions