how to pass smart pointes to function.

L

lokb

Hi,
I have a structure which and defined a smart pointer to the structure.

/* Structure of Begin Document Index Record */
typedef struct BDI_Struct{
unsigned char rname;
unsigned short int rlen;
int code;
short int reserved;
char indexName[9];
unsigned char tripLen;
unsigned char tripCode;
TRIPLET01 t01;
}BDIStruct;

/* Structure of Index Element Record */
typedef struct record{
unsigned char rname;
unsigned short int rlen;
int code;
short int reserved;
unsigned char tripLen;
unsigned char tripCode;
union
{
TRIPLET01 t01;
TRIPLET57 t57;
TRIPLET2D t2D;
TRIPLET36 t36;
TRIPLET02 t02;
TRIPLET80 t80;
TRIPLETGEN tgen;
}TRec;
}IndexStruct;

Defined the smart pointer as below:
where ObjVar returns the pointer.
typedef ObjVar<IELStruct> IELStruct_sptr;
typedef ObjVar<BDIStruct> BDIStruct_sptr;

Now when i am passing the smart pointers to the function i get compilation
errors
I am passing the smart pointers as

int vValidateIndModcaFile(ObjVar<BDIStruct>& bdirec,ObjVar<IndexStruct>&
ielrec);

Is it the right way to pass the smart pointers.

Could you plz guide me.

I am getting the erros like

"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 68.11:
1540-1101 (W) A return value of type "BDI_Struct *" is expected.
"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 63.12:
1540-0700 (I) The previous message was produced while processing
"ObjVar<BDI_Struct>::eek:perator->()".
"IndOutModcaParser.cpp", line 1814.5: 1540-0700 (I) The previous message
was produced while processing
"CIndOutModcaParser::vValidateIndModcaFile(ObjVar<BDI_Struct> &,
ObjVar<record> &)".
"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 68.11:
1540-1101 (W) A return value of type "record *" is expected.
"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 63.12:
1540-0700 (I) The previous message was produced while processing
"ObjVar<record>::eek:perator->()".
"IndOutModcaParser.cpp", line 1814.5: 1540-0700 (I) The previous message
was produced while processing
"CIndOutModcaParser::vValidateIndModcaFile(ObjVar<BDI_Struct> &,
ObjVar<record> &)"

Thanks,
Lokb
 
L

lokb

forgot to ad i am declaring the smart pointers as

BDIStruct_sptr bdirec;
IndexStruct_sptr ielrec;

caling function as

iRet = vValidateIndModcaFile(bdirec,ielrec);
 
V

Victor Bazarov

lokb said:
I have a structure which and defined a smart pointer to the structure.

/* Structure of Begin Document Index Record */
typedef struct BDI_Struct{
unsigned char rname;
unsigned short int rlen;
int code;
short int reserved;
char indexName[9];
unsigned char tripLen;
unsigned char tripCode;
TRIPLET01 t01;
}BDIStruct;

/* Structure of Index Element Record */
typedef struct record{
unsigned char rname;
unsigned short int rlen;
int code;
short int reserved;
unsigned char tripLen;
unsigned char tripCode;
union
{
TRIPLET01 t01;
TRIPLET57 t57;
TRIPLET2D t2D;
TRIPLET36 t36;
TRIPLET02 t02;
TRIPLET80 t80;
TRIPLETGEN tgen;
}TRec;
}IndexStruct;

Defined the smart pointer as below:
where ObjVar returns the pointer.
typedef ObjVar<IELStruct> IELStruct_sptr;

What's "IELStruct"? What's ObjVar? I can only see the definitions
of "BDIStruct", "BDI_Struct", "IndexStruct", and "record". No other
types have been defined in the global namespace.
typedef ObjVar<BDIStruct> BDIStruct_sptr;

Now when i am passing the smart pointers to the function i get compilation
errors
I am passing the smart pointers as

int vValidateIndModcaFile(ObjVar<BDIStruct>& bdirec,ObjVar<IndexStruct>&
ielrec);

Is it the right way to pass the smart pointers.

Could you plz guide me.

I am getting the erros like

