-2

I know that this particular error has been brought up a dozen times.However, through all my searching I've failed to find a solution that relates to my specific circumstance.

I am a college student learning C++ coming from Java. I know that it would be better to use Vectors instead of Arrays. I also know that I'm probably understanding pointers wrong and I'm hoping your explanations will help me in that regard.

Okay, here's my three files:

main.cpp

#include <iostream>
#include "Arrays.h"

// Global variables
const int ARRAY_SIZE = 4;
const int MIN_RANGE = 0;
const int MAX_RANGE = 100;

// Main function
int main()
{
    // Create two arrays populated with random numbers
    int* Array1 = createArray(ARRAY_SIZE, MIN_RANGE, MAX_RANGE); // One of these days I'll understand why ponters are so cool
    int* Array2 = createArray(ARRAY_SIZE, MIN_RANGE, MAX_RANGE); // Right now I find them a tad irritating

    // Create the third array
    int* Array3 = mergeArrays(Array1, Array2);

    // Print all arrays with the format "array1[1] + array2[1] = array3[1]"
    for (size_t i = 0; i < ARRAY_SIZE; i++)
    {
        std::cout << Array1[i] << " + " << Array2[i] << " = " << Array3[i] << std::endl << std::endl;
    }

    // Wait for input...
    system("pause");

    return 0;
}

Arrays.h

// Fucntion Prototypes
int* createArray(int arraySize, int minRange, int maxRange); // Creates an array populated with random numbers
int* mergeArrays(int* firstArray, int* secondArray); // Creates an array by adding the associated eements of two arrays
void printHeader(); // Prints the program's header

Arrays.cpp

#include <cstdlib>
#include <ctime>
#include "Arrays.h"

// Function to create an array with n random numbers between 0 and x
int* createArray(int arraySize, int minRange, int maxRange)
{
    srand(time(NULL)); // Generate seed for rand()

    int* Array = new int[arraySize]; // Create a pointer to a new array

    for (size_t i = 0; i < arraySize; i++) // Loop to assign values to each index in the array
    {
        // Generate a random number for each index in the array
        Array[i] = (rand() % maxRange) + minRange;
    }

    return Array;
}

// Function to create an array from two arrays with the associated values added together
int* mergeArrays(int* firstArray, int* secondArray, int arraySize)
{
    int* resultArray = new int[arraySize]; // Create a pointer to a new array

    for (size_t i = 0; i < arraySize; i++) // Loop to assign values to each index in the array
    {
        /* The value at each index in the array is equal to the sum of the values
        at the same index in the other two arrays*/
        resultArray[i] = firstArray[i] + secondArray[i];
    }

    return resultArray;
}

The Error I'm getting when I build it in Visual Studio 2013 is:

1>main.obj : error LNK2019: unresolved external symbol "int * __cdecl mergeArrays(int *,int *)" (?mergeArrays@@YAPAHPAH0@Z) referenced in function _main
1>C:\Assignment 2_1\Debug\Assignment 2_1.exe : fatal error LNK1120: 1 unresolved externals
Nyhilo
  • 56
  • 1
  • 7

1 Answers1

1

You have a difference between declaration and implementation.

int* mergeArrays(int* firstArray, int* secondArray) //.h
int* mergeArrays(int* firstArray, int* secondArray, int arraySize) //.cpp

Your main function uses the function from the .h files, however, this one is never implemented. In the cpp file, you declare a function with the same name, but another signature (overloaded)

Bruce
  • 7,094
  • 1
  • 25
  • 42