0

I have a class called arr and it has a function named _union written like this:

template<class T>
arr<T> *arr<T>::_union(arr<T> B) {
    arr<T> *C(this->length + B._length());
    bool isPresent = false;
    for (int i = 0; i < length; i++)
        C->push_back(this->get(i));
    for (int j = 0; j < B._length(); j++) {
        for (int k = 0; k < C->_length(); k++) {
            if (B.get(j) == C->get(k))
                isPresent = true;
        }
        if (!isPresent)
            C->push_back(B.get(j));
        isPresent = false;
    }

    return C;
}

The function returns a pointer of an object that was newly created inside this function's scope.

In main function, I wrote code like this :

arr<int> *a3 = a1._union(a2);
a3->display();

When I run, this gives me an error:

enter image description here

What is the problem here? If I don't use any pointers and just return normal object then everything is fine.

Please help me. Also I don't have any copy constructers inside the class. I am just trying to create my own array class with data and functions.

3
  • Note: The length in C will be wrong in those cases where the containers contain elements that compare equal (since you don't push_back duplicates). Commented Feb 8, 2021 at 7:18
  • I am using two data variables for this class i.e. SIZE and LENGTH. SIZE is max limit and LENGTH is current size. Commented Feb 8, 2021 at 9:58
  • The constructor is taking SIZE, but push_back increases length by 1, which was initially 0. Commented Feb 8, 2021 at 9:59

1 Answer 1

3

In this code

arr<T> *C(this->length + B._length());

C is a pointer and this->length + B._length() is an integer, hence the error. You can't assign an integer to a pointer.

I guess you were trying to write this code

arr<T> *C = new arr<T>(this->length + B._length());

This code allocates a new arr<T> object using new and calls the a constructor for that object using the integer parameter this->length + B._length().

However is usually a bad idea to use dynamic allocation like this. You should think about redesigning your function without using pointers.

template<class T>
arr<T> arr<T>::_union(arr<T> B) {
    arr<T> C(this->length + B._length());
    ...
    return C;
}

This will require you to define a copy constructor etc for arr<T>. But that is normal C++ programming you shouldn't be reluctant to do it.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh yeah. Writing such huge class might have made me forgot small things. Thank you so much. P.S. I am new to programming :).
@Bhuvansai: be careful... quite a big part of programming is about small things (details). A fantastic design can still end up in horrible software if most details are not right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.