0

Пытаюсь сделать библиотеку полиномов. В консоли пользователя выдает ошибки. Помогите исправить, я в этом совсем не разбираюсь. Еще выдает ошибку: отсутствует оператор << соответствующий этим операндам. ( подчеркивает -, +, *, << и getDerivative ).

Тут сами ошибки.

#include "MathLibrary.h"
#include <iostream>
#include <string>

#pragma comment(lib, "MathLibrary.lib")

using namespace std;

int main() {

Polynomial&lt;int&gt; a{ 3, 1, 1 };
Polynomial&lt;int&gt; b{ 1, 5, 1 };


cout &lt;&lt; &quot;a = &quot; &lt;&lt; a &lt;&lt; endl;
cout &lt;&lt; &quot;b = &quot; &lt;&lt; b &lt;&lt; endl;
cout &lt;&lt; &quot;a + b = &quot; &lt;&lt; a + b &lt;&lt; endl;
cout &lt;&lt; &quot;a - b = &quot; &lt;&lt; a - b &lt;&lt; endl;
cout &lt;&lt; &quot;a * b = &quot; &lt;&lt; a * b &lt;&lt; endl;
cout &lt;&lt; &quot;Derivative of a = &quot; &lt;&lt; getDerivative(a) &lt;&lt; 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&lt;T&gt;&amp; coefficients) : size{ coefficients.size() }, coefficients{ new T[size] } {
    std::copy(coefficients.begin(), coefficients.end(), Polynomial::coefficients);
}

Polynomial(const Self&amp; o) : size{ o.size }, coefficients(new T[size]{}) {
    std::copy(o.coefficients, o.coefficients + o.size, coefficients);
}

Self&amp; operator=(const Self&amp; o) {
    if (this != &amp;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&amp;&amp; o) noexcept : size{ std::move(o.size) }, coefficients{ std::exchange(o.coefficients, nullptr) } {}

Self&amp; operator=(Self&amp;&amp; o) noexcept {
    if (this != &amp;o) {
        size = std::move(o.size);
        if (coefficients != o.coefficients) {
            coefficients = std::exchange(o.coefficients, nullptr);
        }
    }
    return *this;
}

Self&amp; operator+=(const Self&amp; o) {
    if (this == &amp;o) {
        return *this;
    }
    if (o.size &gt; 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 &lt; o.size; ++i) {
        coefficients[i] += o.coefficients[i];
    }
    return *this; 
}

virtual ~Polynomial() {
    delete[] coefficients;
}

std::size_t getSize() const {
    return size;
}

T&amp; operator[](std::size_t i) {
    return coefficients[i];
}

const T&amp; 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; }

0 Answers0