Funtion that multiplies every element of array type point

This is code for multiplying all array elements type Point with integer

1
2
3
4
5
6
7
8
  Vector Vector::Multiply_with_m(int m)
 {
	 Vector v(this->dim);
	 int i;
	 for (i = 0; this->dim; i++)
v.vector[i] = this->vector[i] * m;//here program fails  Access violation writing location
	 return v;
 }

//vector[i]is type Point
I have defined operator functions for = and *.Also i have constructor definition.
Last edited on
for (int i = 0; i < this->dim; i++)

Some thoughts:

1. Why declare 'int i' outside of your loop?
2. Creating a local copy of your object and returning by value can be expensive.
It says exception unhelded and i do not know hot to eliinate that mistake.Whole message
Unhandled exception at 0x012E5693 in Idemo.exe: 0xC0000005: Access violation writing location 0x0060E000.
We have done in classes for declaring outside the loop.
Last edited on
Changing this:
for (i = 0; this->dim; i++)
to this:
for (i = 0; i < this->dim; i++)
does not work? If not, then we need to see more code. The minimum amount of code which compiles and reproduces the error would make it easier to find the problem.

this->dim always evaluates as true if the array size is > 0. I'm assuming this->dim is the size of the array, so you need i < this->dim. Sorry I wasn't explicit in my last post.
Last edited on
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
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<iostream>
#include"Vektor.h"
#include<stdlib.h>
#include<math.h>

Vector::Vector()
{
	this->dim = 0;
}
Vector::Vector(int vel)
{
	if (vel <= 0)
	{
		std::cout << "Wrong value \n";
		exit(1);
	}
	this->dim = vel;
	vector = new Tacka[vel];
}
Vector::Vector(const Vector &p)
{
	int i;
	this->dim = p.dim;
	vector = new Tacka[p.dim];
	for (i = 0; i < dim; i++)
		vector[i] = p.vector[i];
}
Vector::~Vector()
{
	if (vector != 0)
		delete[] vector;
}
inline void Vector::set(int k)
{
	this->dim = k;
}
inline Tacka Vector::get(int k) const
{
	Tacka a;
	a=vector[k];//Izmine
	return a;

}
void Vector::arrange()
{
	int i, j;
	Tacka pom;
	for (i = 0; i<dim - 1; i++)

		for (j = i + 1; j<dim; j++)
			if (vector[i] < vector[j])//Definesat <
			{
				pom = vector[i];
				vector[i] = vector[j];
				vector[j] = pom;
			}
}
Tacka Vector::Middle_value()//returns value closest to the middle value //ispravi tacak
{
	int  i;
	Tacka mid , min;
	for (i = 0; i < dim; i++)
		mid = (mid + vector[i])/dim;//operator +
	min = vector[0] - mid;
	for (i = 1; i < dim; i++)
		if (min > (vector[i] - mid))//- i vece
			min = vector[i] - mid;
	return min;
}
Vector Vector::Connect(const Vector &p)//connects two arraays in one
{
	int i, j;
	Vector newvector(this->dim + p.dim);
	for (i = 0; i < this->dim; i++)
		newvector.vector[i] = this->vector[i];
	for (j = 0; j < p.dim; j++)
		newvector.vector[j + i] = p.vector[j];
	return newvector;
}
Vector Vector::Saberi(const Vector &v1)
{
	int i;
	Vector v(v1.dim);
	if (v1.dim == this->dim)
		for (i = 0; i < v1.dim; i++)
			v.vector[i] = v1.vector[i] + this->vector[i];
	return v;
}
void Vector::Izbaci_Uredi(Vector v)
{
	int i, j,k;
	Tacka pom;
	for(i=0;i<v.dim-1;i++)
		for(j=1;j<v.dim;j++)
			if (v.vector[i] == v.vector[j])
			{
				k = i;
				for (k = 0; k < i; k++)
					v.vector[i+1] = v.vector[i];
				v.dim = v.dim - 1;
			}

	for(i=0;i<v.dim-1;i++)
		for(j=1;j<v.dim;j++)
			if (v.vector[i] > v.vector[j])
			{
				pom = v.vector[i];
				v.vector[i] = v.vector[j];
				v.vector[j] = pom;
			}


}

