下面的类能被泛型算法支持么?
比如:list<Log> lLog;
Log.h内容如下:
#pragma once
// CLogclass CLog : public CWnd
{
DECLARE_DYNAMIC(CLog)public:
CLog();
CLog(int i,CString s);
virtual ~CLog();
bool operator==( CLog log );
bool operator<( CLog log );
protected:
DECLARE_MESSAGE_MAP()
private:
int iId;
CString sBody;
};
Log.cpp内容如下:
// Log.cpp : 实现文件
//#include "stdafx.h"
#include "smschat.h"
#include "Log.h"
#include <list>
// CLogIMPLEMENT_DYNAMIC(CLog, CWnd)
CLog::CLog()
{
iId = 0;
}CLog::CLog(int i,CString s)
{
this->iId = i;
this->sBody = s;
}CLog::~CLog()
{
}bool CLog::operator == ( CLog log )
{
if ( this->iId == log.iId && this->sBody == log.sBody )
{
return true;
}
else
{
return false;
}
}bool CLog::operator < ( CLog log )
{
if ( this->iId < log.iId )
{
return true;
}
else
{
return false;
}
}BEGIN_MESSAGE_MAP(CLog, CWnd)
END_MESSAGE_MAP()// CLog 消息处理程序

解决方案 »

  1.   

    protected:
    DECLARE_MESSAGE_MAP()->你的Message Mapping为何要声明成protected,我觉得一般都认是public的。
      

  2.   

    那些都是自动生成的,我只写了2个变量。还有
    CLog(int i,CString s);
    bool operator==( CLog log );
    bool operator<( CLog log );
    这三个函数。我要用list。我看c++primer上说要满足3条(有默认构造函数,支持==和<)才能用list
      

  3.   

    没问题,不过得写list<CLog> lLog;而不是<Log>
    list<x>中的x可以是任何类名
      

  4.   

    对了,那个#include <list>是不是应该放在smschatDlg.cpp里面啊,我要在这里调用那些算法。
    第一次设计类,第一次用list,可能问题比较可笑,各位大虾多包涵。能帮我看看那个运算符重载对不对么?
    是不是bool CLog::operator == (const CLog &log )这样定义比较好啊?
      

  5.   

    对了,那个#include <list>是不是应该放在smschatDlg.cpp里面啊,我要在这里调用那些算法。
      

  6.   

    对了,那个#include <list>是不是应该放在smschatDlg.cpp里面啊,我要在这里调用那些算法。->我觉得哪里第一次用到就放到哪里,当然如果自己“清楚”的话,放在统一的某个头文件中也可以。
      

  7.   

    泛型算法还需加入#include <algorithm>