Hello,
I have a sample code which is a header file and I am trying to write a CPP program which uses this file. But I am getting errors while running the code.

Here is the header file:

#ifndef FineInterfacePtr_h
#define FineInterfacePtr_h

template<class T>
class CSafePtr {
public:
    CSafePtr( T* _iface = 0 ) { attach( _iface ); }
    CSafePtr( const CSafePtr& other ) { attach( other.iface ); }
    ~CSafePtr() { release(); }

    bool IsNull() const { return iface == 0; }

    T** GetBuffer();    
    void** GetQIBuffer();

    T& operator *() const { return *iface; }
    T* operator ->() const { return iface; }

    operator T*() const { return iface; }

    CSafePtr& operator = ( const CSafePtr& other );
    CSafePtr& operator = ( T* _iface );

private:
    void attach( T* _iface );
    void release();

    T* iface;       
};

template<class T>
inline T** CSafePtr<T>::GetBuffer()
{   
    release();
    return &iface;  
}

template<class T>
inline void** CSafePtr<T>::GetQIBuffer()
{   
    release();      
    return reinterpret_cast<void**>(&iface); 
}

template<class T>
inline CSafePtr<T>& CSafePtr<T>::operator = ( const CSafePtr<T>& other )
{
    if( other.iface != iface ) {
        release();
        attach( other.iface );  
    }
    return *this;
}

template<class T>
inline CSafePtr<T>& CSafePtr<T>::operator = ( T* _iface )
{
    if( _iface != iface ) {
        release();
        attach( _iface );   
    }
    return *this;
}

template<class T>
inline void CSafePtr<T>::attach( T* _iface )
{   
    if( _iface != 0 ) {
        _iface->AddRef();   
    }
    iface = _iface; 
}

template<class T>
inline void CSafePtr<T>::release()
{   
    if( iface != 0 ) {
        iface->Release();   
    }
    iface = 0;  
}

#endif // FineInterfacePtr_h

In the .cpp file I have declared a object to the class as:
CSafePtr<int> myobject(int* t);

and when I try to call the function GetBuffer() I get the following error:
Structure required on the left side of . or .* in the function main().

Can anyone please let me know how to call the function GetBuffer()and why this error is coming.

Any help would be really appriciated.

Thank you,
Swathi

Edited 3 Years Ago by Dani: Formatting fixed

Should'nt declaring the object in C++ be like

int *t=...;
.
.
.
CSafePtr<int> myobject(t);

Rather than:

CSafePtr<int> myobject(int *t)

Correct me if am wrong but shouldnt this

#ifndef FineInterfacePtr_h
#define FineInterfacePtr_h

be something like this

#ifndef FINEINTERFACEPTR_H_INCLUDED
#define FINEINTERFACEPTR_H_INCLUDED

Correct me if am wrong

You're wrong :) It doesn't matter what you put behind the ifndef and define, as long as it's the same:

#ifndef whocares
#define whocares
//do stuff
#endif

at SwathiSangral:

I see what the problem is. Very interesting... I would swear that this should work

In the .cpp file I have declared a object to the class as: CSafePtr<int> myobject(int* t);

The compiler actually sees that as a function prototype, i.e. a function named myobject, taking an int pointer and returning CSafePtr<int>.
So, you'd need to change it as stephen84s already pointed out.
However, CSafePtr will not work with native types, since it issues calls to AddRef() and Release(). So you need to have classes/structs implementing those.

You're wrong :) It doesn't matter what you put behind the ifndef and define, as long as it's the same:

Thanks for that correction...so are you saying that its the name of the header file that matters in the header guard??

> so are you saying that its the name of the header file that matters in the header guard??
It doesn't matter what you use for a header guard name as long as it's unique. All that matters is that the symbol you define isn't defined anywhere else except in the header guard.

This article has been dead for over six months. Start a new discussion instead.