void Vector::Rotiraj_levo(Vector v)
{
	int i;
	Tacka pom;
	pom = v.vector[0];
	for (i = 1; i < v.dim; i++)
		v.vector[i] = v.vector[i + 1];
	v.vector[dim] = pom;

}

void Vector::Rotiraj_desno(Vector v)
{
	int i;
	Tacka pom;
	pom = v.vector[dim];
	for (i = v.dim-1; i = 0; i--)
		v.vector[i+1] = v.vector[i];
	v.vector[0] = pom;

}
 Vector Vector::operator=(const Vector &v)
{
	 Vector v1(v.dim);
	 Tacka *vector1;
	 int dim1;
	 dim1 = v.dim;
	 vector1 =new Tacka[v.dim];
	 v1.dim = dim1;
	 v1.vector[dim1] = *vector1;
	 return v1;
}
 ostream& operator<<(ostream &out, const Vector &v)
 {
	 int i;
	 for (i = 0; i < v.dim; i++)
		 out << v.vector[i] << endl;
	 return out;
 }
 istream& operator>>(istream &in, Vector &v)
 {
	 int i;
	 for (i = 0; i < v.dim; i++)
		 in >> v.vector[i];
	 return in;
 }
 Vector Vector::Pomnozi(const Vector &v)
 {
	 Vector v1;
	 int i;
	 if (v.dim == this->dim)
		 for (i = 0; i < v1.dim; i++)
			 v1.vector[i] = v1.vector[i] * this->vector[i];
	 return  v1;

 }
 Vector Vector::Saberi_sa_n(int n)
 {
	 Vector v(this->dim);
	 int i;
	 for (i = 0; i < this->dim; i++)
		 v.vector[i] = this->vector[i] + n;

	 return v;
 }
 Vector& Vector::Add_with_m(int m)//I translated this function some functions are on my language
 {
	 Vector v(this->dim);
	 for (int i = 0;i< this->dim; i++)
		 v.vector[i] = this->vector[i] * m;

	 return v;
 }

Now it calls bad alloc
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
#pragma once
using namespace std;
#include<iostream>
#include"Klasa.h"

class Vector {
private:Tacka * vector;
		int dim;
public:
	Vector();
	Vector(int vel);
	Vector(const Vector &p);
	~Vector();
	inline void set(int k);
	inline Tacka get(int k) const;
	void arrange();
	Tacka Middle_value();
	Vector Connect(const Vector &p);
	Vector Saberi(const Vector &v1);
	void Izbaci_Uredi(Vector v);
	void Rotiraj_levo(Vector v);
	void Rotiraj_desno(Vector v);
	 Vector operator=(const Vector &v);
	 friend istream& operator>>(istream &in, Vector &v);
	 friend ostream& operator<<(ostream &out, const Vector &v);
	 Vector Pomnozi(const Vector &v);
	 Vector Saberi_sa_n( int n);
	 Vector& Add_with_m(int m);
};


and this are point classes and cpp
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
#pragma once
#include <iostream>
using namespace std;

class Tacka
{
private:
	double m_x;
	double m_y;
	double m_z;
public:
	Tacka();
	Tacka(const Tacka &p);
	void Unos(double x, double y, double z);
	void Mnozenje(int x);
	void Stampaj() const;
	Tacka Sabiranje(const Tacka &p);
	Tacka Oduzimanje(const Tacka &p);
	~Tacka();
	friend Tacka operator*(const Tacka &t1, int m);
	friend Tacka operator*(const Tacka &t1, const Tacka &t2);
	friend bool operator<(const Tacka &t1, const Tacka &t2);
	friend Tacka operator+(const Tacka &t1, const Tacka &t2);
	friend Tacka operator+(const Tacka &t1, int n);
	friend Tacka operator/(const Tacka &t1, int value);
	friend Tacka operator-(const Tacka &t1, const Tacka &t2);
	friend bool operator>(const Tacka&t1, const Tacka &t2);
	friend bool operator==(const Tacka&t1, const Tacka &t2);
	friend istream& operator>>(istream &in, Tacka &t);
	friend ostream& operator<<(ostream &out, const Tacka &t);
};

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
#include<iostream>
#include<cmath>
#include "Klasa.h"
using namespace std;
Tacka::Tacka()
{
	m_x = 0.;
	m_y = 0.;
	m_z = 0.;

}
Tacka::Tacka(const Tacka&p)
{
	m_x = p.m_x;
	m_y = p.m_y;
	m_z = p.m_z;


}
void Tacka::Unos(double x, double y, double z)
{
	m_x = x;
	m_y = y;
	m_z = z;
}
void Tacka::Mnozenje(int x)
{
	m_x *= x;
	m_y *= x;
	m_z *= x;

}
void Tacka::Stampaj() const
{
	cout << "Vrednosti dimenzija su \n";
	cout << "x= " << m_x << ",y=" << m_y << ",z=" << m_z << endl;
}
Tacka Tacka::Sabiranje(const Tacka &p)
{
	double x, y, z;

	x = this->m_x + p.m_x;
	y = this->m_y + p.m_y;
	z = this->m_z + p.m_z;
	Tacka novatacka;
	novatacka.Unos(x, y, z);
	return novatacka;
}
Tacka Tacka::Oduzimanje(const Tacka &p)
{
	Tacka novatacka;
	double x, y, z;
	x = this->m_x - p.m_x;
	y = this->m_y - p.m_y;
	z = this->m_z - p.m_z;
	novatacka.Unos(x, y, z);
	return novatacka;
}
Tacka::~Tacka() {

}

Tacka operator*(const Tacka & t1, const Tacka & t2)
{ 
	double x, y, z;
	Tacka t;
	x = t1.m_x * t2.m_x;
	y = t1.m_y * t2.m_y;
	z = t1.m_z * t2.m_z;
	t.Unos(x, y, z);
	return t;
	
}

bool operator<(const Tacka &t1, const Tacka &t2)
{
	double x,y;
	x = sqrt((t1.m_x)*(t1.m_x) + (t1.m_y)*(t1.m_y) + (t1.m_z)*(t1.m_z));
	y = sqrt((t2.m_x)*(t2.m_x) + (t2.m_y)*(t2.m_y) + (t2.m_z)*(t2.m_z));
	return x < y;
}
Tacka operator+(const Tacka &t1,const Tacka &t2)
{
	double x, y, z;
	Tacka t;
	x = t1.m_x + t2.m_x;
	y = t1.m_y + t2.m_y;
	z = t1.m_z + t2.m_z;
	t.Unos(x, y, z);
	return t;

}
Tacka operator/(const Tacka &t1, int value)
{
	double x, y, z;
	Tacka t;
	x = t1.m_x / value;
	y = t1.m_y / value;
	z = t1.m_z / value;
	t.Unos(x, y, z);
	return t;
}
Tacka operator-(const Tacka &t1, const Tacka &t2)
{
	double x, y, z;
	Tacka t;
	x = t1.m_x - t2.m_x;
	y = t1.m_y - t2.m_y;
	z = t1.m_z - t2.m_z;
	return t;

}
bool operator>(const Tacka &t1, const Tacka &t2)
{
	double x, y;
	x = sqrt((t1.m_x)*(t1.m_x) + (t1.m_y)*(t1.m_y) + (t1.m_z)*(t1.m_z));
	y = sqrt((t2.m_x)*(t2.m_x) + (t2.m_y)*(t2.m_y) + (t2.m_z)*(t2.m_z));
	return x > y;
}
bool operator==(const Tacka &t1, const Tacka &t2)
{
	return (t1.m_x == t2.m_x && t1.m_y == t2.m_y && t1.m_z == t2.m_z);

}

istream & operator>>(istream & in, Tacka & t)
{
	in >> t.m_x;
	in >> t.m_y;
	in >> t.m_z;
	return in;
}

ostream & operator<<(ostream & out, const Tacka & t)
{
	out << "Tacka(" << t.m_x << ',' << t.m_y << ',' << t.m_z << ')' << endl;
	return out;
}
Tacka operator+(const Tacka &t1, int n)
{
	Tacka t;
	t.m_x = t1.m_x + n;
	t.m_y = t1.m_y + n;
	t.m_z = t1.m_z + n;
	return t;
}
Tacka operator*(const Tacka &t1, int m)
{
	Tacka t;
	t.m_x = t1.m_x * m;
	t.m_y = t1.m_y * m;
	t.m_z = t1.m_z * m;

	return t;
}


