function - C++: run-time error from a template class -


hi guys have problem program. i'm new c++ , i'm trying code generic programming but, usual, have lot of errors program. i'm trying best don't understand error is. want template class describe method add() take sum, c'tor , compute() make arithmetic average of sum. nadd number of element. thank much!

template<typename t> class accumulatormean {     public:          accumulatormean() : sum(0), nadd(0), media(0) {};         t add(const t& data);         t compute(); private:         int nadd;         t sum;         t media; };  template <typename t> t& accumulatormean::add(const t& data) {     sum += data;     nadd++;     return sum; } template <typename t> t& accumulatormean::compute() {     media = sum/nadd;     return media; } int main() {     accumulatormean a;     a.add<int>(5); } 

there few error in here:

first:

your main should this:

int main() {     accumulatormean<int> a;     a.add(5); } 

you specified class have template parameter. have add when instantiate class.

second:

when define member functions of class template param has added too:

template <typename t> t accumulatormean<t>::add(const t& data) {     ... } 

see accumulatormean<t>

and third:

in code definitions of functions returned t& while declared them return t. wrong. changed above in code.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -