954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ function call error

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 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
inline T** CSafePtr::GetBuffer()
{
release();
return &iface;
}

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

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

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

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

template
inline void CSafePtr::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 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

SwathiSangral
Newbie Poster
1 post since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

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

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


Rather than:

CSafePtr<int> myobject(int *t)
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

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
joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 
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


atSwathiSangral:

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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
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.
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.

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 
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??

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 

> 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.

Radical Edward
Posting Pro
545 posts since May 2008
Reputation Points: 361
Solved Threads: 97
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You