Thread: linked lists of a polynomial

  1. #1
    Unregistered
    Guest

    linked lists of a polynomial

    Hi every one, I'm very new in c++ programming, and I'm encounting some difficulties with my program. It's supposed to create a polynomial with a linked list. It keeps giving me errors and I don't know what to do anymore. The polynomial is created with a single nested statement in the constructor. But it doesn't seem to work. Please help me.


    class Term
    {
    public:
    Term (double coefficient, int exponent=0, Term *next=NULL);
    double evaluate (double x);
    void print();
    private:
    double coefficient;
    int exponent;
    Term *next;
    };

    Term::Term(double coefficient, int exponent = 0, Term *next = NULL)
    {
    Term *pt = new Term(1,10, new Term(-3,4,new Term (17)));
    }

    double Term::evaluate (double x);
    {
    if (Term *pt != NULL)
    {
    while(pt->next != NULL)
    {
    int expo = pt->exponent;
    double accum+ = pt->coefficient * pow(x, expo);
    pt=pt->next;
    }
    pt->next = NULL;
    }
    return accum;
    }

    int main()
    {
    Term test;
    double result = test.evaluate (2);
    cout << "and the answer is: " << result << endl;
    return 0;
    }

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Term test;
    double result = test.evaluate (2);//this is where your problem is


    it should be the following

    Term test (2);
    double result=test.evaluate;



    I haven't looked over the rest of your code but check this and come back later if it still doesn't work.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Just a a suggestion - using polynomials in an algorithm will make things pretty complex if you do any circular logic or anything - I would suggest breaking any trinomials into two smaller binomials that co-complement.

  4. #4
    Unregistered
    Guest
    I don't think my problem is in the declaration of my class Term instance. Look further down, and you'll see that the function evaluate does receive a double (x) and return one.
    Here are a couple of the errors I get

    error C2572: 'Term::Term' : redefinition of default parameter : parameter 3
    question2.cpp(8) : see declaration of 'Term::Term'
    'Term::Term' : redefinition of default parameter : parameter 2
    error C2447: missing function header (old-style formal list?)

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    O, sorry, I thought that the double you inputted into evaluate was was u were trying to use to set as the coefficient.

    I am kinda busy right now but I'll try to help u later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  4. linked lists of a polynomial
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2002, 06:44 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM