Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A programme that doesn't get compiled in C++

  1. Jul 18, 2015 #1

    MathematicalPhysicist

    User Avatar
    Gold Member

    I have the following code for operations on polynomials in C++, I get plenty of errors which I want to rectify, here's the code:
    Code (Text):

    #include<stdio.h>
    #include<math.h>
    template<class T> class list{
    protected:
    int number;
    T** item;
    public:
    list(int n=0):number(n), item(n ? new T*[n]:0){
    } // constructor
    list(int n, const T&t)
    : number(n), item(n ? new T*[n] : 0){
    for(int i=0; i<number; i++)
    item[i] = new T(t);
    } // constructor with T argument
    list(const list<T>&);
    const list<T>& operator=(const list<T>&);
    template<class T> class polynomial:public list<T>{
    public:
    polynomial(int n=0){
    number = n;
    item  =  n  ?  new  T*[n] : 0;
    for(int i=0; i<n; i++)
    item[i] = 0;
    } // constructor
    polynomial(int n, const T&a){
    number = n;
    item  =  n  ?  new  T*[n] : 0;
    for(int i=0; i<n; i++)
    item[i] = new T(a);
    } // constructor with ’T’ argument
    template<class T>
    const polynomial<T>&
    operator+=(polynomial<T>& p, const polynomial<T>&q){
    if(p.size() >= q.size())
    for(int i=0; i<q.size(); i++)
    p(i) += q[i];
    else{
    polynomial<T> keepQ = q;
    p = keepQ += p;
    }
    return p;
    } // add polynomial
    template<class T>
    const polynomial<T>
    operator+(const polynomial<T>& p,
    const polynomial<T>&q){
    polynomial<T> keep = p;
    return keep += q;
    } // add two polynomials
    template<class T>
    const polynomial<T>&
    operator*=(polynomial<T>& p, const T&a){
    for(int i=0; i<p.size(); i++)
    p(i) *= a;
    return p;
    } // multiplication by scalar
    template<class T>
    const polynomial<T>
    operator*(const T&a, const polynomial<T>&p){
    polynomial<T> keep = p;
    return keep *= a;
    } // scalar times polynomial
    template<class T>
    polynomial<T>
    operator*(const polynomial<T>&p,const polynomial<T>&q){
    polynomial<T> result(p.degree()+q.degree()+1,0);
    for(int i=0; i<result.size(); i++)
    for(int j=max(0,i-q.degree());
    j<=min(i,p.degree()); j++){
    if(j == max(0,i-q.degree()))
    result(i) = p[j] * q[i-j];
    else
    result(i) += p[j] * q[i-j];
    }
    return result;
    } // multiply two polynomials
    template<class T>
    polynomial<T>&
    operator*=(polynomial<T>&p, const polynomial<T>&q){
    return  p  =  p  *  q;
    } // multiply by polynomial
    int main(){
    polynomial<double> p(3,1);
    print(p * p);
    return 0;
    }
     
    I get an error with "declaration of 'class <T>'", I get more errors, but first this one, how to fix this?
    I just copied this code from a book on solving pdes with c++, I added the math library since I thought that it will include the max and min functions.
     
  2. jcsd
  3. Jul 18, 2015 #2

    MathematicalPhysicist

    User Avatar
    Gold Member

    BTW, I have compiled it in dev C++ 5.11.
     
  4. Jul 18, 2015 #3

    wle

    User Avatar

    Is that really how the code appears in your book?

    Cleaning up the formatting and putting }; to end the class declarations gets you this:

    Code (C++):
    #include<stdio.h>
    #include<math.h>

    template<class T> class list {
    protected:
        int number;
        T** item;
    public:
        list(int n=0):number(n), item(n ? new T*[n]: 0) { }

        list(int n, const T& t) : number(n), item(n ? new T*[n] : 0) {
            for(int i=0; i<number; i++)
                item[i] = new T(t);
        }
        list(const list<T>&);
        const list<T>& operator=(const list<T>&);
    };

    template<class T> class polynomial : public list<T> {
    public:
        polynomial(int n=0) {
            number = n;
            item = n ? new T*[n] : 0;
            for(int i=0; i<n; i++)
                item[i] = 0;
        }

        polynomial(int n, const T& a) {
            number = n;
            item = n ? new T*[n] : 0;

            for(int i = 0; i < n; i++)
                item[i] = new T(a);
        }
    };

    template<class T>
    const polynomial<T>& operator+=(polynomial<T>& p, const polynomial<T>& q)
    {
        if (p.size() >= q.size())
            for(int i = 0; i < q.size(); i++)
                p(i) += q[i];
        else {
            polynomial<T> keepQ = q;
            p = keepQ += p;
        }
        return p;
    }

    template<class T>
    const polynomial<T> operator+(const polynomial<T>& p, const polynomial<T>& q)
    {
        polynomial<T> keep = p;
        return keep += q;
    }

    template<class T>
    const polynomial<T>& operator*=(polynomial<T>& p, const T& a)
    {
        for(int i = 0; i < p.size(); i++)
            p(i) *= a;
        return p;
    }

    template<class T>
    const polynomial<T> operator*(const T& a, const polynomial<T>& p)
    {
        polynomial<T> keep = p;
        return keep *= a;
    }

    template<class T>
    polynomial<T> operator*(const polynomial<T>& p, const polynomial<T>& q)
    {
        polynomial<T> result(p.degree() + q.degree() + 1, 0);
        for (int i = 0; i < result.size(); i++)
            for (int j = max(0, i - q.degree()); j <= min(i, p.degree()); j++) {
                if (j == max(0, i - q.degree()))
                    result(i) = p[j] * q[i-j];
                else
                    result(i) += p[j] * q[i-j];
            }
        return result;
    }

    template<class T>
    polynomial<T>& operator*=(polynomial<T>& p, const polynomial<T>& q) {
        return p = p * q;
    }

    int main()
    {
        polynomial<double> p(3, 1);
        print(p * p);
        return 0;
    }
    I also deleted the comments since they're a bit pointless. E.g., overloading operator+() and such is already pretty self explanatory.

    There's still lots of problems with this code (e.g. code tries to use functions, methods, and overloaded operators that aren't defined, nontrivial inlined constructors, looks like it leaks memory, unnecessary complexity, there's already a list and other containers in STL, and the resident C++ experts here can probably add to this list; basically: yuck).


    That's in <algorithm>.
     
    Last edited: Jul 18, 2015
  5. Jul 18, 2015 #4

    MathematicalPhysicist

    User Avatar
    Gold Member

    @wle ,It doesn't appear like that in the book.

    The operations and definitions of polynomial appear scattered in the book, and not as a whole, it appears on pages: 162-166 and the code for 'list' appears on page 124 in the following text:
    Solving PDEs in C++

    I know how to write a max and min functions, but I thought that are already included in some library in C++, to write these functions all the time will be a pitty, how can I save it in a library?

    Appreciate your input here.
     
    Last edited by a moderator: Jul 21, 2015
  6. Jul 18, 2015 #5

    wle

    User Avatar

    Then you're getting errors because you didn't copy the full implementation. You left out code for list on pages 124 and 125 and you'll also need the print() function at the top of page 126. You also left out part of the declaration of polynomial on page 163.


    They are, just not in math.h. They're defined in the algorithm header (#include <algorithm.h>, if you just want it to work as is).
     
    Last edited by a moderator: Jul 21, 2015
  7. Jul 18, 2015 #6

    MathematicalPhysicist

    User Avatar
    Gold Member

    @wle I've added the syntax you told me to add, but I till get an error "stray '\230' in program" on lines 18 and 83 and other errors.

    I am writing the corrected code, in case you can spot my errors:
    Code (Text):

    #include<stdio.h>
    #include<math.h>
    #include <algorithm>
    template<class T> class list {
    protected:
        int number;
        T** item;
    public:
        list(int n=0):number(n), item(n ? new T*[n]: 0) { }

        list(int n, const T& t) : number(n), item(n ? new T*[n] : 0) {
            for(int i=0; i<number; i++)
                item[i] = new T(t);
        }
        list(const list<T>&);
        const list<T>& operator=(const list<T>&);
    }
    ˜list(){
    for(int i=0; i<number; i++)
    delete item[i];
    delete [] item;
    }
    int size() const{
    return number;
    }
    T& operator()(int i){
    if(item[i])return *(item[i]);
    }
    const T& operator[](int i)const{
    if(item[i])return *(item[i]);
    }
    };
    template<class T>
    list<T>::list(const list<T>&l):number(l.number),
    item(l.number ? new T*[l.number] : 0){
    for(int i=0; i<l.number; i++)
    if(l.item[i]) item[i] = new T(*l.item[i]);
    }
    template<class T>
    const list<T>&
    list<T>::operator=(const list<T>& l){
    if(this != &l){
    if(number > l.number)
    delete [] (item + l.number);
    if(number < l.number){
    delete [] item;
    item = new T*[l.number];
    }
    for(int  i  =  0;  i  <  l.number; i++)
    if(l.item[i]) item[i] = new T(*l.item[i]);
    number = l.number;
    }
    return *this;
    }
    template<class T>
    void print(const list<T>&l){
    for(int i=0; i<l.size(); i++){
    printf("i=%d:\n",i);
    print(l[i]);
    }
    }




    template<class T> class polynomial : public list<T> {
    public:
        polynomial(int n=0) {
            number = n;
            item = n ? new T*[n] : 0;
            for(int i=0; i<n; i++)
                item[i] = 0;
        }

        polynomial(int n, const T& a) {
            number = n;
            item = n ? new T*[n] : 0;

            for(int i = 0; i < n; i++)
                item[i] = new T(a);
        }
    };
    ˜polynomial(){
    }
    int degree() const{
    return number-1;
    }
    };


    template<class T>
    const polynomial<T>& operator+=(polynomial<T>& p, const polynomial<T>& q)
    {
        if (p.size() >= q.size())
            for(int i = 0; i < q.size(); i++)
                p(i) += q[i];
        else {
            polynomial<T> keepQ = q;
            p = keepQ += p;
        }
        return p;
    }

    template<class T>
    const polynomial<T> operator+(const polynomial<T>& p, const polynomial<T>& q)
    {
        polynomial<T> keep = p;
        return keep += q;
    }

    template<class T>
    const polynomial<T>& operator*=(polynomial<T>& p, const T& a)
    {
        for(int i = 0; i < p.size(); i++)
            p(i) *= a;
        return p;
    }

    template<class T>
    const polynomial<T> operator*(const T& a, const polynomial<T>& p)
    {
        polynomial<T> keep = p;
        return keep *= a;
    }

    template<class T>
    polynomial<T> operator*(const polynomial<T>& p, const polynomial<T>& q)
    {
        polynomial<T> result(p.degree() + q.degree() + 1, 0);
        for (int i = 0; i < result.size(); i++)
            for (int j = max(0, i - q.degree()); j <= min(i, p.degree()); j++) {
                if (j == max(0, i - q.degree()))
                    result(i) = p[j] * q[i-j];
                else
                    result(i) += p[j] * q[i-j];
            }
        return result;
    }

    template<class T>
    polynomial<T>& operator*=(polynomial<T>& p, const polynomial<T>& q) {
        return p = p * q;
    }

    int main()
    {
        polynomial<double> p(3, 1);
        print(p * p);
        return 0;
    }
     
    Thanks in advance!!!
     
  8. Jul 18, 2015 #7

    jtbell

    User Avatar

    Staff: Mentor

    Those two lines should be
    Code (Text):

    ~list(){
    ~polynomial(){
     
    Note the "centered" tilde instead of your "raised" tilde. Your tilde might be part of your OS's mechanism for producing letters like ñ. My tilde is a "standalone" one. On my Macintosh US keyboard it's on the top left key (shifted). Your keyboard is probably different.
     
  9. Jul 18, 2015 #8

    MathematicalPhysicist

    User Avatar
    Gold Member

    @jtbell thanks.

    I still get some errors, such as the following:
    on line 18, "expected class-name before '(' token .
    on line 23, non-member function 'int size()' cannot have cv-qualifier .
    on line 24, 'number' was not declared in this scope .
    on lines 26 and 29 'T' doesn't name a type .
    on line 32, expected declaration before '}' token.

    How to fix these errors?
    Thanks in advance!!
     
  10. Jul 18, 2015 #9

    Borg

    User Avatar
    Science Advisor
    Gold Member

    I haven't worked with C++ in years but here are some of my observations.

    The first error in a stack trace is often the source of following errors. For example, are you sure that the tilde on line 18 is legal before the list() method? (I don't know) The compiler seems to think that that line is something other than a method. Is 'list' a C++ keyword?

    Scope errors can be due to missing or misplaced brackets. When you don't format all of your code the way that wle showed, it makes it very difficult to see obvious errors. In this particular case, it could also be due to the compiler's confusion at line 18. Fix that line and try recompiling.

    Also, while you can avoid putting brackets after a for loop when there is only one line, it is not good practice. It's too easy to mistakenly see an ending bracket for something else and think that it's part of your for loop when it isn't. Just one more thing to make debugging a chore.
     
    Last edited: Jul 18, 2015
  11. Jul 18, 2015 #10

    MathematicalPhysicist

    User Avatar
    Gold Member

    @Borg I corrected the tildes, the former tildes were copied from the ebook. I hope you or someone else can help me with my other errors.

    It was sure easy if the code were written as a whole instead of bits of pieces, but that's how it is.
     
  12. Jul 18, 2015 #11

    jtbell

    User Avatar

    Staff: Mentor

    In C++, a class's destructor has the name of the class, prefixed by a tilde. (Its constructor simply has the name of the class.)
     
  13. Jul 18, 2015 #12

    jtbell

    User Avatar

    Staff: Mentor

    Let's take the errors one at a time, because they tend to cascade.

    On the preceding line, the closing brace '}' should be followed by a semicolon ';'.

    That '}' marks the end of the class declaration which begins with 'template<class T> class list {'. A class or struct declaration must be followed by a ';'.

    This is a common pattern with C++ errors, by the way. An error on one line often doesn't trigger a compiler message until the following line, or even later. In such cases the given error message often doesn't reflect the nature of the actual error!
     
  14. Jul 18, 2015 #13

    MathematicalPhysicist

    User Avatar
    Gold Member

    @jtbell I added the semi-colon, but I still get the same error message, here's the corrected code:
    Code (Text):

    #include<stdio.h>
    #include<math.h>
    #include <algorithm>
    template<class T> class list {
    protected:
        int number;
        T** item;
    public:
        list(int n=0):number(n), item(n ? new T*[n]: 0) { }

        list(int n, const T& t) : number(n), item(n ? new T*[n] : 0) {
            for(int i=0; i<number; i++)
                item[i] = new T(t);
        }
        list(const list<T>&);
        const list<T>& operator=(const list<T>&);
    };
    ~list(){
    for(int i=0; i<number; i++)
    delete item[i];
    delete [] item;
    }
    int size() const{
    return number;
    }
    T& operator()(int i){
    if(item[i])return *(item[i]);
    }
    const T& operator[](int i)const{
    if(item[i])return *(item[i]);
    }
    };
    template<class T>
    list<T>::list(const list<T>&l):number(l.number),
    item(l.number ? new T*[l.number] : 0){
    for(int i=0; i<l.number; i++)
    if(l.item[i]) item[i] = new T(*l.item[i]);
    }
    template<class T>
    const list<T>&
    list<T>::operator=(const list<T>& l){
    if(this != &l){
    if(number > l.number)
    delete [] (item + l.number);
    if(number < l.number){
    delete [] item;
    item = new T*[l.number];
    }
    for(int  i  =  0;  i  <  l.number; i++)
    if(l.item[i]) item[i] = new T(*l.item[i]);
    number = l.number;
    }
    return *this;
    }
    template<class T>
    void print(const list<T>&l){
    for(int i=0; i<l.size(); i++){
    printf("i=%d:\n",i);
    print(l[i]);
    }
    }




    template<class T> class polynomial : public list<T> {
    public:
        polynomial(int n=0) {
            number = n;
            item = n ? new T*[n] : 0;
            for(int i=0; i<n; i++)
                item[i] = 0;
        }

        polynomial(int n, const T& a) {
            number = n;
            item = n ? new T*[n] : 0;

            for(int i = 0; i < n; i++)
                item[i] = new T(a);
        }
    };
    ~polynomial(){
    }
    int degree() const{
    return number-1;
    }
    };


    template<class T>
    const polynomial<T>& operator+=(polynomial<T>& p, const polynomial<T>& q)
    {
        if (p.size() >= q.size())
            for(int i = 0; i < q.size(); i++)
                p(i) += q[i];
        else {
            polynomial<T> keepQ = q;
            p = keepQ += p;
        }
        return p;
    }

    template<class T>
    const polynomial<T> operator+(const polynomial<T>& p, const polynomial<T>& q)
    {
        polynomial<T> keep = p;
        return keep += q;
    }

    template<class T>
    const polynomial<T>& operator*=(polynomial<T>& p, const T& a)
    {
        for(int i = 0; i < p.size(); i++)
            p(i) *= a;
        return p;
    }

    template<class T>
    const polynomial<T> operator*(const T& a, const polynomial<T>& p)
    {
        polynomial<T> keep = p;
        return keep *= a;
    }

    template<class T>
    polynomial<T> operator*(const polynomial<T>& p, const polynomial<T>& q)
    {
        polynomial<T> result(p.degree() + q.degree() + 1, 0);
        for (int i = 0; i < result.size(); i++)
            for (int j = max(0, i - q.degree()); j <= min(i, p.degree()); j++) {
                if (j == max(0, i - q.degree()))
                    result(i) = p[j] * q[i-j];
                else
                    result(i) += p[j] * q[i-j];
            }
        return result;
    }

    template<class T>
    polynomial<T>& operator*=(polynomial<T>& p, const polynomial<T>& q) {
        return p = p * q;
    }

    int main()
    {
        polynomial<double> p(3, 1);
        print(p * p);
        return 0;
    }
     
     
  15. Jul 18, 2015 #14

    jtbell

    User Avatar

    Staff: Mentor

    Ah, now I think I see. That curly brace shouldn't have been there at all, because the '~list' line and several following lines are actually part of the class declaration. Several lines later there is a '};' line which (I think) actually closes the 'template<class T> class list {'.

    Delete the line '};' (the one before '~list()') entirely and see what happens. If my guess is correct, the code up to the following '};' should be re-formatted to be consistent with the preceding lines, to prevent confusion. Semi-experts like me tend to get confused by poorly formatted/indented code. Real experts remember to ignore the indentation and focus on the actual code, character by character!
     
    Last edited: Jul 18, 2015
  16. Jul 18, 2015 #15

    MathematicalPhysicist

    User Avatar
    Gold Member

  17. Jul 18, 2015 #16

    wle

    User Avatar

    Here's a complete version that should compile:

    Code (C++):
    #include <stdio.h>
    #include <algorithm>

    using namespace std;

    template<class T> class list {
    protected:
        int number;
        T** item;
    public:
        list(int n=0):number(n), item(n ? new T*[n]: 0) { }

        list(int n, const T& t) : number(n), item(n ? new T*[n] : 0) {
            for(int i = 0; i < number; i++)
                item[i] = new T(t);
        }

        list(const list<T>&);

        const list<T>& operator=(const list<T>&);

        ~list() {
            for(int i = 0; i < number; i++)
                delete item[i];
            delete [] item;
        }
       
        int size() const{ return number; }
       
        T& operator() (int i) { return *(item[i]); }

        const T& operator[](int i) const { return *(item[i]); }
    };

    template<class T>
    list<T>::list(const list<T>&l):number(l.number),
                                   item(l.number ? new T*[l.number] : 0)
    {
        for(int i=0; i<l.number; i++)
            if(l.item[i]) this->item[i] = new T(*l.item[i]);
    }

    template<class T>
    const list<T>& list<T>::operator=(const list<T>& l){
        if(this != &l){
            if(number > l.number)
                delete [] (item + l.number);
            if(number < l.number){
                delete [] item;
                item = new T*[l.number];
            }
            for(int  i  =  0;  i  <  l.number; i++)
                if(l.item[i]) item[i] = new T(*l.item[i]);
            number = l.number;
        }
        return *this;
    }

    void print(const list<double>& l)
    {
        for (int i = 0; i < l.size(); i++){
            // printf("i=%d:\n",i);
            // print(l[i]);
            printf("i=%d: %f\n", i, l[i]);
        }
    }

    template<class T> class polynomial : public list<T> {
    public:
        polynomial(int n=0) {
            this->number = n;
            this->item = n ? new T*[n] : 0;
            for(int i=0; i<n; i++)
                this->item[i] = 0;
        }

        polynomial(int n, const T& a) {
            this->number = n;
            this->item = n ? new T*[n] : 0;

            for(int i = 0; i < n; i++)
                this->item[i] = new T(a);
        }

        ~polynomial() { }

        int degree() const { return this->number - 1; }
    };

    template<class T>
    const polynomial<T>& operator+=(polynomial<T>& p, const polynomial<T>& q)
    {
        if (p.size() >= q.size())
            for(int i = 0; i < q.size(); i++)
                p(i) += q[i];
        else {
            polynomial<T> keepQ = q;
            p = keepQ += p;
        }
        return p;
    }

    template<class T>
    const polynomial<T> operator+(const polynomial<T>& p, const polynomial<T>& q)
    {
        polynomial<T> keep = p;
        return keep += q;
    }

    template<class T>
    const polynomial<T>& operator*=(polynomial<T>& p, const T& a)
    {
        for(int i = 0; i < p.size(); i++)
            p(i) *= a;
        return p;
    }

    template<class T>
    const polynomial<T> operator*(const T& a, const polynomial<T>& p)
    {
        polynomial<T> keep = p;
        return keep *= a;
    }

    template<class T>
    polynomial<T> operator*(const polynomial<T>& p, const polynomial<T>& q)
    {
        polynomial<T> result(p.degree() + q.degree() + 1, 0);
        for (int i = 0; i < result.size(); i++)
            for (int j = max(0, i - q.degree()); j <= min(i, p.degree()); j++) {
                if (j == max(0, i - q.degree()))
                    result(i) = p[j] * q[i-j];
                else
                    result(i) += p[j] * q[i-j];
            }
        return result;
    }

    template<class T>
    polynomial<T>& operator*=(polynomial<T>& p, const polynomial<T>& q) {
        return p = p * q;
    }

    int main()
    {
        polynomial<double> p(3, 1);
        print(p * p);
        return 0;
    }
    Changes to make it work:
    • Put using namespace std; at the top. (Alternatively you could put std:: in front of min and max.)
    • You left out the print() function from your book. It depends on another print() function that I didn't want to hunt for, so I modified it to use only printf().
    • Changed number and item to this->number and this->item in polynomial member functions.
    • Removed if test in list members operator() and operator[].

    These were minimal changes just to get the code to compile without errors or warnings. Posting this doesn't mean I endorse it as an example of good code.
     
    Last edited: Jul 18, 2015
  18. Jul 19, 2015 #17

    MathematicalPhysicist

    User Avatar
    Gold Member

    @wle thanks.

    Now I tried adding the following codes that appear in the book following the codes with the polynomial. I get an error that:

    Code (C++):

    #include <stdio.h>
    #include <algorithm>

    using namespace std;

    template<class T> class list {
    protected:
        int number;
        T** item;
    public:
        list(int n=0):number(n), item(n ? new T*[n]: 0) { }

        list(int n, const T& t) : number(n), item(n ? new T*[n] : 0) {
            for(int i = 0; i < number; i++)
                item[i] = new T(t);
        }

        list(const list<T>&);

        const list<T>& operator=(const list<T>&);

        ~list() {
            for(int i = 0; i < number; i++)
                delete item[i];
            delete [] item;
        }
     
        int size() const{ return number; }
     
        T& operator() (int i) { return *(item[i]); }

        const T& operator[](int i) const { return *(item[i]); }
    };

    template<class T>
    list<T>::list(const list<T>&l):number(l.number),
                                   item(l.number ? new T*[l.number] : 0)
    {
        for(int i=0; i<l.number; i++)
            if(l.item[i]) this->item[i] = new T(*l.item[i]);
    }

    template<class T>
    const list<T>& list<T>::operator=(const list<T>& l){
        if(this != &l){
            if(number > l.number)
                delete [] (item + l.number);
            if(number < l.number){
                delete [] item;
                item = new T*[l.number];
            }
            for(int  i  =  0;  i  <  l.number; i++)
                if(l.item[i]) item[i] = new T(*l.item[i]);
            number = l.number;
        }
        return *this;
    }

    void print(const list<double>& l)
    {
        for (int i = 0; i < l.size(); i++){
            // printf("i=%d:\n",i);
            // print(l[i]);
            printf("i=%d: %f\n", i, l[i]);
        }
    }

    template<class T> class polynomial : public list<T> {
    public:
        polynomial(int n=0) {
            this->number = n;
            this->item = n ? new T*[n] : 0;
            for(int i=0; i<n; i++)
                this->item[i] = 0;
        }

        polynomial(int n, const T& a) {
            this->number = n;
            this->item = n ? new T*[n] : 0;

            for(int i = 0; i < n; i++)
                this->item[i] = new T(a);
        }

        ~polynomial() { }

        int degree() const { return this->number - 1; }
    };

    template<class T>
    const polynomial<T>& operator+=(polynomial<T>& p, const polynomial<T>& q)
    {
        if (p.size() >= q.size())
            for(int i = 0; i < q.size(); i++)
                p(i) += q[i];
        else {
            polynomial<T> keepQ = q;
            p = keepQ += p;
        }
        return p;
    }

    template<class T>
    const polynomial<T> operator+(const polynomial<T>& p, const polynomial<T>& q)
    {
        polynomial<T> keep = p;
        return keep += q;
    }

    template<class T>
    const polynomial<T>& operator*=(polynomial<T>& p, const T& a)
    {
        for(int i = 0; i < p.size(); i++)
            p(i) *= a;
        return p;
    }

    template<class T>
    const polynomial<T> operator*(const T& a, const polynomial<T>& p)
    {
        polynomial<T> keep = p;
        return keep *= a;
    }

    template<class T>
    polynomial<T> operator*(const polynomial<T>& p, const polynomial<T>& q)
    {
        polynomial<T> result(p.degree() + q.degree() + 1, 0);
        for (int i = 0; i < result.size(); i++)
            for (int j = max(0, i - q.degree()); j <= min(i, p.degree()); j++) {
                if (j == max(0, i - q.degree()))
                    result(i) = p[j] * q[i-j];
                else
                    result(i) += p[j] * q[i-j];
            }
        return result;
    }

    template<class T>
    polynomial<T>& operator*=(polynomial<T>& p, const polynomial<T>& q) {
        return p = p * q;
    }
    template<class T>
    const T
    HornerPolynomial(const polynomial<T>&p, const T&x){
    T result = p[p.degree()];
    for(int i=p.degree(); i>0; i--){
    result *= x;
    result += p[i-1];
    }
    return result;
    } // Horner algorithm to calculate a polynomial
    template<class T>
    const list<T>
    deriveRinverse(const T&r, int n){
    list<T> Rinverse(n+1,0);
    Rinverse(0) = 1/r;
    for(int i=0; i<n; i++)
    Rinverse(i+1) = -double(i+1)/r * Rinverse[i];
    return Rinverse;
    } // derivatives of 1/r

    template<class T>
    const T
    Taylor(const list<T>&f, const T&h){
    T powerOfHoverIfactorial = 1;
    T sum=0;
    for(int i=0; i<f.size()-1; i++){
    sum += f[i] * powerOfHoverIfactorial;
    powerOfHoverIfactorial *= h/(i+1);
    }
    return sum;
    } // Taylor approximation to f(x+h)
    template<class T>
    const T
    HornerTaylor(const list<T>&f, const T&h){
    T result = f[f.size()-2];
    for(int i=f.size()-2; i>0; i--){
    result *= h/i;
    result += f[i-1];
    }
    return result;
    } // Horner algorithm for Taylor approximation
    int main(){
    polynomial<double> p(3, 1);
        print(p * p);
        return 0;
    return 0;
    }
    template<class T>
    const T
    deriveProduct(const list<T>&f,const list<T>g,int n){
    T  sum  =  0;
    const int n=8;
    int triangle[n][n];
    for(int i=0; i<n; i++)
    triangle[i][0]=triangle[0][i]=1;
    for(int i=1; i<n-1; i++)
    for(int j=1; j<=n-1-i; j++)
    triangle[i][j] = triangle[i-1][j]+triangle[i][j-1];
    for(int i=0; i<=n; i++)
    sum += triangle[n-i][i] * f[i] * g[n-i];
    return sum;
    } // nth derivative of a product

     
    I get on line 194 the next error:"declaration of 'const int n' shadows a parameter", how to reconcile this problem? I added the triangle array to this template that appears on page 176 of the ebook, the triangle appears in chapter 1 section 19, but in "int main()".
     
  19. Jul 19, 2015 #18

    MathematicalPhysicist

    User Avatar
    Gold Member

    I now tried to work out the code on page 178, section 5.13 the operation of calculating the coefficinets of (1+x+y)^2.
    I get the error on line 188: invalid initilization of reference of type 'const list <double>&' from expression of type 'polynomial<polynomial<double> >'.
    How to fix it?
    Code (C++):

    #include <stdio.h>
    #include <algorithm>

    using namespace std;

    template<class T> class list {
    protected:
        int number;
        T** item;
    public:
        list(int n=0):number(n), item(n ? new T*[n]: 0) { }

        list(int n, const T& t) : number(n), item(n ? new T*[n] : 0) {
            for(int i = 0; i < number; i++)
                item[i] = new T(t);
        }

        list(const list<T>&);

        const list<T>& operator=(const list<T>&);

        ~list() {
            for(int i = 0; i < number; i++)
                delete item[i];
            delete [] item;
        }
       
        int size() const{ return number; }
       
        T& operator() (int i) { return *(item[i]); }

        const T& operator[](int i) const { return *(item[i]); }
    };

    template<class T>
    list<T>::list(const list<T>&l):number(l.number),
                                   item(l.number ? new T*[l.number] : 0)
    {
        for(int i=0; i<l.number; i++)
            if(l.item[i]) this->item[i] = new T(*l.item[i]);
    }

    template<class T>
    const list<T>& list<T>::operator=(const list<T>& l){
        if(this != &l){
            if(number > l.number)
                delete [] (item + l.number);
            if(number < l.number){
                delete [] item;
                item = new T*[l.number];
            }
            for(int  i  =  0;  i  <  l.number; i++)
                if(l.item[i]) item[i] = new T(*l.item[i]);
            number = l.number;
        }
        return *this;
    }

    void print(const list<double>& l)
    {
        for (int i = 0; i < l.size(); i++){
            // printf("i=%d:\n",i);
            // print(l[i]);
            printf("i=%d: %f\n", i, l[i]);
        }
    }

    template<class T> class polynomial : public list<T> {
    public:
        polynomial(int n=0) {
            this->number = n;
            this->item = n ? new T*[n] : 0;
            for(int i=0; i<n; i++)
                this->item[i] = 0;
        }

        polynomial(int n, const T& a) {
            this->number = n;
            this->item = n ? new T*[n] : 0;

            for(int i = 0; i < n; i++)
                this->item[i] = new T(a);
        }

        ~polynomial() { }

        int degree() const { return this->number - 1; }
    };

    template<class T>
    const polynomial<T>& operator+=(polynomial<T>& p, const polynomial<T>& q)
    {
        if (p.size() >= q.size())
            for(int i = 0; i < q.size(); i++)
                p(i) += q[i];
        else {
            polynomial<T> keepQ = q;
            p = keepQ += p;
        }
        return p;
    }

    template<class T>
    const polynomial<T> operator+(const polynomial<T>& p, const polynomial<T>& q)
    {
        polynomial<T> keep = p;
        return keep += q;
    }

    template<class T>
    const polynomial<T>& operator*=(polynomial<T>& p, const T& a)
    {
        for(int i = 0; i < p.size(); i++)
            p(i) *= a;
        return p;
    }

    template<class T>
    const polynomial<T> operator*(const T& a, const polynomial<T>& p)
    {
        polynomial<T> keep = p;
        return keep *= a;
    }

    template<class T>
    polynomial<T> operator*(const polynomial<T>& p, const polynomial<T>& q)
    {
        polynomial<T> result(p.degree() + q.degree() + 1, 0);
        for (int i = 0; i < result.size(); i++)
            for (int j = max(0, i - q.degree()); j <= min(i, p.degree()); j++) {
                if (j == max(0, i - q.degree()))
                    result(i) = p[j] * q[i-j];
                else
                    result(i) += p[j] * q[i-j];
            }
        return result;
    }

    template<class T>
    polynomial<T>& operator*=(polynomial<T>& p, const polynomial<T>& q) {
        return p = p * q;
    }
    template<class T>
    const T
    HornerPolynomial(const polynomial<T>&p, const T&x){
    T result = p[p.degree()];
    for(int i=p.degree(); i>0; i--){
    result *= x;
    result += p[i-1];
    }
    return result;
    } // Horner algorithm to calculate a polynomial
    template<class T>
    const list<T>
    deriveRinverse(const T&r, int n){
    list<T> Rinverse(n+1,0);
    Rinverse(0) = 1/r;
    for(int i=0; i<n; i++)
    Rinverse(i+1) = -double(i+1)/r * Rinverse[i];
    return Rinverse;
    } // derivatives of 1/r

    template<class T>
    const T
    Taylor(const list<T>&f, const T&h){
    T powerOfHoverIfactorial = 1;
    T sum=0;
    for(int i=0; i<f.size()-1; i++){
    sum += f[i] * powerOfHoverIfactorial;
    powerOfHoverIfactorial *= h/(i+1);
    }
    return sum;
    } // Taylor approximation to f(x+h)
    template<class T>
    const T
    HornerTaylor(const list<T>&f, const T&h){
    T result = f[f.size()-2];
    for(int i=f.size()-2; i>0; i--){
    result *= h/i;
    result += f[i-1];
    }
    return result;
    } // Horner algorithm for Taylor approximation
    int main(){
    polynomial<polynomial<double> >
    p2(2,polynomial<double>(1,1));
    p2(1) = polynomial<double>(2,1);
    print(p2*p2);
    return 0;
    }
     
     
  20. Jul 19, 2015 #19

    wle

    User Avatar

    You've defined a function that takes a parameter int n and also declares a variable const int n:
    Code (C++):
    template<class T>
    const T deriveProduct(const list<T>& f, const list<T> g, int n)
    {
        T sum = 0;
        const int n = 8;
    This is what's causing the error.


    So you copied code from one place in your book into the body of a completely unrelated function? Why would you do that?


    With clang++ I got the error message:
    Code (Text):
    polyn3.cc:186:5: error: no matching function for call to 'print'
        print(p2*p2);
        ^~~~~
    polyn3.cc:59:6: note: candidate function not viable: no known conversion from
          'polynomial<polynomial<double> >' to 'const list<double>' for 1st argument
    void print(const list<double>& l)
    This means that the compiler didn't find a version of the function print() that would accept a parameter of type polynomial<polynomial<double> >.

    You can try to fix this by changing the definition of print() back to the way it is in the book and adding a print() function that will work with doubles:
    Code (C++):
    void print(double x)
    {
        printf("%f\n", x);
    }

    template<class T>
    void print(const list<T>& l)
    {
        for (int i = 0; i < l.size(); i++) {
            printf("i=%d:\n", i);
            print(l[i]);
        }
    }
     
  21. Jul 20, 2015 #20

    MathematicalPhysicist

    User Avatar
    Gold Member

    @wle what appears in the book is:
    Code (C++):

    void print(double d){
    printf("%f; ",d);
    } /* print a double variable */
     
    without the second part that you wrote.

    Anyway, thanks, it's now well compiled and running.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: A programme that doesn't get compiled in C++
  1. C++ compilers (Replies: 24)

Loading...