![]() |
|
#1
|
|||
|
|||
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:
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:
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
|
|||
|
|||
Re: Is it possible to 'compose' functions?Quote:
Yes: CPP / C++ / C Code:
Output: Code:
Quote:
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
|
|||
|
|||
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
|
|||
|
|||
Re: Is it possible to 'compose' functions?Quote:
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 GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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