Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > Вложенный класс + STD + оперратор ==


Автор: SeregaLBN 24.7.2006, 11:03
Покажу на примере (пользуюсь VS6).

Есть простой код - класс 'Запись' и класс векторов этих записей:
Код

#include <windows.h>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

struct CRecord {
   string     m_strName;
   // ...
};

bool operator==(const CRecord& r1, const CRecord& r2) {
   return (r1.m_strName == r2.m_strName);
}

class CWrapper {
private:
   vector<CRecord> m_Records;

public:
   void Find(int index) const;
};

void CWrapper::Find(int index) const {
   //...
   std::find(m_Records.begin(), m_Records.end(), m_Records[index]);
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
   return 0;
}

Всё отлично компилируется.

А вот при попытке вложить класс CRecord внутрь CWrapper (т.к. у меня CRecord есть сущность только CWrapper'а) и происходят ошибки.
Подскажите что и как тут переделать... Насколько я понимаю, траблы в операторе ==, но вот как исправить...
Переделанный код:
Код

#include <windows.h>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class CWrapper {
private:
   struct CRecord {
      string     m_strName;
      // ...
   };


   static bool operator==(const CWrapper::CRecord& r1, const CWrapper::CRecord& r2) {
      return (r1.m_strName == r2.m_strName);
   }

   vector<CRecord> m_Records;

public:
   void Find(int index) const;
};

void CWrapper::Find(int index) const {
   std::find(m_Records.begin(), m_Records.end(), m_Records[index]);
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
   return 0;
}


А ошибки следующие:
error C2784: 'bool __cdecl std::operator ==(const class std::istream_iterator<_U,_E,_Tr> &,const class std::istream_iterator<_U,_E,_Tr> &)' : could not deduce template argument for 'const class std::istream_iterator<_U,_E,_Tr> &' from 'const struct CWrapper::CRecord'
        see reference to function template instantiation 'const struct CWrapper::CRecord *__cdecl std::find(const struct CWrapper::CRecord *,const struct CWrapper::CRecord *,const struct CWrapper::CRecord &)' being compiled

error C2784: 'bool __cdecl std::operator ==(const class std::reverse_bidirectional_iterator<_BI,_Ty,_Rt,_Pt,_D> &,const class std::reverse_bidirectional_iterator<_BI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template argument for 'const class std::reverse_bidirectional_iterator<_BI,_Ty,_Rt,_Pt,_D> &' from 'const struct CWrapper::CRecord'
        see reference to function template instantiation 'const struct CWrapper::CRecord *__cdecl std::find(const struct CWrapper::CRecord *,const struct CWrapper::CRecord *,const struct CWrapper::CRecord &)' being compiled

error C2784: 'bool __cdecl std::operator ==(const class std::vector<_Ty,_A> &,const class std::vector<_Ty,_A> &)' : could not deduce template argument for 'const class std::vector<_Ty,_A> &' from 'const struct CWrapper::CRecord'
        see reference to function template instantiation 'const struct CWrapper::CRecord *__cdecl std::find(const struct CWrapper::CRecord *,const struct CWrapper::CRecord *,const struct CWrapper::CRecord &)' being compiled
 

Автор: SeregaLBN 24.7.2006, 12:41
Разобрался. Тема закрыта
Код

#include <windows.h>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class CWrapper {
private:
   struct CRecord {
      string     m_strName;
      // ...
      bool operator==(const CRecord& r2) const {
         return (m_strName == r2.m_strName);
      }
   };

   //friend bool operator==(const CWrapper::CRecord& r1, const CWrapper::CRecord& r2) {
   //   return (r1.m_strName == r2.m_strName);
   //}
   vector<CRecord> m_Records;

public:
   void Find(int index) const;
};

void CWrapper::Find(int index) const {
   find(m_Records.begin(), m_Records.end(), m_Records[index]);
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
   return 0;
}
 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)