Unknown errors in a class with multiple constructors and deep copying

Vector.h
https://hastebin.com/zotejiqazu.cpp

Vector.cpp
https://hastebin.com/suxujumaxu.cpp

main.cpp
https://hastebin.com/jexezarage.cpp

I do not understand why there how to make the program output zeros for b.print and the first c.print. Also why there is an error when the program finishes the last c.print.
I do not know how to fix the problem that is in my constructors in the Vector.cpp file.
please delete the duplicated post http://www.cplusplus.com/forum/general/244066/
¿what's pch.h?

1
2
3
4
Vector::Vector(int s) {

	entries = new int[s];
}
you didn't set `size' there


1
2
3
4
Vector::Vector(const Vector &other) {
	size = other.size;
	entries=(new int[*other.entries]);
}
that's not how you copy arrays, you are trying to access the first element (which may not exist)


> I do not understand why there how to make the program output zeros for b.print and the first c.print
¿what's supposed to happen? I've got
[]
[0 0 0]
[0 0 0]
Invalid position. Position does not exist
Invalid position. Position does not exist
[1 2 3]
[1 2 3]
[1 0 3]
[1 2 3]
[]
Invalid position. Position does not exist
[]
[]
after fixing the constructors


> entries = new int[s];
by the way, you aren't setting the elements there
you reserve the memory but don't put any content, so they can have anything when you try to print them.
Last edited on
Topic archived. No new replies allowed.