Last edited on
Did you try running it through a debugger? That will often give you the exact line of code the program crashes on.

This is a strange way to overload the '=' operator. I'm not sure this is doing what you've intended. If you want it to behave the way the '=' operator does in the C++ language, you should copy the contents of Vector v to *this.

1
2
3
4
5
6
7
8
9
10
11
 Vector Vector::operator=(const Vector &v)
{
	 Vector v1(v.dim);
	 Tacka *vector1;
	 int dim1;
	 dim1 = v.dim;
	 vector1 =new Tacka[v.dim];
	 v1.dim = dim1;
	 v1.vector[dim1] = *vector1;
	 return v1;
}

Here you created Vector v1 with the default constructor when I think you meant to create a Vector with a size. This will definitely cause problems.

1
2
3
4
5
6
7
8
9
10
 Vector Vector::Pomnozi(const Vector &v)
 {
	 Vector v1;
	 int i;
	 if (v.dim == this->dim)
		 for (i = 0; i < v1.dim; i++)
			 v1.vector[i] = v1.vector[i] * this->vector[i];
	 return  v1;

 }


This function is incomplete. Notice you created Tacka t, but didn't do anything with it.

1
2
3
4
5
6
7
8
9
10
Tacka operator-(const Tacka &t1, const Tacka &t2)
{
	double x, y, z;
	Tacka t;
	x = t1.m_x - t2.m_x;
	y = t1.m_y - t2.m_y;
	z = t1.m_z - t2.m_z;
	return t;

}
Thanj you very much for the answers.It really helped me.But now I don't have clue how to read elements from Text.txt into my objects .If for example text has numbers 1 2 1 3 2 1 4 2.
http://www.cplusplus.com/reference/fstream/fstream/

A simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream infile("data.txt");
    int x, y, z;
    while (!infile.eof())
    {
        infile >> x >> y >> z;
        cout << '(' << x << ", " << y << ", " << z << "), ";
    }
    infile.close();
    return 0;
}


Input: 1 2 1 3 2 1 4 2 5
Output: (1, 2, 1), (3, 2, 1), (4, 2, 5), 
Last edited on
I appreciate your help.Thank you.
There's are errors:
1
2
3
4
5
6
7
8
9
10
void Vector::Rotiraj_desno(Vector v)
{
    int i;
    Tacka pom;
    pom = v.vector[dim];
//  for (i = v.dim-1; i = 0; i--) // wrong
    for (i = v.dim-1; i >= 0; i--) // corrected
        v.vector[i+1] = v.vector[i];
    v.vector[0] = pom;
}


Your assignment is broken. This:
1
2
3
4
5
6
7
8
9
10
11
Vector Vector::operator=(const Vector &v)
{
     Vector v1(v.dim);
     Tacka *vector1;
     int dim1;
     dim1 = v.dim;
     vector1 =new Tacka[v.dim];
     v1.dim = dim1;
     v1.vector[dim1] = *vector1;
     return v1;
}

should be:
1
2
3
4
5
6
7
8
9
10
11
12
Vector& operator=(const Vector &v)
{
    if (this != &v) {
        if (dim > 0)
            delete [] vector;
        vector = new Tacka[v.dim];
        dim = v.dim;
        for (int i = 0; i < dim; ++i)
            vector[i] = v.vector[i];
    }
    return *this;
}


Of course, Vector::Pomnozi needs fixing:
1
2
3
4
5
6
7
8
9
10
Vector Vector::Pomnozi(const Vector &v)
{
    if (v.dim != dim)
        throw std::runtime_error("v.dim != dim");

    Vector v1(dim);
    for (int i = 0; i < dim; i++)
        v1.vector[i] = v.vector[i] * vector[i];
     return  v1;
}


You don't need to use this-> to get to a member.

The code is a little careless in places, just review it and fix it. You didn't post your code that generated the original the error, so I can't fix that.
Last edited on
Topic archived. No new replies allowed.