Search code examples
arraysintconstantsvoidfill

How to fix error when adding array functions after main?


#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
void FillArray (int x[50], const int Size);
void PrintArray (int x[50], const int Size);

int main()
{
    const int SizeArray = 10;
    int A[SizeArray] = {0};
    FillArray (A, SizeArray);
    PrintArray (A, SizeArray);

    return 0;
}

void FillArray (int x[50], const int Size)
{
for (int i = 0; i < Size; i++)
    {
        cout << endl << "Please enter an integer: ";
        cin >> x[i];
    }
}

void PrintArray (int x[50], const int Size)
{
for (int i = 0; i < Size; i++)
{
    cout << endl << x[i];
}
}

The error I get is below. I have to create new functions that can read and print arrays respectively. The above is my main function followed by the read (fill) array. It won't run though.

1>------ Build started: Project: Some, Configuration: Debug Win32 ------ 1> Some.cpp 1>Some.obj : error LNK2019: unresolved external symbol "void __cdecl PrintArray(int * const,int)" (?PrintArray@@YAXQAHH@Z) referenced in function _main 1>C:\Users\GmxTrey\Documents\Visual Studio 2010\Projects\Some\Debug\Some.exe : fatal error >LNK1120: 1 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Solution

  • It looks like your FillArray is declared and defined, but PrintArray is only declared, but not defined; this is why the linker complains. You need to provide a definition of PrintArray to address this problem.