"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 68.11:
1540-1101 (W) A return value of type "BDI_Struct *" is expected.

That seems to be a warning "(W)". And you haven't provided the code
in 'sptr.hpp' so that we could see why the warning is given.
"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 63.12:
1540-0700 (I) The previous message was produced while processing
"ObjVar<BDI_Struct>::eek:perator->()".

It seems that

ObjVar<BDI_Struct>::eek:perator->()

does not return BDI_Struct*, but instead returns something else
that compiler doesn't like.
"IndOutModcaParser.cpp", line 1814.5: 1540-0700 (I) The previous message
was produced while processing
"CIndOutModcaParser::vValidateIndModcaFile(ObjVar<BDI_Struct> &,
ObjVar<record> &)".
"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 68.11:
1540-1101 (W) A return value of type "record *" is expected.

Same thing here. We'd need to see the line 68.11 of 'sptr.hpp'.
"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 63.12:
1540-0700 (I) The previous message was produced while processing
"ObjVar<record>::eek:perator->()".

This again seems to indicate that the template's operator-> does
not return what is expected of it.
"IndOutModcaParser.cpp", line 1814.5: 1540-0700 (I) The previous message
was produced while processing
"CIndOutModcaParser::vValidateIndModcaFile(ObjVar<BDI_Struct> &,
ObjVar<record> &)"

The declaration of the 'vValidateIndModcaFile' function is not to
blame. More likely the 'ObjVar' template is not up to par with what
the compiler expects from it.

Victor
 
J

John Harrison

Hi,
I have a structure which and defined a smart pointer to the structure.

/* Structure of Begin Document Index Record */
typedef struct BDI_Struct{
unsigned char rname;
unsigned short int rlen;
int code;
short int reserved;
char indexName[9];
unsigned char tripLen;
unsigned char tripCode;
TRIPLET01 t01;
}BDIStruct;

/* Structure of Index Element Record */
typedef struct record{
unsigned char rname;
unsigned short int rlen;
int code;
short int reserved;
unsigned char tripLen;
unsigned char tripCode;
union
{
TRIPLET01 t01;
TRIPLET57 t57;
TRIPLET2D t2D;
TRIPLET36 t36;
TRIPLET02 t02;
TRIPLET80 t80;
TRIPLETGEN tgen;
}TRec;
}IndexStruct;

Defined the smart pointer as below:
where ObjVar returns the pointer.
typedef ObjVar<IELStruct> IELStruct_sptr;
typedef ObjVar<BDIStruct> BDIStruct_sptr;

Now when i am passing the smart pointers to the function i get
compilation
errors
I am passing the smart pointers as

int vValidateIndModcaFile(ObjVar<BDIStruct>& bdirec,ObjVar<IndexStruct>&
ielrec);

Is it the right way to pass the smart pointers.

Could you plz guide me.

I am getting the erros like

"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 68.11:
1540-1101 (W) A return value of type "BDI_Struct *" is expected.
"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 63.12:
1540-0700 (I) The previous message was produced while processing
"ObjVar<BDI_Struct>::eek:perator->()".
"IndOutModcaParser.cpp", line 1814.5: 1540-0700 (I) The previous message
was produced while processing
"CIndOutModcaParser::vValidateIndModcaFile(ObjVar<BDI_Struct> &,
ObjVar<record> &)".
"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 68.11:
1540-1101 (W) A return value of type "record *" is expected.
"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 63.12:
1540-0700 (I) The previous message was produced while processing
"ObjVar<record>::eek:perator->()".
"IndOutModcaParser.cpp", line 1814.5: 1540-0700 (I) The previous message
was produced while processing
"CIndOutModcaParser::vValidateIndModcaFile(ObjVar<BDI_Struct> &,
ObjVar<record> &)"

Thanks,
Lokb

Without seeing the defintion of ObjVar it seems impossible to answer your
question about how to use it. Smart pointers are classes just like any
other, how you use them depends on how they are written.

What you have written would certainly work with some smart pointer classes
I have seen, but why it isn't OK with your smart pointer can only be
guessed at without seeing the code of your smart pointer class.

john
 
P

Phlip

lokb said:
I have a structure which and defined a smart pointer to the structure.

