未解决的外部符号错误LNK2019

时间:2014-11-11 21:31:35

标签: c++ unresolved-external

我是C ++的新手,在我写了一些智能指针后,我发现了以下错误: 错误1错误LNK2019:未解析的外部符号" public:__ thishisall RC :: RC(void)" (?? 0RC @@ QAE @ XZ)在函数_main中引用 我读过这是因为它无法找到一些外部来源。但我不使用任何外部源代码,下面的代码在一个文件中。如何修复此错误消息?

我的代码:

#include <iostream>

using namespace std;

class RC
{
    private:
    int count; // Reference count

    public:
    RC();

    int incRefCnt()
    {
        count++;
        return count;
    }

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


template < class T > class my_pointer
{
    private:

    T* reference;       /** pointer */

    public:

    my_pointer();

    my_pointer(T* reference)
    {
        reference->incRefCnt();
    }

    my_pointer(const my_pointer<T>& sp) : reference(sp.reference)
    {
        reference->incRefCnt();
    }

    ~my_pointer()
    {
        if (reference->decRefCnt() == 0)
        {
            delete reference;
        }
    }

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

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

    my_pointer<T>& operator = (const my_pointer<T>& sp)
    {
        if (this != &sp) 
        {
            if (reference->decRefCnt() == 0)
            {
                delete reference;
            }

            reference = sp.reference;
            reference->incRefCnt();
        }

        return *this;
    }
};

void main()
{
    my_pointer<RC> obj1 = new RC();
    my_pointer<RC> obj2 = obj1;
}

0 个答案:

没有答案