I need to create function Sum() that calculates sum between two containers. Code below work fine except function Sum between two containers...
How I should re - write my code that everything work fine. Many thanks in advance.
Condition of exercise is : "Also create a Sum() function that calculates the sum between two iterators. The function then uses the template argument for the iterator type and accepts two iterators, the start- and end iterator"
1>------ Build started: Project: HP2_ex2_iter, Configuration: Debug Win32 ------
1> main.cpp
1>c:\all my\с++\ha level 7\solution\level 7 homework overview of the standard template library\hp2_ex2_iter\main.cpp(47): error C2275: 'C1' : illegal use of this type as an expression
1> c:\all my\с++\ha level 7\solution\level 7 homework overview of the standard template library\hp2_ex2_iter\main.cpp(107) : see declaration of 'C1'
1> c:\all my\с++\ha level 7\solution\level 7 homework overview of the standard template library\hp2_ex2_iter\main.cpp(107) : see reference to function template instantiation 'double Sum<std::vector<_Ty>,std::list<_Ty>>(C1,C2)' being compiled
1> with
1> [
1> _Ty=int,
1> C1=std::vector<int>,
1> C2=std::list<int>
1> ]
1>c:\all my\с++\ha level 7\solution\level 7 homework overview of the standard template library\hp2_ex2_iter\main.cpp(47): error C2275: 'C2' : illegal use of this type as an expression
1> c:\all my\с++\ha level 7\solution\level 7 homework overview of the standard template library\hp2_ex2_iter\main.cpp(107) : see declaration of 'C2'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include <list>
#include <vector>
#include <iostream>
#include <map>
using namespace std;
template <typename T>
double Sum (list<T>& container)
{
double sum = 0;
list<T>::iterator il=container.begin();
cout << endl;
while (il!=container.end())
{
sum+=*(il++);
}
return sum ;
}
template <typename T>
double Sum (vector<T>& container)
{
double sum = 0;
vector<T>::iterator iv=container.begin();
while (iv!=container.end())
{
sum+=*(iv++);
}
return sum;
}
template <typename T, typename T1>
double Sum (map<T, T1>& container)
{
double sum =0 ;
map<T, T1>::iterator im=container.begin();
while (im!=container.end())
{
sum+=im++->second;
}
return sum;
}
template <typename C1,typename C2>
double Sum (C1, C2)
{
return Sum(C1) + Sum(C2); // line 47
}
int main() {
//*****************
// LIST CONTAINER
//*****************
cout << "----- sum of list container ----- "<< endl;
list <int> l ;
for (int i=0;i<10;i++) l.push_back(i);
list<int>::iterator il=l.begin();
cout << "-----Print without accumlating------" << endl;
for (il=l.begin();il!=l.end();++il)
{
cout << (*il) << ", ";
}
cout << endl;
cout << "\ntest of sum function for list :" << Sum(l) << endl;
//*****************
// VECTOR CONTAINER
//*****************
cout << "\n\n\n\n----- sum of VECTOR CONTAINER ----- "<< endl;
double sumV=0;
vector <int> v;
for (int i=0;i<10;i++) v.push_back(i);
vector<int>::iterator iv=v.begin();
cout << "-----Print without accumlating------" << endl;
for (iv=v.begin();iv!=v.end();++iv)
{
cout << (*iv) << ", ";
}
cout << "\ntest of sum function for vector :"<< Sum(v) << endl;
//*****************
// MAP CONTAINER
//*****************
cout << "\n\n\n\n----- sum of MAP CONTAINER ----- "<< endl;
double sumM=0;
map <double, double > m;
for (int i=0;i<10;i++) m[i] = i*10;
map<double, double>::iterator im=m.begin();
cout << "\ntest of sum function for map :"<< Sum(m) << endl;
cout << "-----Print without accumlating------" << endl;
for (im=m.begin();im!=m.end();++im)
{
cout <<"("<< im->first << ", " << im->second<<")";
}
cout <<endl;
//*****************
// Sum of two CONTAINERs
//*****************
cout << "\ntest of sum function for sum between two containers :" <<Sum(v,l) << endl; // line 107
return 0;
}
|