GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Jun-2009, 13:00
Ivan Ivan is offline
New Member
 
Join Date: May 2009
Posts: 4
Ivan is on a distinguished road

Is it possible to 'compose' functions?


I have a template class Vector<T> that is inherited from the std::vector<T>,
but has extended functionality, for example, it has the component-wise
multiplication * of vectors.

I also (separately) have a function
CPP / C++ / C Code:
  double mean(Vector<double>& v){
...
};


that computes the mean of a Vector<double>.

Now if, for a Vector<double> b (of 100 fixed numbers),
I write cout<< mean ( (b*b))<<endl;
the compiler is not happy. (the code as below works)
CPP / C++ / C Code:
  Vector<double> a=b*b;
          cout<<mean(a)<<end;

Is there a way to be able to write cout<< mean ( (b*b))<<endl; ? (for example,
surely you can write cout<< sin (cos (3))<<endl; )
  #2  
Old 19-Jun-2009, 14:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Is it possible to 'compose' functions?


Quote:
Originally Posted by Ivan
...
Is there a way to be able to write cout<< mean ( (b*b))<<endl; ?

Yes:

CPP / C++ / C Code:
//
// davekw7x
//
// My Vector template class definition stuff is in Vector.h
//
#include "Vector.h"

#include <iostream>
using namespace std;


double mean(const Vector<double> & v);
void print_Vec(const Vector<double> & v);

int main()
{
    int n = 4;

    Vector<double> b(n);

    for (int i = 0; i < n; i++) {
        b[i] = (i+1);
    }
    cout << "b         =";
    print_Vec(b);

    cout << "mean(b)   = " << mean(b) << endl << endl;

    cout << "b*b       =";
    print_Vec(b*b);

    cout << "mean(b*b) = " << mean(b*b) << endl;

    return 0;
}


void print_Vec(const Vector<double> & v)
{
    for (size_t i = 0; i < v.size(); i++) {
        cout << " " << v[i];
    }
    cout << endl;
}


//
// The definition of mean() can go here
//


Output:

Code:
b = 1 2 3 4 mean(b) = 2.5 b*b = 1 4 9 16 mean(b*b) = 7.5

Quote:
Originally Posted by Ivan
compiler is not happy...

I respectfully suggest that for a reasonable expectation of meaningful help with fewer iterations, you might tell us exactly what messages the compiler gave you. Maybe someone can help you to understand what your grouchy old compiler is trying to tell you.

Also, you might tell us what compiler and what version you are using. Sometimes it makes a difference to people who are trying to help.

Regards,


Dave
  #3  
Old 19-Jun-2009, 15:26
Ivan Ivan is offline
New Member
 
Join Date: May 2009
Posts: 4
Ivan is on a distinguished road

Re: Is it possible to 'compose' functions?


Thanks a lot, davekw7x. Your solution works. I should have declared
double mean(const Vector<double> & v)

instead of

double mean(Vector<double> & v).



I respectfully suggest that for a reasonable expectation of meaningful help with fewer iterations, you might tell us exactly what messages the compiler gave you. Maybe someone can help you to understand what your grouchy old compiler is trying to tell you.

Also, you might tell us what compiler and what version you are using. Sometimes it makes a difference to people who are trying to help.

Regards,


Dave[/quote]

Point taken.
  #4  
Old 19-Jun-2009, 16:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Is it possible to 'compose' functions?


Quote:
Originally Posted by Ivan
Thanks a lot, davekw7x. Your solution works. I should have declared
double mean(const Vector<double> & v)

instead of

double mean(Vector<double> & v).

The point is that when you use an expression as an argument to a function, the compiler creates a temporary (constant) object to feed to the function. I'm guessing the compiler message might have been related to "improper initialization of non-constant reference...blah-blah-blah..."

That message may not make much sense to us mere humans (See Footnote) until we have seen it a few times (or a few hundred times like some of us), but it is actually better than lots of compiler messages.


Regards,

Dave

Footnote:

"No one was born knowing this stuff, you know!"
---davekw7x
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with some functions baka_yaro C++ Forum 3 14-Mar-2007 06:56
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
Functions and Classes - Where did I go wrong? redmage C++ Forum 5 10-Apr-2005 19:31
conflict between printf and stdarg.h va functions mirizar C Programming Language 3 12-Jul-2004 09:11
Understanding functions tommy69 C Programming Language 15 15-Mar-2004 18:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:28.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.