Windows 11, Visual Studio 2022, C++
This is my first attempt at a template. It will compare two arrays of type uint8_t, uint16_t, uint32_t, or uint64_t. And maybe others I have yet to considered.
Here is the h file:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#pragma once
#include <stdint.h>
enum compare_enum
{
data_error,
equal_values,
first_is_larger,
second_is_larger
};
template<typename TYPE>
compare_enum compare_arrays( TYPE *a, TYPE *b, size_t compare_count );
|
Here is the code file:
1 2 3 4 5 6 7 8 9
|
// compare_arrays_template.cpp
// A function to compare arrays and detect differences
#include "compare_arrays_template.h"
compare_enum compare_arrays( uint8_t *a, uint8_t *b, size_t compare_count )
{
return data_error;
}
|
And here is how it will be called in a console app that does little else
1 2 3 4 5 6 7 8
|
cout << " compare two arrays " << endl;
const size_t array_length = 8;
uint8_t first[ array_length ] = { 1, 2, 3, 4, 9, 6, 7, 8 };
uint8_t second[ array_length ] = { 1, 2, 3, 4, 5, 6, 7, 8 };
compare_enum status;
status = compare_arrays( first, second, array_length );
|
The code to do the compare and check the status will be added once this is at the point of compiling.
In the h file, VS squiggles “compare_arrays” and notes: “Function definition for compare_arrays not found.” That is not correct, because there it is.
The build error is:
1> compare_arrays.obj : error LNK2019: unresolved external symbol "enum compare_enum __cdecl compare_arrays<unsigned char>(unsigned char *,unsigned char *,unsigned int)" (??$compare_arrays@E@@YA?AW4compare_enum@@PAE0I@Z) referenced in function _main
1>E:\WX\compare_arrays\Debug\compare_arrays.exe : fatal error LNK1120: 1 unresolved externals
The error message appears to indicate it does not like the enum. But an enum is always a constant and its declared just above the function.
I tried a few variations of declaring the enum extern, but probably did not find the right formula.
Please advise as to what I am not recognizing.
Thank you for your time.