简体   繁体   中英

No matching function for call, but why?

I've nearly finished my smart pointer so I uploaded it to my univerity's website which runs a number of automated tests on my code.

The thing is I don't know what kind of tests are being excecuted. I am able to read the automated test's stdout which I did:

In instantiation of ‘my_pointer<T>::my_pointer() [with T = tester]’:
error: no matching function for call to ‘tester::tester()’
note: candidates are:
tester::tester(my_pointer<tester>)
candidate expects 1 argument, 0 provided
tester::tester(const tester&)
candidate expects 1 argument, 0 provided

So I'm guessing for some odd reason it won't invoke my my_pointer() constructor? This is my smart pointer class:

template<class T>
class my_pointer {
  T* raw_pointer;

public:
    my_pointer() {
        raw_pointer = new T();
        raw_pointer->incRefCnt();
    }

    my_pointer(T *obj) : raw_pointer(obj) {
        if(raw_pointer != NULL) raw_pointer->incRefCnt();
    }

    my_pointer(const my_pointer<T>& smart_pointer) : raw_pointer(smart_pointer.raw_pointer) {
        if(raw_pointer != NULL) raw_pointer->incRefCnt();
    }

    T& operator*() {
        return *raw_pointer;
    }

    T* operator->() {
        return raw_pointer;
    }

    operator T*() {
        return raw_pointer;
    }

    my_pointer<T> &operator=(const my_pointer<T> &smart_pointer) {
        if(this != &smart_pointer && raw_pointer != NULL) {
            /** if this was the last reference to the given memory address */
            if (raw_pointer->decRefCnt() == 0) {
                delete raw_pointer;                    
            }

            raw_pointer = smart_pointer.raw_pointer;
            raw_pointer->incRefCnt();
         }

        return *this;
    }

    bool operator== (const T* pointer) {
        return raw_pointer == pointer;
    }

    bool operator!= (const T* pointer) {
        return raw_pointer != pointer;
    }

    bool operator== (const my_pointer<T> &smart_pointer) {
        return raw_pointer == smart_pointer.raw_pointer;
    }

    bool operator!= (const my_pointer<T> &smart_pointer) {
        return raw_pointer != smart_pointer.raw_pointer;
    }

    ~my_pointer() {
       if(raw_pointer->decRefCnt() == 0 && raw_pointer != NULL) {
           delete raw_pointer;
       }
    }
};

This is a class, in which the references can be counted:

class refcounted {
private:
    int count;
public:
    refcounted() : count(0) { }

    int incRefCnt() {
        return ++count;
    }

    int decRefCnt() {
        return --count;
    }
};

Can you see any problems with the code? Thanks in advance!

It's what it says.

You're doing new T() (ie new tester() ), but there is no default constructor inside tester .

It is impossible to suggest solutions without knowing the assignment's constraints.

But, generally, you'd accept a T* into the smart pointer constructor so that the call site may be responsible for [dynamically] constructing it however is required. (An alternative would be a variadic template constructor, effectively mimicking standard "in-place" construction.)

You seem to be doing this already, so you could try removing the default constructor entirely… but clearly some part of the assignment requires it, otherwise it would not be being called.

Typically a default-constructed smart pointer would essentially do nothing. You could try that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM