I have implemented a class of polynomials (and matrices). I checked the performance of all the methods in a separate sandbox project. After that, I split the class into 2 files. .h and .cpp. I tried to use the methods of the class and it turned out that the lnk2019 error occurs in each one. Earlier, I was able to solve a similar error by adding before the class description (inside .h)lines:
template<typename P> class polynomial;
template<typename P> std::ostream& operator<<(std::ostream& out, const polynomial<P>& plnm);
template<typename P> std::istream& operator>> (std::istream& in, polynomial<P>& plnm);
Adding the lines above did not fix the error.
content .h
#include<iostream>
#include <sstream>
template<typename P> class polynomial;
template<typename P> std::ostream& operator<<(std::ostream& out, const polynomial<P>& plnm);
template<typename P> std::istream& operator>> (std::istream& in, polynomial<P>& plnm);
template<typename P> class polynomial
{
public:
P* ptr;
uint64_t deg;
polynomial() :polynomial(0) {};
polynomial(uint64_t size);
~polynomial();
polynomial<P> operator+(const polynomial<P>& other)const;//addition of polynomials
polynomial<P> operator-(const polynomial<P>& other)const;
polynomial<P> operator/(const polynomial<P>& other)const;//binary division of a polynomial by a polynomial
polynomial<P> operator%(const polynomial<P>& other)const;//the remainder of the division of a polynomial by a polynomial
polynomial<P> operator*(const polynomial<P>& other)const;//binary polynomial multiplication
polynomial<P> operator*(const P& other)const; //binary polynomial multiplication by an element from the field
friend std::ostream& operator<<<>(std::ostream& out, const polynomial<P>& plnm); // overloading the output operator
friend std::istream& operator>><>(std::istream& in, polynomial<P>& plnm); // overloading the input operator
polynomial<P> operator>>(const uint64_t power)const;//coefficient shift (increase). or (plnm*x^n)
polynomial<P> operator<<(const uint64_t power)const;//coefficient shift(decrease). or the whole part of (plnm* x^(-n))
//P& operator[](uint64_t index);//the access operator to the polynomial coefficient
polynomial<P>& operator=(const polynomial<P>& other);
bool operator==(const polynomial<P>& other)const;//
bool operator!=(const polynomial<P>& other)const;
};
content main
int main() {
polynomial<double> a(2);
std::cin >> a;
std::cout << "a = n" <<a;
system("pause");
return 0;
}
an error occurs with each method, so for brevity, I wrote the simplest option in the main one.
part of the contents of the .cpp file
template<typename P>polynomial<P> polynomial<P>::operator>>(const uint64_t power)const
{
polynomial result(deg + power);
for (uint64_t i = 0; i < deg; i++)
{
result.ptr[i + power] = ptr[i];
}
return result;
}
template<typename P>polynomial<P> polynomial<P>::operator<<(const uint64_t power)const
{
polynomial result(deg - power);
for (uint64_t i = 0; i <= (deg - power); i++)
{
result.ptr[i] = ptr[i + power];
}
return result;
}
I assume that the problem may be related to the fact that the polynomial<T> that I declare in the main is not considered a template. This was the case with the operators >> and <<. But I fixed it with three lines, but the compiler seems to think differently.