Derived Vector Class

My class is causing an error explosion...
Hundreds of errors from: .../bits/vector.tcc
What is wrong with it?

I am probably not understanding how " : " works, but I though that I was just adding methods to std::vector.

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
#ifndef _VECTOR_H
#define	_VECTOR_H

#include <cstdlib>
#include <cmath>
#include <vector>

template <class T>
class Vector : public std::vector<T>
{
    void operator+=(const std::vector<T>& x)
    {
        size_t n=this->size();
        for(size_t i=0; i<n; i++){ this[i]+=x[i]; }
    }
    std::vector<T> operator+(const std::vector<T>& x) const
    {
        std::vector<T> temp(this);
        temp+=x;
        return temp;
    }
    
    void operator-=(const std::vector<T>& x)
    {
        size_t n=this->size();
        for(size_t i=0; i<n; i++){ this[i]-=x[i]; }
    }
    std::vector<T> operator-(const std::vector<T>& x) const
    {
        std::vector<T> temp(this);
        temp-=x;
        return temp;
    }
    
    T operator*(const std::vector<T>& x) const
    {
        size_t n=x.size();
        T sum=0;
        for(size_t i=0;i<n;i++){ sum+=this[i]*x[i]; }
        return sum;
    }
    
    T norm2(double p=2) const
    {
        long double sum=0;
        size_t n=this->size();
        for(size_t i=0;i<n;i++){ sum+=abs( pow(this[i],p) ); }
        return sum;
    }
    T norm(double p=2){ return pow( this->norm2(p), 1/p ); }
    
    T min() const
    {
        int n=this->size();
        T tmp, min=fabs(this[0]);
        for(int i=1;i<n;i++)
        {
            tmp=abs(this[i]);
            if(min>tmp) { min=tmp; }
        }
        return min;
    }
    T max() const
    {
        int n=this->size();
        T tmp, max=fabs(this[0]);
        for(int i=1;i<n;i++)
        {
            tmp=abs(this[i]);
            if(max<tmp) { max=tmp; }
        }
        return max;
    }
         

};

#endif	/* _VECTOR_H */
Last edited on
I see a few logical mistakes but nothing that would throw a compiler error. I remember I tried to do something like this long long ago as well, but don't remember if I was successful or not.

What errors are you getting?

One of the logic problems I saw:

this[i] doesn't do what you're expecting. 'this' is a pointer to a vector, so by doing this[i] you're treating it as if it were an array of vectors -- rather than accessing an individual element. You probably meant to do: (*this)[i]. This comes up several times in your code.

Last edited on
I think I figured it out...
I have to derive new constructors as well:
1
2
Vector(size_t size) : std::vector(size) {}
Vector(size_t size, T val) : std::vector(val,size) {}



In principle, a derived class inherits every member of a base class except:

* its constructor and its destructor
* its operator=() members
* its friends

Although the constructors and destructors of the base class are not inherited themselves, its default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed.


http://www.cplusplus.com/doc/tutorial/inheritance.html


edit: or not! (figured out) still have an exploding compiler
Last edited on
IMHO you would be better off making most of these free functions rather than trying to derive from vector<>.

Eg, most of your operators allow the righthand side to be vector<> type but not Vector type, unless you have a Vector constructor that takes a vector<>.

Your += and -= operators are also not intuitive... they should return a Vector&.
Plus std::vector does not have a virtual destructor.
I decided to go with jsmith...but it would have been nice if my derivation had worked.

@seymore
I know nothing about virtual ...

this works in case anyone wants code:
(comments still appreciated)

vectorFunc.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
#ifndef _VECTORFUNC_H
#define	_VECTORRUNC_H

#include <cstdlib>
#include <cmath>
#include <vector>

template <class T>
std::vector<T>& operator+=(std::vector<T>& x, const std::vector<T>& y)
{
    size_t n=x.size();
    for(size_t i=0; i<n; i++){ x[i]+=y[i]; }
    return x;
}
template <class T>
std::vector<T> operator+(const std::vector<T>& x, const std::vector<T>& y)
{
    std::vector<T> temp(x);
    temp+=y;
    return temp;
}

template <class T>
std::vector<T>& operator-=(std::vector<T>& x, const std::vector<T>& y)
{
    size_t n=x.size();
    for(size_t i=0; i<n; i++){ x[i]-=y[i]; }
    return x;
}
template <class T>
std::vector<T> operator-(const std::vector<T>& x, const std::vector<T>& y)
{
    std::vector<T> temp(x);
    temp-=y;
    return temp;
}

template <class T>
T operator*(const std::vector<T>& x, const std::vector<T>& y)
{
    size_t n=y.size();
    T sum=0;
    for(size_t i=0;i<n;i++){ sum+=x[i]*y[i]; }
    return sum;
}

template <class T>
T norm2(const std::vector<T>& x, long double p=2)
{
    T sum=0;
    size_t n=x.size();
    for(size_t i=0;i<n;i++){ sum+=abs( pow((long double)x[i],p) ); }
    return sum;
}
template <class T>
T norm(const std::vector<T>& x, long double p=2.0)
{ return pow( (long double)norm2(x,p), 1/p ); }

template <class T>
T min(const std::vector<T>& x)
{
    int n=x.size();
    T min=fabs(x[0]);
    for(int i=1;i<n;i++){ if(min>abs(x[i])) { min=abs(x[i]); } }
    return min;
}
template <class T>
T max(const std::vector<T>& x)
{
    int n=x.size();
    T max=fabs(x[0]);
    for(int i=1;i<n;i++){ if(max<abs(x[i])) { max=abs(x[i]); } }
    return max;
}

#endif	/* _VECTORFUNC_H */



note that you can send a negative value of p to norm, too lazy too write protection.
Last edited on
I notice that you have min and max functions. You don't need them. Reference std::min_element and std::max_element; these do exactly what you are doing.

If you decide to keep them nonetheless, I'd suggest
* renaming the local variables min and max to something else
* renaming your min and max functions to something else

(Both because <algorithm> already contains templated functions called min
and max).

Note that your min and max functions access element 0 even if the
vector is empty. Probably want to fix that.
Topic archived. No new replies allowed.