Operations between two classes

I have wrote two template classes:

Vectors.h
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
#include "std_lib_facilities.h"
#include "Numbers.h"
#ifndef Vector_Advanced_Vectors_h
#define Vector_Advanced_Vectors_h

template <typename T>
vector<T> f(vector<T>& v1, vector<T>& v2)
{
    if (v1.size() != v2.size()) error("Impossible to compute the calculation, different sizes.");
    for (int i = 0; i < v1.size(); ++i)
    {
        v1[i] += v2[i];
    }
    
    return v1;
}

template <typename T>
void print_vec(const vector<T>& v)
{
    for (int i = 0; i < v.size(); ++i)
    {
        cout << i + 1 << ") "
        << v[i] << endl;
    }
}

template <typename T,typename U>
class mix
{
private:
    vector<T> vt;
    vector<U> vu;
public:
    mix() : vt{0}, vu{0} {};
    mix(vector<T> v1, vector<U> v2) : vt{v1}, vu{v2} {};
    ~mix() {};
    vector<U> f_mix(vector<T>& vt, vector<U>& vu);
    vector<T> f_mix_num(vector<Number<T>> vt, vector<Number<U>> vu);
    vector<T> get_v1() const {return vt;}
    vector<U> get_v2() const {return vu;}
    double sum_vec(const vector<U>& vu);
    void operator=(const mix<T,U>& n ) // Overloaded operator= (Special operator that should be as member function)
    {
        vt = n.vt;
        vu = n.vu;
    }
};

template <typename T, typename U>
vector<U> mix<T,U>::f_mix(vector<T>& vt, vector<U>& vu)
{
    if (vt.size() != vu.size()) error("Impossible to compute the calculation, different sizes.");
    vector<U> vu1 = vu;
    for (int i = 0; i < vt.size(); ++i)
    {
        vu1[i] = vt[i] * vu[i];
    }
    
    return vu1;
}

template <typename T, typename U>
vector<T> mix<T,U>::f_mix_num(vector<Number<T>> vt, vector<Number<U>> vu)
{
    if (vt.size() != vu.size()) error("Impossible to compute the calculation, different sizes.");
    vector<Number<T>> vu1 = vt;
    for (int i = 0; i < vt.size(); ++i)
    {
        vu1[i] = vt[i] * vu[i];
    }
    
    return vu1;
}

template <typename T, typename U>
double mix<T,U>::sum_vec(const vector<U>& vu)
{
    double result = 0.0;
    for (int i = 0; i < vu.size(); ++i)
    {
        result += vu[i];
    }
    
    return result;
}

/*
 template<>   // Specialization with both T and U specialized ---> Template typenames empty "<>" because
 // All specialized
 class mix<vector<int>,vector<double>>        // Specialization
 {
 vector<double> f_mix(vector<int>& vt, vector<double>& vu);
 vector<double> operator=(vector<int>& v1);
 };
 
 vector<double> mix<vector<int>,vector<double>>::f_mix(vector<int>& vt, vector<double>& vu)
 {
 if (vt.size() != vu.size()) error("Impossible to compute the calculation, different sizes.");
 vector<double> vu1;
 for (int i = 0; i < vt.size(); ++i)
 {
 double n = 0.0;
 n = vt[i] * vu[i];
 vu1.push_back(n);
 }
 
 return vu1;
 }
 */


#endif 


and Numbers.h
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
#ifndef Vector_Advanced_Numbers_h
#define Vector_Advanced_Numbers_h

#include <stdio.h>
#include "std_lib_facilities.h"

template<typename T>
class Number
{
private:
    T val1;
    T val2;
public:
    Number() : val1(0), val2(0) {};  // Default constructor
    Number(T val1, T val2) : val1(val1), val2(val2) {};    // Full constructor
    ~Number() {};    // Destructor
    T& get_val1() const {return val1;}
    T& get_val2() const {return val2;}
    friend ostream& operator<<(ostream& os, Number<T>& n)
    {
        return os << "val1 is: " << n.val1 << " "
        << "val 2 is: " << n.val2 << endl;
    }
    friend istream& operator>>(istream& is, Number<T>& n)
    {
        cout << "val1: ";
        is >> n.val1;
        cout << "val2: ";
        is >> n.val2;
        return is;
    }
    void operator=(const T& n ) // Overloaded operator= (Special operator that should be as member function)
    {
        val1 = n.val1;
        val2 = n.val2;
    }
    
    Number<T>& operator+(const Number<T>& n)
    {
        Number<T> n1;
        n1.val1 = this->val1 + n.val1;
        n1.val2 = this->val2 + n.val2;
        return n1;
    }
    Number<T>& operator-(const Number<T>& n)
    {
        Number<T> n1;
        n1.val1 = this->val1 - n.val1;
        n1.val2 = this->val2 - n.val2;
        return n1;
    }
    Number<T>& operator/(const Number<T>& n)
    {
        Number<T> n1;
        n1.val1 = this->val1 / n.val1;
        n1.val2 = this->val2 / n.val2;
        return n1;
    }
    Number<T>& operator*(const Number<T>& n)
    {
        Number<T> n1;
        n1.val1 = this->val1 * n.val1;
        n1.val2 = this->val2 * n.val2;
        return n1;
    }
    Number<T>& operator%(const Number<T>& n)
    {
        Number<T> n1;
        n1.val1 = this->val1 % n.val1;
        n1.val2 = this->val2 % n.val2;
        return n1;
    }
};

#endif 


Vectors performs operations between two different types of variables while Numbers holds two value of different types. I am trying to merge them but, when I am trying to calling the m.f_mix_num function, Xcode tells me "No viable conversion from 'Vector<Number<double>>' to 'Vector<Number<Number<double>>>'. I tried to rewrite the function in terms of "Number" but it does not work.
Here is my main.cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "std_lib_facilities.h"
#include "Vectors.h"
#include "Numbers.h"

int main(int argc, const char * argv[])
{
    Number<double> n;
    cin >> n;
    vector<Number<double>> v1;
    v1.push_back(n);
    Number<int> n1;
    cin >> n1;
    vector<Number<int>> v2;
    v2.push_back(n1);
    mix<Number<double>,Number<int>> m;
    vector<Number<double>> v3;
    v3 = m.f_mix_num(v1, v2);  // !! ERROR !!
    
    return 0;
}


Thanks!
Last edited on
leourb wrote:
Vectors performs operations between two different types of variables while Numbers holds two value of different types.
I don't understand what you mean? Both classes only work with one type at a time...?
Sorry if I expressed badly. One handles two different types, while the other just one. Vectors handles vector<T> and vector<U> while Numbers handles Number<T>. My aim is to create a mix<vector<Number<int>>,vector<Number<double>>> to multiply and sum using mix.f_mix function.
Last edited on
What is mix? It is not a standard library type and you have not shown its definition.
I have defined it in Vectors.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <typename T,typename U>
class mix
{
private:
    vector<T> vt;
    vector<U> vu;
public:
    mix() : vt{0}, vu{0} {};
    mix(vector<T> v1, vector<U> v2) : vt{v1}, vu{v2} {};
    ~mix() {};
    vector<U> f_mix(vector<T>& vt, vector<U>& vu);
    vector<T> f_mix_num(vector<Number<T>> vt, vector<Number<U>> vu);
    vector<T> get_v1() const {return vt;}
    vector<U> get_v2() const {return vu;}
    double sum_vec(const vector<U>& vu);
    void operator=(const mix<T,U>& n ) // Overloaded operator= (Special operator that should be as member function)
    {
        vt = n.vt;
        vu = n.vu;
    }
};


It can hold two different type of vectors!
This looks wrong:

vector<T> f_mix_num(vector<Number<T>> vt, vector<Number<U>> vu);

I think you probably meant this:

vector<T> f_mix_num(vector<T> vt, vector<U> vu);
Yep it was.. I have done! Thank you for your advices LB :)
Topic archived. No new replies allowed.