argument to the constructor Vector::Vector(int)

Hi Guys,

I define a type Vec4 as a vector of four floats.
I want to define a class Vector similar to Vec4 ,but with the size given as an argument to the constructor Vector::Vector(int)

How do I change Vec4?
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

struct floatt{
float f[4];
floatt(float f1, float f2, float f3, float f4)
{
f[0] = f1;
f[1] = f2;
f[2] = f3;
f[3] = f4;
}

floatt(const floatt &r)
{
f[0] = r.f[0];
f[1] = r.f[1];
f[2] = r.f[2];
f[3] = r.f[3];
	}
floatt &operator=(const floatt &r)
{
f[0] = r.f[0];
f[1] = r.f[1];
f[2] = r.f[2];
f[3] = r.f[3];
return *this;
}

};



class Vec4{
	vector<floatt> fval;
public:
	Vec4()
	{
		fval.push_back(floatt(rand() % 2 + 2.1f, rand() % 3 + 1.7f,rand() % 5 + 7.3f, rand() % 3 + 4.9f));
	}

	Vec4(floatt f)
	{
		fval.push_back(floatt(f));
	}
	Vec4(const Vec4 &f)
	{
		fval = f.fval;
	}

	Vec4 &operator=(const Vec4 &f)
	{
		fval =  f.fval;
		return *this;
	}


	void display()const
	{
		cout << "("<< fval[0].f[0] << ")" << "(" << fval[0].f[1] << ")" <<  "(" <<fval[0].f[2] << ")"<<"("<<fval[0].f[3]<< ")" << endl;

	}

	Vec4 operator+(const Vec4 &v)const
	{
		Vec4 result = *this;
		result.fval[0].f[0] += v.fval[0].f[0];
		result.fval[0].f[1] += v.fval[0].f[1];
		result.fval[0].f[2] += v.fval[0].f[2];
		result.fval[0].f[3] += v.fval[0].f[3];
		
		return result;
	}

	Vec4 operator-(const Vec4 &v)const
	{
		Vec4 result = *this;
		result.fval[0].f[0] -= v.fval[0].f[0];
		result.fval[0].f[1] -= v.fval[0].f[1];
		result.fval[0].f[2] -= v.fval[0].f[2];
		result.fval[0].f[3] -= v.fval[0].f[3];
		
		return result;
	}

	Vec4 operator/(const Vec4 &v)const
	{
		Vec4 result = *this;
		result.fval[0].f[0] /= v.fval[0].f[0];
		result.fval[0].f[1] /= v.fval[0].f[2];
		result.fval[0].f[2] /= v.fval[0].f[3];
		result.fval[0].f[3] /= v.fval[0].f[4];
		
		return result;
	}

	Vec4 operator*(const Vec4 &v)const
	{
		Vec4 result = *this;
		result.fval[0].f[0] *= v.fval[0].f[0];
		result.fval[0].f[1] *= v.fval[0].f[1];
		result.fval[0].f[2] *= v.fval[0].f[2];
		result.fval[0].f[3] *= v.fval[0].f[3];
		
		return result;
	}

	
	Vec4 &operator+=(const Vec4 &v)
	{
		this->fval[0].f[0] += v.fval[0].f[0];
		this->fval[0].f[1] += v.fval[0].f[1];
		this->fval[0].f[2] += v.fval[0].f[2];
		this->fval[0].f[3] += v.fval[0].f[3];

		return *this;

	}
	Vec4 &operator-=(const Vec4 &v)
	{
		this->fval[0].f[0] -= v.fval[0].f[0];
		this->fval[0].f[1] -= v.fval[0].f[1];
		this->fval[0].f[2] -= v.fval[0].f[2];
		this->fval[0].f[3] -= v.fval[0].f[3];

		return *this;

	}
	Vec4 &operator*=(const Vec4 &v)
	{
		this->fval[0].f[0] *= v.fval[0].f[0];
		this->fval[0].f[1] *= v.fval[0].f[1];
		this->fval[0].f[2] *= v.fval[0].f[2];
		this->fval[0].f[3] *= v.fval[0].f[3];

		return *this;

	}
	Vec4 &operator/=(const Vec4 &v)
	{
		this->fval[0].f[0] /= v.fval[0].f[0];
		this->fval[0].f[1] /= v.fval[0].f[1];
		this->fval[0].f[2] /= v.fval[0].f[2];
		this->fval[0].f[3] /= v.fval[0].f[3];

		return *this;

	}


	float &operator [] (int index)	
	{ 	

		return  this->fval[0].f[index];
	
	}

	friend ostream &operator<<(ostream &os, Vec4 &v);
	friend istream &operator>>(istream &is, Vec4 &v);



};


ostream &operator<<(ostream &os, Vec4 &v)
{
	return  os << "(" << v.fval[0].f[0] << ")"  << "(" << v.fval[0].f[1] << "(" << v.fval[0].f[2] << ")" << "(" << v.fval[0].f[3] << ")" <<   endl;

}

istream &operator>>(istream &is, Vec4 &v)
{
	return is >>  v.fval[0].f[0] >> v.fval[0].f[1] >> v.fval[0].f[2] >> v.fval[0].f[3];
}
There are several ways
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//using templates (vector<4> v;)
template<unsigned int n>
class vector{
private:
  float v[n];
};
//dynamical allocation
class vector{
private:
  float *v;
public:
  vector(unsigned int n){ v=new float[n]; }
//you will need to do the destructor and copy constructor too
};
//use std::vector
class vector{
private:
  std::vector<float> v;
  vector(unsigned int n){ v.resize(n); }
};

However you will need to modify all your functions (use loops)

Why are you always using f[0]? Just make one value instead of an array then.
how ?
how what?
Using loops:
1
2
3
4
5
6
Vector & operator+=(const Vector &v)const
	{
                for(int K=0; K<n; K++)
		    this->val[K] += v.val[K];
		return *this;
	}


Why an array?
1
2
3
4
5
class Vec4{
	//vector<floatt> fval; //you never use anything besides the first cell
         floatt fval;
//...
};
Last edited on
class Vec4{
//vector<floatt> fval; //you never use anything besides the first cell
floatt fval;
//...
};



I could not understand it?
I could not understand it?
Are you asking if you understand? (please, be more specific)

I suppose Vec4 is one vector of four components (coordinates, and weight maybe). in floatt you have already that information.

Only the first cell
1
2
3
4
result.fval[0].f[0] /= v.fval[0].f[0];
result.fval[0].f[1] /= v.fval[0].f[2];
result.fval[0].f[2] /= v.fval[0].f[3];
result.fval[0].f[3] /= v.fval[0].f[4];
1
2
3
4
 struct floatt{
 float f[4];
};
 


Is it that correct ?
1
2
3
4
 class Vec4{
 floatt fval;
};
 


Is it that correct ?

1
2
3
4
 class Vec4{
vector<floatt> fval;
};
 

Topic archived. No new replies allowed.