1

In the 1st code snippet below, I am trying to remove an element from a vector within a member function based on a static condition function fed into std::remove function. Then I am getting lots of template errors shown in the 2nd snippet. Can you please tell me what I am missing?

SNIPPET 1 (CODE)

void removeVipAddress(std::string &uuid)
{
          struct RemoveCond
          {
            static bool condition(const VipAddressEntity & o)
            {
              return o.getUUID() == uuid;
            }
          };

          std::vector<VipAddressEntity>::iterator last =
            std::remove(
                    mVipAddressList.begin(),
                    mVipAddressList.end(),
                    RemoveCond::condition);

          mVipAddressList.erase(last, mVipAddressList.end());

}

SNIPPET 2 (COMPILATION OUTPUT)

 /usr/include/c++/4.7/bits/random.h:4845:5: note: template<class _IntType> bool      std::operator==(const std::discrete_distribution<_IntType>&, const   std::discrete_distribution<_IntType>&)
 /usr/include/c++/4.7/bits/random.h:4845:5: note:   template argument deduction/substitution failed:
 In file included from /usr/include/c++/4.7/algorithm:63:0,
             from Entity.hpp:12:
 /usr/include/c++/4.7/bits/stl_algo.h:174:4: note:   ‘ECLBCP::VipAddressEntity’ is not  derived from ‘const std::discrete_distribution<_IntType>’
 In file included from /usr/include/c++/4.7/random:50:0,
               from /usr/include/c++/4.7/bits/stl_algo.h:67,
               from /usr/include/c++/4.7/algorithm:63,
               from Entity.hpp:12:
 /usr/include/c++/4.7/bits/random.h:4613:5: note: template<class _RealType> bool  std::operator==(const std::extreme_value_distribution<_RealType>&, const  std::extreme_value_distribution<_RealType>&)
 /usr/include/c++/4.7/bits/random.h:4613:5: note:   template argument deduction/substitution failed:
 In file included from /usr/include/c++/4.7/algorithm:63:0,
             from Entity.hpp:12:
 /usr/include/c++/4.7/bits/stl_algo.h:174:4: note:   ‘ECLBCP::VipAddressEntity’ is not  derived from ‘const std::extreme_value_distribution<_RealType>’
F. Aydemir
  • 2,665
  • 5
  • 40
  • 60
  • That is only some note parts of an error message, the real error is missing. – PlasmaHH Feb 22 '13 at 13:15
  • As far as I know you can't use RemoveCond::condition as a template arguement here. Take a look at http://stackoverflow.com/a/7627218/767543 where "however this is not allowed, f cannot be passed to a template function in C++03." is stated. I might be wrong though – Hayri Uğur Koltuk Feb 22 '13 at 13:17
  • I got so many error messages that could not fit into buffer of the terminal... – F. Aydemir Feb 22 '13 at 13:18

1 Answers1

4

I guess you're looking for std::remove_if(), not std::remove().

std::remove_if() accepts a predicate as it third argument, and removes elements satisfying that predicate.

std::remove() takes a value as the third argument, and removes elements equal to the value.

EDIT

To make this work, you will also have to turn your RemoveCond definition into a predicate object, because it needs state. Like this:

void removeVipAddress(std::string &uuid)
{
      struct RemoveCond : public std::unary_function<VipAddressEntity, bool>
      {
        std::string uuid;

        RemoveCond(const std::string &uuid) : uuid(uuid) {}

        bool operator() (const VipAddressEntity & o)
        {
          return o.getUUID() == uuid;
        }
      };

      std::vector<VipAddressEntity>::iterator last =
        std::remove(
                mVipAddressList.begin(),
                mVipAddressList.end(),
                RemoveCond(uuid));

      mVipAddressList.erase(last, mVipAddressList.end());

}
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • Yep, remove takes no predicate. – jtepe Feb 22 '13 at 13:16
  • Ya, this helped thanks but then I got the following errors. Do you have any comment on these? Entity.hpp: In static member function ‘static bool ECLBCP::VipAddressSet::removeVipAddress(std::string&)::RemoveCond::condition(const ECLBCP::VipAddressEntity&)’: Entity.hpp:203:32: error: use of parameter from containing function Entity.hpp:197:7: error: ‘std::string& uuid’ declared here – F. Aydemir Feb 22 '13 at 13:17
  • @Fardaarda I've added it to the answer. – Angew is no longer proud of SO Feb 22 '13 at 13:29