/* Structure of Begin Document Index Record */
typedef struct BDI_Struct{
unsigned char rname;
unsigned short int rlen;
int code;
short int reserved;
char indexName[9];
unsigned char tripLen;
unsigned char tripCode;
TRIPLET01 t01;
}BDIStruct;

Why are you writing C style C++ code? Just say struct BDI_Struct.
/* Structure of Index Element Record */
typedef struct record{
unsigned char rname;
unsigned short int rlen;
int code;
short int reserved;
unsigned char tripLen;
unsigned char tripCode;
union
{
TRIPLET01 t01;
TRIPLET57 t57;
TRIPLET2D t2D;
TRIPLET36 t36;
TRIPLET02 t02;
TRIPLET80 t80;
TRIPLETGEN tgen;
}TRec;
}IndexStruct;

Why are you using a union? Just list the members you need, and don't use all
of them at the same time.

The answers to both questions might well be "I'm matching a binary format
that someone else wrote". If so, ignore my complaints.
Defined the smart pointer as below:
where ObjVar returns the pointer.
typedef ObjVar<IELStruct> IELStruct_sptr;
typedef ObjVar<BDIStruct> BDIStruct_sptr;

Now when i am passing the smart pointers to the function i get compilation
errors
I am passing the smart pointers as

int vValidateIndModcaFile(ObjVar<BDIStruct>& bdirec,ObjVar<IndexStruct>&
ielrec);

Why does vValidateIndModcalFile() have such a tortuous name?

Also, why does vValidateIndModcalFile() care how its BDIStruct and
IndexStruct are stored? Will it ever delete or re-seat the smart pointer's
object?

Just pass in the raw objects:

int vValidateIndModcaFile(BDIStruct & bdirec, IndexStruct & ielrec);
I am getting the erros like

"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 68.11:
1540-1101 (W) A return value of type "BDI_Struct *" is expected.
"/vpapp/accenture/ISIXRelease1206302004/include/sptr.hpp", line 63.12:
1540-0700 (I) The previous message was produced while processing
"ObjVar<BDI_Struct>::eek:perator->()".

My suggestion will probably fix all that.

But you get operator->() errors from a smart pointer by writing something
like sp->method(). Your controlled objects have no methods, so if you still
have a problem post the code that actually appears inside
vValidateIndModcaFile().
 
L

lokb

Hi,
This is what ObjVar is doing
#ifndef SPTR_HPP
#define SPTR_HPP

#include <string>

using namespace std;

template <class T> class Counted;

template <class T>
class ObjVar
{
public:
ObjVar()
{
m_pCounted = 0;
}


ObjVar(T* pT)
{
m_pCounted = new Counted<T>(pT);
m_pCounted->GetRef();
}


~ObjVar()
{
UnBind();
}


ObjVar(const ObjVar<T>& rVar)
{

m_pCounted = rVar.m_pCounted;
if (!Null())
m_pCounted->GetRef();
}


ObjVar<T>& operator=(const ObjVar<T>& rVar)
{

if (!rVar.Null())
rVar.m_pCounted->GetRef();
UnBind();
m_pCounted = rVar.m_pCounted;
return *this;
}


ObjVar<T>& operator=(Counted<T>* pCounted)
{
if (!rVar.Null())
rVar.m_pCounted->GetRef();
UnBind();
m_pCounted = pCounted;
return *this;
}


T* operator->()
{
if (!Null())
return m_pCounted->my_pT;

}


T& operator*()
{
if (!Null())
return *m_pCounted->my_pT;
}


const T* operator->() const
{
if (Null())
throw return;
return m_pCounted->my_pT;

}


bool Null() const
{
return m_pCounted == 0;
}


void SetNull()
{
UnBind();
}


private:
void UnBind()
{
if (!Null() && m_pCounted->FreeRef() == 0)
delete m_pCounted;
m_pCounted = 0;
}

Counted<T>* m_pCounted;

};
template <class T> class Counted
{
friend class ObjVar<T>;
private:
Counted(T* pT) : Count(0), my_pT(pT)
{

}


~Counted()
{
if (Count == 0)
delete my_pT;
}


unsigned GetRef()
{
return ++Count;
}


unsigned FreeRef()
{
if (Count!=0)
return --Count;
}

T* const my_pT;
unsigned Count;
};

