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

  • C/C++
  • Thread starter MathematicalPhysicist
  • Start date
  • Tags
    C++
In summary: I can use some kind of template code for it.In summary, the conversation is about a code for operations on polynomials in C++ that is causing errors. The code is copied from a book on solving PDEs in C++ and also includes a template class for list. The code appears scattered in the book and the person is unsure about how to fix the errors, including a declaration error for 'class <T>'. The person also added the math library thinking it would include max and min functions, but they are actually in <algorithm>.
  • #1
MathematicalPhysicist
Gold Member
4,699
371
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:
#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.
 
Technology news on Phys.org
  • #2
BTW, I have compiled it in dev C++ 5.11.
 
  • #3
Is that really how the code appears in your book?

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

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).
MathematicalPhysicist said:
I added the math library since I thought that it will include the max and min functions.

That's in <algorithm>.
 
Last edited:
  • #4
@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:
  • #5
MathematicalPhysicist said:
@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++

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.
I know how to write a max and min functions, but I thought that are already included in some library in C++

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:
  • #6
@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:
#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!
 
  • #7
MathematicalPhysicist said:
"stray '\230' in program" on lines 18 and 83
MathematicalPhysicist said:
˜list(){
MathematicalPhysicist said:
˜polynomial(){
Those two lines should be
Code:
~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.
 
  • Like
Likes MathematicalPhysicist
  • #8
@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!
 
  • #9
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:
  • #10
@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.
 
  • #11
Borg said:
For example, are you sure that the tilde on line 18 is legal before the list() method? (I don't know)

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.)
 
  • #12
Let's take the errors one at a time, because they tend to cascade.

MathematicalPhysicist said:
on line 18, "expected class-name before '(' token

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!
 
  • #13
@jtbell I added the semi-colon, but I still get the same error message, here's the corrected code:
Code:
#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;
}
 
  • #14
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:
  • #16
Here's a complete version that should compile:

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:
  • #17
@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:

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()".
 
  • #18
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?
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;
}
 
  • #19
MathematicalPhysicist said:
I get on line 194 the next error:"declaration of 'const int n' shadows a parameter", how to reconcile this problem?

You've defined a function that takes a parameter int n and also declares a variable const int n:
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.
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()".

So you copied code from one place in your book into the body of a completely unrelated function? Why would you do that?
MathematicalPhysicist said:
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?

With clang++ I got the error message:
Code:
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:
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]);
    }
}
 
  • Like
Likes MathematicalPhysicist
  • #20
@wle what appears in the book is:
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.
 
  • #21
@wle I now want to compile the code on page 192, section 6.9. He says that we are using the low level code from chapter 2, sections 18 and 20, for the objects 'vector<T,N>' and 'matrix', so I copied the code in those sections, before the code in section 6.9, but I get plenty of errors.
C++:
#include<stdio.h>
template<class T, int N> class vector{
T component[N];
public:
vector(const T&);
vector(const vector&);
const vector& operator=(const vector&);
const vector& operator=(const T&);
~vector(){
} // destructor
const T& operator[](int i) const{
return component[i];
} //read ith component
void set(int i,const T& a){
component[i] = a;
} // change ith component
template<class T, int N>
vector<T,N>::vector(const T&  a  =  0){ 
for(int  i  =  0;  i  <  N;  i++)
component[i] = a;
} // constructor
template<class T, int N>
vector<T,N>::vector(const vector<T,N>& v){
for(int  i  =  0;  i  <  N;  i++)
component[i] = v.component[i];
} // copy constructor
template<class T, int N>
const vector<T,N>& vector<T,N>::operator=(
const vector<T,N>& v){
if(this != &v)
for(int  i  =  0;  i  <  N;  i++)
component[i] = v.component[i];
return *this;
} // assignment operator
template<class T, int N>
const vector<T,N>& vector<T,N>::operator=(const T& a){
for(int  i  =  0;  i  <  N;  i++)
component[i] = a;
return *this;
} // assignment operator with a scalar argument
template<class T, int N>
const vector<T,N>&
vector<T,N>::operator+=(const vector<T,N>&v){
for(int  i  =  0;  i  <  N;  i++)
component[i] += v[i];
return *this;
} // adding a vector to the current vector
template<class T, int N>
const vector<T,N>
operator+(const vector<T,N>&u, const vector<T,N>&v){
return vector<T,N>(u) += v;
} // vector plus vector
template<class T, int N>
const vector<T,N>&
operator+(const vector<T,N>&u){
return u;
} // positive of a vector
template<class T, int N>
const vector<T,N>
operator-(const vector<T,N>&u){
return vector<T,N>(u) *= -1;
} // negative of a vector
template<class T, int N>
const T
operator*(const vector<T,N>&u, const vector<T,N>&v){
T  sum  =  0; 
for(int  i  =  0;  i  <  N;  i++)
sum += u[i] * +v[i];
return sum;
} // vector times vector (inner product)
template<class T, int N>
T squaredNorm(const vector<T,N>&u){
return u*u;
} // sum of squares
template<class T, int N>
void print(const vector<T,N>&v){
printf("(");
for(int i = 0;i < N; i++){
printf("v[%d]=",i);
print(v[i]);
}
printf(")\n");
} // printing a vector
const vector& operator+=(const vector&);
const vector& operator-=(const vector&);
const vector& operator*=(const T&);
const vector& operator/=(const T&);
};
template<class T, int N, int M>
class matrix : public vector<vector<T,N>,M>{
public:
matrix(){}
matrix(const vector<T,N>&u, const vector<T,N>&v){
set(0,u);
set(1,v);
} // constructor
const T& operator()(int i,int j) const{
return (*this)[j][i];
} // read the (i,j)th matrix element
const matrix& operator*=(const T&);
const matrix& operator/=(const T&);
};

