CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2002
    Posts
    2

    Body already defined error in macro call in C++

    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>:perator[] (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:perator [](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

  2. #2
    Join Date
    Sep 2002
    Location
    Philadelphia ***Epoch: Timeless***
    Posts
    560

    Thumbs down

    First of all, you should disable smilies when you type code .
    SolarFlare

    Those who cling to life die and those who defy death live. -Sun Tzu

    cout << endl;
    return 0;
    }

  3. #3
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    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

  5. #5
    Join Date
    Sep 2002
    Posts
    2
    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

  6. #6
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured