Пытаюсь сделать библиотеку полиномов. В консоли пользователя выдает ошибки. Помогите исправить, я в этом совсем не разбираюсь. Еще выдает ошибку: отсутствует оператор << соответствующий этим операндам. ( подчеркивает -, +, *, << и getDerivative ).
Тут сами ошибки.
#include "MathLibrary.h"
#include <iostream>
#include <string>
#pragma comment(lib, "MathLibrary.lib")
using namespace std;
int main() {
Polynomial<int> a{ 3, 1, 1 };
Polynomial<int> b{ 1, 5, 1 };
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "a * b = " << a * b << endl;
cout << "Derivative of a = " << getDerivative(a) << endl;
return 0;
}
Уже библиотека. Файл h.
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif
#include <iostream>
#include <utility>
#include <iomanip>
#include <string>
template<typename T>
class Polynomial {
public:
using Self = Polynomial<T>;
Polynomial() : size{ 1 }, coefficients(new T[1]{}) {}
explicit Polynomial(std::size_t size) : size{ size }, coefficients(new T[size]{}) {}
Polynomial(const std::initializer_list<T>& coefficients) : size{ coefficients.size() }, coefficients{ new T[size] } {
std::copy(coefficients.begin(), coefficients.end(), Polynomial::coefficients);
}
Polynomial(const Self& o) : size{ o.size }, coefficients(new T[size]{}) {
std::copy(o.coefficients, o.coefficients + o.size, coefficients);
}
Self& operator=(const Self& o) {
if (this != &o) {
size = o.size;
if (coefficients != o.coefficients) {
delete[] coefficients;
coefficients = new T[size]{};
std::copy(o.coefficients, o.coefficients + o.size, coefficients);
}
}
return *this;
}
Polynomial(Self&& o) noexcept : size{ std::move(o.size) }, coefficients{ std::exchange(o.coefficients, nullptr) } {}
Self& operator=(Self&& o) noexcept {
if (this != &o) {
size = std::move(o.size);
if (coefficients != o.coefficients) {
coefficients = std::exchange(o.coefficients, nullptr);
}
}
return *this;
}
Self& operator+=(const Self& o) {
if (this == &o) {
return *this;
}
if (o.size > size) {
T* extended = new T[o.size]{};
std::copy(coefficients, coefficients + size, extended);
delete[] coefficients;
coefficients = extended;
size = o.size;
}
for (std::size_t i = 0; i < o.size; ++i) {
coefficients[i] += o.coefficients[i];
}
return *this;
}
virtual ~Polynomial() {
delete[] coefficients;
}
std::size_t getSize() const {
return size;
}
T& operator[](std::size_t i) {
return coefficients[i];
}
const T& operator[](std::size_t i) const {
return coefficients[i];
}
private:
std::size_t size;
T* coefficients;
};
Файл cpp.
// dllmain.cpp : Определяет точку входа для приложения DLL.
#include "pch.h" // use stdafx.h in Visual Studio 2017 and earlier
#include <utility>
#include <limits.h>
#include "MathLibrary.h"
#include <string>
BOOL APIENTRY MathLibraryMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
template<typename T>
std::ostream& operator<<(std::ostream& out, const Polynomial<T>& p) {
for (std::size_t i = p.getSize(); i > 0; --i) {
const T& value = p[i - 1];
if (value >= 0) {
out << "+";
}
out << std::fixed << std::setprecision(2) << value;
if (i == 2) {
out << "x";
}
else if (i > 1) {
out << "x^" << (i - 1);
}
}
return out;
}
template<typename T>
Polynomial<T> operator+(const Polynomial<T>& a, const Polynomial<T>& b) {
if (a.getSize() < b.getSize()) {
return operator+(b, a);
}
Polynomial<T> result{ a };
for (std::size_t i = 0; i < b.getSize(); ++i) {
result[i] += b[i];
}
return result;
}
template<typename T>
Polynomial<T> operator-(const Polynomial<T>& a, const Polynomial<T>& b) {
if (a.getSize() < b.getSize()) {
return operator+(b, a);
}
Polynomial<T> result{ a };
for (std::size_t i = 0; i < b.getSize(); ++i) {
result[i] -= b[i];
}
return result;
}
template<typename T>
Polynomial<T> multiply(const Polynomial<T>& p, const T& coefficient, std::size_t power) {
Polynomial<T> result(p.getSize() + power);
for (std::size_t i = 0; i < p.getSize(); ++i) {
result[i + power] = p[i] * coefficient;
}
return result;
}
template<typename T>
Polynomial<T> operator(const Polynomial<T>& a, const Polynomial<T>& b) {
if (b.getSize() < a.getSize()) {
return operator(b, a);
}
Polynomial<T> result(a.getSize() + b.getSize() - 1);
for (std::size_t i = 0; i < a.getSize(); ++i) {
result += multiply(b, a[i], i);
}
return result;
}
template<typename T>
Polynomial<T> getDerivative(const Polynomial<T>& p) {
if (p.getSize() == 1) {
return Polynomial<T>{0};
}
Polynomial<T> result(p.getSize() - 1);
for (std::size_t i = 0; i < result.getSize(); ++i) {
result[i] = p[i + 1] * (i + 1);
}
return result;
}