Click to See Complete Forum and Search --> : Body already defined error in macro call in C++


amit_lal
September 27th, 2002, 08:15 PM
Hi ,
I have a file in which macros are defined as below:-

# define INSTANTIATE_CACHE_X(X_DECL, T) \
template class X_DECL cache_item<T>; \
template class X_DECL vector<cache_item<T> >; \
template class X_DECL Db_cache<T>; \
T* Db_cache<T>::operator[] (const size_t index) \
{ \
return (*slots)[index].record; \
} \
\
size_t Db_cache<T>::get_cache_size( void ) \
{ \
return slots->size(); \
} \
\
void Db_cache<T>::clear( void ) \
{ \
vector<cache_item<T> >::iterator slot_i; \
\
for ( slot_i = slots->begin(); slot_i != slots->end(); slot_i++ ) \
{ \
slot_i->query = string(); \
slot_i->row = 0; \
} \
} \
\
Db_cache<T> Db_table<T>::cache;

# define INSTANTIATE_CACHE(T) INSTANTIATE_CACHE_X(CLASS_DECL, T)

# define PLUGIN_INSTANTIATE_CACHE(T) INSTANTIATE_CACHE_X(, T)

Now, in another file,macro #2 is being used as -

INSTANTIATE_CACHE(Version)
template class CLASS_DECL Db_table;

I am getting following errors:-

[C++ Error] TIVER.CPP(60): E2171 Body has already been defined for
function 'Db_cache::operator [](unsigned int)'
[C++ Error] TIVER.CPP(60): E2171 Body has already been defined for
function 'Db_cache::get_cache_size()'
[C++ Error] TIVER.CPP(60): E2171 Body has already been defined for
function 'Db_cache::clear()'

Any solutions are welcome.

Thanks,
Amit

SolarFlare
September 27th, 2002, 09:23 PM
First of all, you should disable smilies when you type code ;) .

jfaust
September 27th, 2002, 09:41 PM
No--first of all is: you shouldn't be doing this. This will be a pain to maintain. It's ugly, confusing, error-prone, goes against every good coding standard I've ever read, and it's bad taste.

Second of all: disable smilies ;)

Third of all: Does the included file have a #ifdef/#define/#endif set that shoud be part of all include files to prevent multiple inclusion? That's my guess with what's wrong.

It is just a guess, because I simply don't have the patience or will-power to wade through this mess.

Jeff

Paul McKenzie
September 28th, 2002, 07:50 AM
I agree with jfaust. Why in the world would you use macros in this way? I hope you didn't get this idea from looking at MFC source code.

Assume that you do get it to compile -- can you imagine the nightmare it is if your macros have runtime bugs? Most debuggers cannot debug macro code, and you will have to unravel this mess to even start debugging.

Regards,

Paul McKenzie

amit_lal
September 28th, 2002, 01:34 PM
Hi All,
First of all , I am sorry for the simile problem. I am new to this group and so was not aware of the funda.So, sorry for it.

Second, I too understand that this kind of code is not easily maintainable but I have got the assignment to compile this code and so I have to.
Code is not written by me.

I am posting the problem again.

I have a file in which macros are defined as below:-

# define INSTANTIATE_CACHE_X(X_DECL, T)
template class X_DECL cache_item<T>;
template class X_DECL vector<cache_item<T> >;
template class X_DECL Db_cache<T>;
T* Db_cache<T>::operator[] (const size_t index)
{
return (*slots)[index].record;
}

size_t Db_cache<T>::get_cache_size( void )
{
return slots->size();
}

void Db_cache<T>::clear( void )
{
vector<cache_item<T> >::iterator slot_i;

for ( slot_i = slots->begin(); slot_i != slots->end(); slot_i++ )
{
slot_i->query = string();
slot_i->row = 0;
}
}
Db_cache<T> Db_table<T>::cache;

# define INSTANTIATE_CACHE(T) INSTANTIATE_CACHE_X(CLASS_DECL, T)

# define PLUGIN_INSTANTIATE_CACHE(T) INSTANTIATE_CACHE_X(, T)

Now, in another file,macro #2 is being used as -

INSTANTIATE_CACHE(Version)
template class CLASS_DECL Db_table;

I am getting following errors:-

[C++ Error] TIVER.CPP(60): E2171 Body has already been defined for
function 'Db_cache:Operator [](unsigned int)'
[C++ Error] TIVER.CPP(60): E2171 Body has already been defined for
function 'Db_cache::get_cache_size()'
[C++ Error] TIVER.CPP(60): E2171 Body has already been defined for
function 'Db_cache::clear()'

Any solutions are welcome.

Thanks again,
Amit

jfaust
September 28th, 2002, 02:20 PM
Amit,

Well, you could provide us with an example that compiles. If I can cut and paste it into one or more files to compile, I will take a look at it.

Jeff