Ok, people I have a problem with my homework C++ programming code. I will show you the part where it gets tricky (for me...).
-
The representing code is the code from my Base class ICollection.
#pragma once
#ifndef ICollection_h #define ICollection_h #include <functional> #include <memory> template <class T> class ICollection { public: virtual bool addElement(const T&) = 0; virtual int numberOfElements() const = 0; virtual std::shared_ptr<ICollection> searchForElements(const std::function<bool(const T&)>&) const; }; #endif ICollection_h
As all you can see, this is an abstract class. Focus on searchForElements method.
- The next thing is a new header file. In this file Set.h you can see the source code of my derived class Set which is derived from the first Base class ICollection. You can scroll it all down but I want you to focus on the searchForElements definition(redefinition). If you want the ask me something about anything you see here, feel free to ask. Here comes the code:
#pragma once
#ifndef Set_h
#define Set_h
#include "ICollection.h"
template <class T>
class Set : public ICollection<T>
{
protected:
int index, capacity;
T* array{};
public:
Set();
Set(const int);
Set(const int, const T*);
Set(const Set&);
Set(const Set&&) noexcept;
virtual bool addElement(const T&) override;
virtual int numberOfElements() const override;
virtual std::shared_ptr<ICollection<T>> searchForElements(const std::function<bool(const T&)>&) const override;
};
template <class T>
Set<T>::Set() : index(0), capacity(0), array(nullptr) {}
template <class T>
Set<T>::Set(const int number)
{
index = 0;
capacity = (number > 0) ? number : 0;
array = new(T[capacity]);
}
template <class T>
Set<T>::Set(const int number, const T* elements)
{
capacity = number;
array = new(T[number]);
for (index = 0; index < number; index++)
array[index] = elements[index];
}
template <class T>
Set<T>::Set(const Set& other)
{
capacity = other.capacity;
array = new (T[other.capacity]);
for (index = 0; index < other.index; index++)
array[index] = other.array[index];
}
template <class T>
Set<T>::Set(const Set&& other) noexcept
{
index = other.index;
capacity = other.capacity;
array = other.array;
other.index = other.capacity = 0;
other.array = nullptr;
}
template <class T>
bool Set<T>::addElement(const T& element)
{
for (int i = 0; i < this->index; i++)
if (this->array[i] == element)
return false;
if (this->index < this->capacity)
{
this->array[this->index++] = element;
return true;
}
return false;
}
template <class T>
int Set<T>::numberOfElements() const
{
return this->index;
}
template <class T>
std::shared_ptr<ICollection<T>> Set<T>::searchForElements(const std::function<bool(const T&)>& doSomethnig) const
{
int eCounter = 0; // Element counter.
for (int i = 0; i < this->index; i++)
if (doSomethnig(this->array[i]))
eCounter++;
if (!eCounter)
{
std::shared_ptr<Set> ptr = std::make_shared<Set> (eCounter);
return ptr;
}
T* arrayE = new(T[eCounter]);
for (int i = 0; i < this->index; i++)
arrayE[i] = this->array[i];
std::shared_ptr<Set> ptr = std::make_shared<Set> (eCounter, arrayE);
return ptr;
}
- Ok and we have my main.cpp file:
#include #include "Set.h"
int main()
{
int array1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
ICollection<int>* ptr = new Set<int>(10, array1);
ptr->searchForElements([](const int& X) {return X % 2; });
return 0;
}
- When a press the F5 and try to run the code in Microsoft Visual Studio I get an LNK1120 and LNK2001 error and those are the linker errors. Please can somebody explain me what did I do wrong ? I what to know why I get those errors and how to fix them.
Aucun commentaire:
Enregistrer un commentaire