template<class T, int N>
const vector<T,N>
TaylorScheme(const vector<T,N>&u0,
const matrix<T,N,N>&S,
const list<vector<T,N> >&f,
const T&h){
T powerOfHoverIfactorial = 1;
vector<T,N> sum=0;
vector<T,N> uDerivative = u0;
for(int i=0; i<f.size(); i++){
sum += powerOfHoverIfactorial * uDerivative;
uDerivative  =  S  *  uDerivative + f[i];
powerOfHoverIfactorial *= h/(i+1);
}
return sum;
} // Taylor scheme
on line 2 it says an error of:"shadows template parm 'int N'", there are more errors, as you can check in your compiler.
 
  • #22
This post is a bit blunt, and it's intentional. Two comments.

#1, please try to indent your code. This is very big. Your code tells me you are a sloppy programmer who is not worthy of my help. It might not be valid, but that's what I see. To me, grossly improperly indented code is a big huge sign that I code I am looking at is chock full of bugs.

#2, it appears that you are being taught to use cargo cult programming techniques. You should understand where those errors are coming from, and understand how to fix them. This means you need to know about how the language you are using works. Don't just slap stuff together and hope it works.
 
  • Like
Likes nsaspook
  • #23
@D H I thought that the parts that I copied from the book will work together as it's written in the book that they should, am I missing somewhere a semi-colon?
 
  • #24
MathematicalPhysicist said:
on line 2 it says an error of:"shadows template parm 'int N'", there are more errors, as you can check in your compiler.

Have you checked that you're not making the same types of errors (e.g. copying code incorrectly) that have been pointed out to you earlier in this thread?

By the way, my ability to find and fix problems in this sort of code isn't magic. In this case a big part of it is: 1) properly formatted code is a lot easier to read, 2) there are freely available tools* that can take care of a lot of the work of formatting it for you, and 3) looking at where the autoindentation visibly goes haywire can already identify some basic syntax errors, like where you forgot a closing brace or put one in the wrong place.*For example, the first thing I did with the code in some of your earlier posts was copy/paste it into an Emacs buffer and run the autoindent tool.EDIT: DH beat me to it. Given the pattern in this thread I've been meaning to ask this myself for a while now: where is this code-copying going and what are you expecting to get out of it? The problems you've been having, like not being able to spot an incomplete class declaration, or code depending on missing functions and methods, make it pretty obvious you don't really understand the code you're copying. So why are you doing this?
 
Last edited:
  • #25
MathematicalPhysicist said:
@D H I thought that the parts that I copied from the book will work together as it's written in the book that they should, am I missing somewhere a semi-colon?
You did not copy correctly. You made two very key mistakes. One was not copying correctly. The other was not understanding what you were copying.

