I am building a DLL project in Visual Studio 2019.
I converted the array library I wrote to a DLL and it doesn't work.
There are two compile errors:
C2254: Say that the function is defined twice.
C2953: Say that the array class has already been declared.
The C2254 error was fixed by defining the function at the same time as the function declaration in the array class declaration. So the code is:
//ArrayLib.h
#ifdef ARRAYLIB_EXPORTS
#define ARRAYLIB_API __declspec(dllexport)
#else
#define ARRAYLIB_API __declspec(dllimport)
#endif
typedef signed long int Long;
template <typename T>
class Array {
public:
Array(Long capacity = 256) {
this->front = new T[capacity];
this->capacity = capacity;
this->length = 0;
}
Array(const Array& source) {
this->front = new T[source.capacity];
this->capacity = source.capacity;
Long i = 0;
while (i < source.length) {
this->front[i] = source.front[i];
i++;
}
this->length = source.length;
}
~Array() {
if (this->front != 0) {
delete[] this->front;
}
}
Long Store(Long index, T object) {
this->front[index] = object;
this->length++;
return index;
}
Long Insert(Long index, T object) {
T(*objects);
Long i;
Long j = 0;
objects = new T[this->capacity + 1];
i = 0;
while (i < index) {
objects[j] = this->front[i];
j++;
i++;
}
j++;
while (i < this->length) {
objects[j] = this->front[i];
j++;
i++;
}
if (this->front != 0) {
delete[] this->front;
}
this->front = objects;
this->capacity++;
this->front[index] = object;
this->length++;
return index;
}
Long AppendFromFront(T object) {
Long index = 0;
T(*objects);
Long i = 0;
objects = new T[this->capacity + 1];
while (i < this->length) {
objects[i + 1] = this->front[i];
i++;
}
if (this->front != 0) {
delete[] this->front;
}
this->front = objects;
this->capacity++;
this->front[index] = object;
this->length++;
return index;
}
Long AppendFromRear(T object) {
Long index;
T(*objects);
Long i = 0;
objects = new T[this->capacity + 1];
while (i < this->length) {
objects[i] = this->front[i];
i++;
}
if (this->front != 0) {
delete[] this->front;
}
this->front = objects;
this->capacity++;
index = this->capacity - 1;
this->front[index] = object;
this->length++;
return index;
}
Long Delete(Long index) {
T(*objects) = 0;
Long i = 0;
if (this->capacity > 1) {
objects = new T[this->capacity - 1];
}
while (i < index) {
objects[i] = this->front[i];
i++;
}
i = index + 1;
while (i < this->length) {
objects[i - 1] = this->front[i];
i++;
}
if (this->front != 0) {
delete[] this->front;
this->front = 0;
}
if (this->capacity > 1) {
this->front = objects;
}
this->capacity--;
this->length--;
index = -1;
return index;
}
Long DeleteFromFront() {
T(*objects) = 0;
Long i = 1;
if (this->capacity > 1) {
objects = new T[this->capacity - 1];
}
while (i < this->length) {
objects[i - 1] = this->front[i];
i++;
}
if (this->front != 0) {
delete[] this->front;
this->front = 0;
}
if (this->capacity > 1) {
this->front = objects;
}
this->capacity--;
this->length--;
return -1;
}
Long DeleteFromRear() {
T(*objects) = 0;
Long i = 0;
if (this->capacity > 1)
{
objects = new T[this->capacity - 1];
}
while (i < this->length - 1)
{
objects[i] = this->front[i];
i++;
}
if (this->capacity > 1)
{
this->front = objects;
}
this->capacity--;
this->length--;
return -1;
}
void Clear() {
if (this->front != 0)
{
delete[] this->front;
this->front = 0;
}
this->capacity = 0;
this->length = 0;
}
Long Modify(Long index, T object) {
this->front[index] = object;
return index;
}
Long LinearSearchUnique(void *key, int(*compare)(void*, void*)) {
Long index = -1;
Long i = 0;
while (i < this->length && compare(this->front + i, key) != 0)
{
i++;
}
if (i < this->length)
{
index = i;
}
return index;
}
void LinearSearchDuplicate(void *key, Long* (*indexes), Long *count, int(*compare)(void*, void*)) {
Long i = 0;
Long j = 0;
*count = 0;
*indexes = new Long[this->length];
while (i < this->length)
{
if (compare(this->front + i, key) == 0)
{
(*indexes)[j] = i;
j++;
(*count)++;
}
i++;
}
}
Long BinarySearchUnique(void *key, int(*compare)(void*, void*)) {
Long index = -1;
Long first = 0;
Long last;
Long mid;
last = this->length - 1;
mid = (first + last) / 2;
while (first <= last && compare(this->front + mid, key) != 0)
{
if (compare(this->front + mid, key) < 0)
{
first = mid + 1;
}
else
{
last = mid - 1;
}
mid = (first + last) / 2;
}
if (first <= last)
{
index = mid;
}
return index;
}
void BinarySearchDuplicate(void *key, Long* (*indexes), Long *count, int(*compare)(void*, void*)) {
Long first = 0;
Long last;
Long mid;
Long i;
Long j = 0;
*count = 0;
*indexes = new Long[this->length];
last = this->length - 1;
mid = (first + last) / 2;
while (first <= last && compare(this->front + mid, key) != 0) {
if (compare(this->front + mid, key) < 0) {
first = mid + 1;
}
else {
last = mid - 1;
}
mid = (first + last) / 2;
}
i = mid - 1;
while (i >= first && compare(this->front + i, key) == 0) {
i--;
}
first = i + 1;
i = first;
while (i <= last && compare(this->front + i, key) == 0) {
(*indexes)[j] = i;
j++;
(*count)++;
i++;
}
}
void BubbleSort(int(*compare)(void*, void*)) {
Long i = 0;
Long j;
T temp;
Long check = 1;
while (i < this->length - 1 && check == 1) {
check = 0;
j = 0;
while (j < this->length - i - 1) {
if (compare(this->front + j, this->front + (j + 1)) > 0) {
temp = this->front[j];
this->front[j] = this->front[j + 1];
this->front[j + 1] = temp;
check = 1;
}
j++;
}
i++;
}
}
void SelectionSort(int(*compare)(void*, void*)) {
Long i = 0;
Long j;
T min;
Long minIndex;
while (i < this->length - 1) {
minIndex = i;
min = this->front[minIndex];
j = i + 1;
while (j < this->length) {
if (compare(&min, this->front + j) > 0) {
minIndex = j;
min = this->front[minIndex];
}
j++;
}
this->front[minIndex] = this->front[i];
this->front[i] = min;
i++;
}
}
void InsertionSort(int(*compare)(void*, void*)) {
Long i = 1;
Long j;
T temp;
while (i < this->length) {
temp = this->front[i];
j = i - 1;
while (j >= 0 && compare(this->front + j, &temp) > 0) {
this->front[j + 1] = this->front[j];
j--;
}
this->front[j + 1] = temp;
i++;
}
}
void Merge(const Array& one, const Array& other, int(*compare)(void*, void*)) {
Long i = 0;
Long j = 0;
Long k = 0;
if (this->front != 0) {
delete[] this->front;
}
this->front = new T[one.length + other.length];
this->capacity = one.length + other.length;
this->length = 0;
while (i < one.length && j < other.length) {
if (compare(one.front + i, other.front + j) < 0) {
this->front[k] = one.front[i];
k++;
this->length++;
i++;
}
else {
this->front[k] = other.front[j];
k++;
this->length++;
j++;
}
}
while (i < one.length) {
this->front[k] = one.front[i];
k++;
this->length++;
i++;
}
while (j < other.length) {
this->front[k] = other.front[j];
k++;
this->length++;
j++;
}
}
T& GetAt(Long index) {
return this->front[index];
}
T& operator [](Long index) {
return this->front[index];
}
T* operator +(Long index) {
return (this->front + index);
}
Array& operator =(const Array& source) {
if (this->front != 0) {
delete[] this->front;
}
this->front = new T[source.capacity];
this->capacity = source.capacity;
Long i = 0;
while (i < source.length) {
this->front[i] = source.front[i];
i++;
}
this->length = source.length;
return *this;
}
// 20160512 추가
void Swap(Long toIndex, Long fromIndex) {
T object = this->front[fromIndex];
Long count;
if (fromIndex > toIndex) {
count = fromIndex - toIndex;
memmove(this->front + toIndex + 1, this->front + toIndex, sizeof(T) * count);
}
else {
count = toIndex - fromIndex;
memmove(this->front + fromIndex, this->front + fromIndex + 1, sizeof(T) * count);
}
this->front[toIndex] = object;
}
Long GetCapacity() const {
return this->capacity;
}
Long GetLength() const {
return this->length;
}
private:
T(*front);
Long capacity;
Long length;
};
extern ARRAYLIB_API int nArrayLib;
ARRAYLIB_API int fnArrayLib(void);
Here is cpp file :
#include "ArrayLib.h"
ARRAYLIB_API int nArrayLib=0;
ARRAYLIB_API int fnArrayLib(void)
{
return 0;
}
However, I am not sure how to fix the C2953 error.
Is there a way?
Or did I make another mistake?
I'm building a DLL for the first time.
I don't even know why the nArrayLib and fnArrayLib exist.
And what makes me more confused is that no errors occur in simple console application or application that created using GUI builders.
The error described above occurred in my notepad application using MFC.
Thanks for the help.