Dinamy array
Jun 3, 2013 at 4:52pm Jun 3, 2013 at 4:52pm UTC
So,I have the next code and what I'm trying to do is to get an element from a dynamic array but I get a" conversion from 'int' to non-scalar type 'Baloon' requested" error.I've tryed to return a `Baloon` object but that gives me errors in the main file.What am I doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
template <typename TElem> class Vector{
priv;te:
TElem* elems;
int size;
int capacity;
public :Vector();
~Vector();
void add(TElem e)
void del(int poz);
Vector(const Vector &dv);
Vector& operator =(const Vector& dv);
TElem* getElems();
TElem getElementByPosition(int pos) const ;
template <typename TElem>
TElem Vector<TElem>::getElementByPosition(int pos) const {
if (!(pos<0 || pos>this ->getSize()))
return this ->elems[pos];
else {
return NULL;
}
};
Jun 3, 2013 at 5:02pm Jun 3, 2013 at 5:02pm UTC
NULL and TElem are different types unless TElem is defined as NULL.
Last edited on Jun 3, 2013 at 5:02pm Jun 3, 2013 at 5:02pm UTC
Jun 3, 2013 at 5:07pm Jun 3, 2013 at 5:07pm UTC
I know that.How can I signal when the given position is out of range without returning a NULL?
Jun 3, 2013 at 5:08pm Jun 3, 2013 at 5:08pm UTC
For example you can use an exception.
Jun 3, 2013 at 5:18pm Jun 3, 2013 at 5:18pm UTC
I'm not really comfortable with throwing exceptions,I don't feel like I can handle them properly.Any other ideas?
Jun 3, 2013 at 5:25pm Jun 3, 2013 at 5:25pm UTC
You can define the function in other way. For example you can define it as returning bool and having an output parameter of type TElem.
Jun 3, 2013 at 5:39pm Jun 3, 2013 at 5:39pm UTC
Ok.Defining it to return that bool is fine but i need that function to take an int when it is called(the position).Can I return only one of the 2 parameters by refference?
Topic archived. No new replies allowed.