I'll look at three ways to write the copy constructor for that class.
Code:
template<class T, int N> class vector {
   T component[N];
public:
   vector(const vector&);
   // Other members declarations elided.
}; // Note the close brace and semicolon.

// This is an out-of-line definition of the copy constructor.
template<class T, int N>
inline // This is important! The author of that ebook omitted this!
vector<T,N>::vector (const vector<T,N>& v) {
   for (int ii = 0; ii < N; ++ii) {
      component[ii] = v.component[ii];
   }
}

Compare that with the copy-constructor declared and defined inline:
Code:
template<class T, int N> class vector{
   T component[N];
public:
   vector(const vector& v) {
      for (int ii = 0; ii < N; ++ii) {
         component[ii] = v.component[ii];
      }
   }
   // Other members declarations/definitions elided.
};
To me, this is much simpler to write and much simpler to read than is the non-inlined version. But both approaches are legal code, and some people do prefer the non-inline version.Finally, compare the above with what you did.
Code:
template<class T, int N> class vector {
   T component[N];
public:
   vector(const vector&);
   // Other members declarations elided.

   // This is an illegal inline definition of the copy constructor that
   // mixes and matches the inline and non-inline syntax.
   template<class T, int N>
   vector<T,N>::vector (const vector<T,N>& v) {
      for (int ii = 0; ii < N; ++ii) {
         component[ii] = v.component[ii];
      }
   }
};
 
Last edited:
  • #26
D H said:
You did not copy correctly. You made two very key mistakes. One was not copying correctly. The other was not understanding what you were copying.

I'll look at three ways to write the copy constructor for that class.
Code:
template<class T, int N> class vector {
   T component[N];
public:
   vector(const vector&);
   // Other members declarations elided.
}; // Note the close brace and semicolon.

// This is an out-of-line definition of the copy constructor.
template<class T, int N>
inline // This is important! The author of that ebook omitted this!
vector<T,N>::vector (const vector<T,N>& v) {
   for (int ii = 0; ii < N; ++ii) {
      component[ii] = v.component[ii];
   }
}

