Doubt regarding structure nesting

D

darkstorm

Consider this:

template<typename T_fp>class TRigidBody : public BaseObject
{
public:
 
D

David White

darkstorm said:
Consider this:

template<typename T_fp>class TRigidBody : public BaseObject
{
public:
.
.
//member functions
.
.
//Current state of the rigid body

typedef struct TRB_state
{
RB_state(){}
TRB_state(){}


//data members

}RB_state;

inline RB_state GetRBState(uint8 state_id) const;

private:
//data members of rigidbody


///<Current state of the rigid body
RB_state m_state[2];
};



template<typename T_fp>
(error)----->inline TRigidBody<T_fp>::RB_state
TRigidBody<T_fp>::GetRBState(uint8 state_id) const
{
return m_state[state_id];
}

When compiling I am getting this error:
error C2143: syntax error : missing ';' before
'TRigidBody<T_fp>::GetRBState'

Somebody please explain it...

After making a number of changes to eliminate other errors (i.e., struct
constructor name, BaseObject, uint8), this compiled with no error in VC++
6.0. However, VS .NET 2003 gave the error that you reported, after first
giving this warning:
warning C4346: 'TRigidBody<T_fp>::RB_state' : dependent name is not a type
prefix with 'typename' to indicate a type

I can't explain the error, but I found that if I made the code a
non-template it compiled. It also compiled if the function body was placed
inside the class definition.

BTW, you don't need a typedef for the struct. That's a C-ism. You can just
use the struct name.

DW
 
D

darkstorm

Thanks for the reply. I couldn't make it compile that way. Instead of
returning a refernce to RB_state, I tried passing it as a reference
parameter. Then it worked fine.

template<typename T_fp>
inline void TRigidBody<T_fp>::GetRBState(RB_state &state, uint8
state_id) const
{
state = m_state[state_id];
}

although, I have to copyback once the modification is complete....
 
S

simont

darkstorm said:
Thanks for the reply. I couldn't make it compile that way. Instead of
returning a refernce to RB_state, I tried passing it as a reference
parameter. Then it worked fine.

template<typename T_fp>
inline void TRigidBody<T_fp>::GetRBState(RB_state &state, uint8
state_id) const
{
state = m_state[state_id];
}

although, I have to copyback once the modification is complete....

Try

template<typename T_fp>
typename TRigidBody<T_fp>::RB_State
TRigidBody<T_fp>::GetRBState(...)
{
// ...
}

the "typename" is required to help out the parser tell the difference
between dependent types and members, before the template instantiation
is available to infer it.
 

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,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top