custom vector assignment

given main code.

and I should make my custom vector class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include “my_vector.h”
void main() {
     myvector a(2);
     myvector b(2);
     cout << a.size();
     
     a[0] = 0.0; a[1] = 2.0;
     b[0] = 1.0; b[1] = 2.0; double c = a*b; \\(inner prod) 
     cout << c << endl; 

      myvectore=a+b;
      cout << e << endl; 
      myvector f = a‐b;
      cout << f << endl;
      double g = a.norm();

     a.resize(3);
     cout << a << endl;
}



there are too much error.
I don't know what to do!
help me.


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


#ifndef my_vector_h
#define my_vector_h

template<class T>
class myvector{
    
private:
    T vectorsize;
    T vectorData;
    T vectorCapacity;
    
    int xpos, ypos;
    
public:
    
    myvector<T>(const T&);
    T size() const;
    void norm();
    void resize(int resi);
    
};

template<class T>
myvector<T>::myvector(const T& n)
    {
        vectorCapacity = n;
        vectorData = new T [vectorCapacity];
        vectorsize = 0;
    }

template<class T>
T myvector<T>::size() const
{
    return vectorsize;
}
 /*   myvector(int x=0, int y=0) : xpos(x), ypos(y)
    {}
    
    
myvector operator+(const myvector &ref)
    {
        myvector pos(xpos+ref.xpos, ypos+ref.ypos);
        return pos;
    }
myvector operator*(const myvector &ref)
    {
        double result;
        result = xpos*ref.ypos + ypos*ref.xpos;
        return result;
    }
myvector operator-(const myvector &ref)
    {
        myvector pos(xpos-ref.xpos, ypos-ref.ypos);
        return pos;
    }
*/
Last edited on
If you want to use cout you need to include <iostream>. Note that cout is inside the std namespace.

Line comments use two forward slashes //.

An #ifndef has to be ended with an #endif.

In main line 13 you are using the wrong minus sign (U+2010). The correct minus sign (U+002D) is the one that you type with your keyboard.

The return type of main should be int (some old compilers use void).

myvector is a class template. To be able to use it as a class you need to specify the template argument, e.g. myvector<double>.

There are also some operators that you use but has not been defined yet.
vector size should not be T . what if you take another type than primitives? vector data should be a pointer , missing constructor with initializer list . no destructor to delete data, no push_back, really?
Topic archived. No new replies allowed.