Compare that with the copy-constructor declared and defined inline:
Code:
template<class T, int N> class vector{
   T component[N];
public:
   vector(const vector& v) {
      for (int ii = 0; ii < N; ++ii) {
         component[ii] = v.component[ii];
      }
   }
   // Other members declarations/definitions elided.
};
To me, this is much simpler to write and much simpler to read than is the non-inlined version. But both approaches are legal code, and some people do prefer the non-inline version.Finally, compare the above with what you did.
Code:
template<class T, int N> class vector {
   T component[N];
public:
   vector(const vector&);
   // Other members declarations elided.

   // This is an illegal inline definition of the copy constructor that
   // mixes and matches the inline and non-inline syntax.
   template<class T, int N>
   vector<T,N>::vector (const vector<T,N>& v) {
      for (int ii = 0; ii < N; ++ii) {
         component[ii] = v.component[ii];
      }
   }
};
@D H @wle I copied in the order they appear in the book, so I don't think a curly brace is missing, perhaps a semi colon is written where is shouldn't be in that case how can Iknow when to erase it?
I am providing below with an indented code, hopefully you can tell me what is wrong with my code?
C++:
#include<stdio.h>
template<class T, int N> class vector {
    T component[N];
public:
    vector(const T&);
    vector(const vector&);
    const vector& operator=(const vector&);
    const vector& operator=(const T&);
    ~vector() {
    } // destructor
    const T& operator[](int i) const {
        return component[i];
    } //read ith component
    void set(int i, const T& a) {
        component[i] = a;
    } // change ith component
    template<class T, int N>
    vector<T, N>::vector(const T&  a = 0) {
        for (int i = 0; i < N; i++)
            component[i] = a;
    } // constructor
    template<class T, int N>
    vector<T, N>::vector(const vector<T, N>& v) {
        for (int i = 0; i < N; i++)
            component[i] = v.component[i];
    } // copy constructor
    template<class T, int N>
    const vector<T, N>& vector<T, N>::operator=(
        const vector<T, N>& v) {
        if (this != &v)
            for (int i = 0; i < N; i++)
                component[i] = v.component[i];
        return *this;
    } // assignment operator
    template<class T, int N>
    const vector<T, N>& vector<T, N>::operator=(const T& a) {
        for (int i = 0; i < N; i++)
            component[i] = a;
        return *this;
    } // assignment operator with a scalar argument
    template<class T, int N>
    const vector<T, N>&
        vector<T, N>::operator+=(const vector<T, N>&v) {
        for (int i = 0; i < N; i++)
            component[i] += v[i];
        return *this;
    } // adding a vector to the current vector
    template<class T, int N>
    const vector<T, N>
        operator+(const vector<T, N>&u, const vector<T, N>&v) {
        return vector<T, N>(u) += v;
    } // vector plus vector
    template<class T, int N>
    const vector<T, N>&
        operator+(const vector<T, N>&u) {
        return u;
    } // positive of a vector
    template<class T, int N>
    const vector<T, N>
        operator-(const vector<T, N>&u) {
        return vector<T, N>(u) *= -1;
    } // negative of a vector
    template<class T, int N>
    const T
        operator*(const vector<T, N>&u, const vector<T, N>&v) {
        T  sum = 0;
        for (int i = 0; i < N; i++)
            sum += u[i] * +v[i];
        return sum;
    } // vector times vector (inner product)
    template<class T, int N>
    T squaredNorm(const vector<T, N>&u) {
        return u*u;
    } // sum of squares
    template<class T, int N>
    void print(const vector<T, N>&v) {
        printf("(");
        for (int i = 0;i < N; i++) {
            printf("v[%d]=", i);
            print(v[i]);
        }
        printf(")\n");
    } // printing a vector
    const vector& operator+=(const vector&);
    const vector& operator-=(const vector&);
    const vector& operator*=(const T&);
    const vector& operator/=(const T&);
};
template<class T, int N, int M>
class matrix : public vector<vector<T, N>, M> {
public:
    matrix() {}
    matrix(const vector<T, N>&u, const vector<T, N>&v) {
        set(0, u);
        set(1, v);
    } // constructor
    const T& operator()(int i, int j) const {
        return (*this)[j][i];
    } // read the (i,j)th matrix element
    const matrix& operator*=(const T&);
    const matrix& operator/=(const T&);
};

template<class T, int N>
const vector<T, N>
TaylorScheme(const vector<T, N>&u0,
    const matrix<T, N, N>&S,
    const list<vector<T, N> >&f,
    const T&h) {
    T powerOfHoverIfactorial = 1;
    vector<T, N> sum = 0;
    vector<T, N> uDerivative = u0;
    for (int i = 0; i<f.size(); i++) {
        sum += powerOfHoverIfactorial * uDerivative;
        uDerivative = S  *  uDerivative + f[i];
        powerOfHoverIfactorial *= h / (i + 1);
    }
    return sum;
} // Taylor scheme
 
  • #27
Hi I have another question, in the last code above that I copied, is it ok first to declare the operators or do I need first to define these operators?
I first defined them, so the next code is copied after I finished defining these operators:
C++:
const vectors operator+=(const vectors); 
const vectors operator-=(const vectors); 
const vectors operator*=(const T&); 
const vectors operator/=(const T&); 
};
 
  • #28
I have another question on syntax in visual C++.

I have the next code:
C++:
#include <iostream>
#define N 3
void main()
{
    int i;
    int *u, *v, w;
    u = new int[N + 1];
    u[1] = 4; u[2] = -5; u[3] = 3;
    cout << endl << "Vector u:" << endl;
    for (i = 1; i <= N; i++)
        cout << u[i] << " ";
    cout << endl;
    v = new int[N + 1];
    v[1] = -2; v[2] = 3; v[3] = 7;
    cout << "Vector v:" << endl;
    for (i = 1; i <= N; i++)
        cout << v[i] << " ";
    cout << endl;
    w = 0;
    for (i = 1; i <= N; i++)
        w += u[i] * v[i];
    cout << "the product w=u.v is " << w << endl;
    delete u, v;
}
I've compiled in visual C++ 2015, and I get an error: 'cout' undeclared identifier on line 9 and 'endl' undeclared identifier on line 9.

