shared ptr - C++ conversion from to non-scalar type requested -
i trying overload + operator member function can add 2 polynomial objects (which linked lists) , keep getting error message conversion 'polynomial*' non-scalar type 'std::shared_ptr<polynomial>' requested
don't understand causing this? have declared 2 shared pointers term objects don't understand why can't polynomial object?
polynomial polynomial::operator+( const polynomial &other ) const { shared_ptr<polynomial> result = new polynomial(); shared_ptr<polynomial::term> = this->head; shared_ptr<polynomial::term> b = other.head; double sum = 0; for(a; != nullptr; = a->next) { for(b; b != nullptr; b = b->next) { if(a->exponent == b->exponent) { sum = a->coeff + b->coeff; result->head = shared_ptr<term>(new term( sum, a->exponent, result->head )); cout << "function has found terms" << endl; } else if(a->exponent != b->exponent) { result->head = shared_ptr<term>(new term( a->coeff, a->exponent, result->head )); cout << "function working" << endl; } } } result; }
main.cpp
void makepolynomials( shared_ptr [], int &x );
int main() { shared_ptr<polynomial> poly[ 100 ]; int npolynomials = 0; makepolynomials( poly, npolynomials ); makepolynomials( poly, npolynomials ); (int j=0; j < npolynomials; j++) cout << *(poly[j]); //shows addition operator works polynomial c; c = *(poly[0])+*(poly[1]); cout << c << endl; } void makepolynomials( shared_ptr<polynomial> poly[], int &npolynomials ) { char filename[20]; cout << "enter filename: "; cin >> filename; ifstream infile; infile.open( filename ); if (! infile.is_open()) { cerr << "error: not open file " << filename << endl; exit(1); } string polynom; while (getline( infile, polynom )) { poly[ npolynomials ] = shared_ptr<polynomial>(new polynomial( polynom )); npolynomials++; } }
here you're requesting implicit conversion polynomial*
std::shared_ptr
:
shared_ptr<polynomial> result = new polynomial();
but since std::shared_ptr
constructor explicit
, conversion isn't possible.
you need pass polynomial*
explicitly std::shared_ptr
constructor:
std::shared_ptr<polynomial> result(new polynomial);
or using equivalent new uniform initialization syntax:
std::shared_ptr<polynomial> result{new polynomial};
Comments
Post a Comment