The IELStruct name was misleading sorry abt that.. its actually
IndexStructure and is defined as

typedef ObjVar<IndexStruct> IndexStruct_sptr;

let me know if u require more information
Thanks,
Lokesh
 
J

John Harrison

let me know if u require more information
Thanks,
Lokesh

I took your code, cut out what I didn't have definitions for, and it
compiled for me. I can't see any reason in the code you've posted for it
not to compile.

Always with questions like this, the way to get them answered quickly, is
to post a complete programs, with definitions for everything.

Now no-one wants to read a huge chunk of code, so spend some time cutting
out the parts of your program that aren't causing any problems. When
you've done that and you have a smallish program left that still gives the
error you don't understand, post the complete program here.

john
 
G

Greg Schmidt

I took your code, cut out what I didn't have definitions for, and it
compiled for me. I can't see any reason in the code you've posted for it
not to compile.

Always with questions like this, the way to get them answered quickly, is
to post a complete programs, with definitions for everything.

Now no-one wants to read a huge chunk of code, so spend some time cutting
out the parts of your program that aren't causing any problems. When
you've done that and you have a smallish program left that still gives the
error you don't understand, post the complete program here.

It's been my experience that the process of reducing it to a small example
that shows the error almost always points me to the solution. The small
version will probably work, and you then add stuff back in one bit at a
time until it breaks again. (Okay, being as I know about binary searches,
I usually add stuff back in a few bits at a time, in such a way that when
it breaks I can isolate each bit and find which was to blame.)
 
J

John Harrison

It's been my experience that the process of reducing it to a small
example
that shows the error almost always points me to the solution. The small
version will probably work, and you then add stuff back in one bit at a
time until it breaks again. (Okay, being as I know about binary
searches,
I usually add stuff back in a few bits at a time, in such a way that when
it breaks I can isolate each bit and find which was to blame.)

I agree, but in this case I was too hasty.

lokb, look at the error messages again. They are only warnings. Now look
at the code for operator-> (for instance)


T* operator->()
{
if (!Null())
return m_pCounted->my_pT;

}

Notice that if !Null() is true this returns 'm_pCounted->my_pT' but if it
is not true nothing is returned at all! That is the source of the error
messages.

The same mistake is made in several other places. Also the const version
of operator-> has a syntax error


const T* operator->() const
{
if (Null())
throw return;
return m_pCounted->my_pT;

}

'throw return' is not legal C++.

Did you write this smart pointer? If so then you need to decide what to do
in the case of a Null pointer. I would suggest something like this

T* operator->()
{
if (!Null())
return m_pCounted->my_pT;
else
throw "null pointer";
}

and similar for all the other cases. If you didn't write this then you
need to go to whoever you got this code from and ask them what to do.

john
 
L

lokb

Thanks John and others
It worked .. just to enhance my understanding..
can u give me some pointers where smart pointers are best implemented.
Infact, I learn form code snap shots quickly then reading a lot of
theory.
Thanks once again for the timely help
Thanks,
lokb
 
J

John Harrison

Thanks John and others
It worked .. just to enhance my understanding..
can u give me some pointers where smart pointers are best implemented.
Infact, I learn form code snap shots quickly then reading a lot of
theory.
Thanks once again for the timely help
Thanks,
lokb

The best smart pointer implementations I've seen are boost and loki. Both
are quite advanced, in particular I'd think you'd need to buy the book
Modern C++ Design by Alexei Alexandrescu to understand the loki one (its
well worth reading however, the most original book on C++ I've ever read).

boost is here http://www.boost.org/libs/smart_ptr/smart_ptr.htm

loki is here http://sourceforge.net/projects/loki-lib/

john
 
L

lokb

Thanks John and others
It worked .. just to enhance my understanding..
can u give me some pointers where smart pointers are best implemented.
Infact, I learn form code snap shots quickly then reading a lot of
theory.
Thanks once again for the timely help
Thanks,
lokb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top