I don't understnad what is the purpose of 'cout' and 'endl', I copied it from the book: "computing for numerical methods using visual C++" on page 17, it should be a dot product of two vectors.
 
  • #29
MathematicalPhysicist said:
I copied in the order they appear in the book, so I don't think a curly brace is missing, perhaps a semi colon is written where is shouldn't be in that case how can Iknow when to erase it?

You should really learn C++ before copying code around like this.
I am providing below with an indented code, hopefully you can tell me what is wrong with my code?

You closed a class declaration in the wrong place. D H actually explained this in his previous post. (If you don't understand the code examples in his post, you should read an explanation of or tutorial on C++ classes.)
MathematicalPhysicist said:
I don't understnad what is the purpose of 'cout' and 'endl', I copied it from the book: "computing for numerical methods using visual C++" on page 17, it should be a dot product of two vectors.

Did you try Google? Or the introduction to just about any tutorial or book on C++? This is not difficult information to look up.
 
  • #30
MathematicalPhysicist said:
@D H @wle I copied in the order they appear in the book, so I don't think a curly brace is missing, perhaps a semi colon is written where is shouldn't be in that case how can Iknow when to erase it?
You are missing a close curly brace and a semicolon.

Rather than copying from what is most likely an illegal download of what is most likely a mediocre (at best) book (read the reviews), I suggest you
  • Get a good book that will teach you C++; there are plenty of them out there. Here's a nice FAQ on learning C++: https://isocpp.org/wiki/faq/how-to-learn-cpp.
  • Look to free and legal mathematical codes. There are lots of them. As a starter, the boost library contains a large number of numerical codes, and eigen is a very nice matrix and vector set of templates and classes.
 
  • #31
@D H where exactly am I missing a close brace and a semicolon?

So you tell me to ditch this book, and the other book. any other recommendations on books, perhaps "numerical recipes"?
 
  • #32
@D H how do I add those libraries to visual C++ STL library?
 
  • #33
MathematicalPhysicist said:
C++:
#include <iostream>
#define N 3
void main()
{
    int i;
    int *u, *v, w;
    u = new int[N + 1];
    u[1] = 4; u[2] = -5; u[3] = 3;
    cout << endl << "Vector u:" << endl;
    for (i = 1; i <= N; i++)
        cout << u[i] << " ";
    cout << endl;
    v = new int[N + 1];
    v[1] = -2; v[2] = 3; v[3] = 7;
    cout << "Vector v:" << endl;
    for (i = 1; i <= N; i++)
        cout << v[i] << " ";
    cout << endl;
    w = 0;
    for (i = 1; i <= N; i++)
        w += u[i] * v[i];
    cout << "the product w=u.v is " << w << endl;
    delete u, v;
}
cout and endl belong to the std namespace. To use them in your program, you should prefix each use of these with "std::". In other words, as std::cout or std::endl. Another option, but not recommended, is to add using std; above the header for your main function.

cout is one of the standard streams that your program can use for input or output (cout, cin, cerr, clog). The "out" part of the name indicates that this is an output stream. The other standard streams are for input, errors, and logging. The statement cout << v[1]; sends the value of v[1] to the standard output stream (typically, the screen). endl is the newline character.
 
Last edited:
  • #34
MathematicalPhysicist said:
Hi I have another question, in the last code above that I copied, is it ok first to declare the operators or do I need first to define these operators?
I first defined them, so the next code is copied after I finished defining these operators:
C++:
const vectors operator+=(const vectors);
const vectors operator-=(const vectors);
const vectors operator*=(const T&);
const vectors operator/=(const T&);
};
What you described is backwards: prototypes (what you have above) should appear before the operator definitions, not after them.
 
  • #35
@Mark44 thanks.
Btw, do you happen to know why when I am executing a source file in visual C++ 2015 community the window of the .exe file appears and disappears without me pressing any key to exit from the programme?

The Debug gives me the following lines:
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Cannot find or open the PDB file.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Cannot find or open the PDB file.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Cannot find or open the PDB file.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Symbols loaded.
'Project6.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Symbols loaded.
The program '[6836] Project6.exe' has exited with code 0 (0x0).

I picked an empty project file and as a source file a cpp file, what am I do wrong here that the window exits automatically without me pressing any key?

Drives me nuts...
 

Similar threads

  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
4
Views
784
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top