首页 新闻 会员 周边 捐助

紧急求助:C++ 类的友元函数能够引用类的公有成员函数吗?如何声明一个指向类的成员函数的指针?

0
悬赏园豆:5 [已解决问题] 解决于 2010-09-02 10:47

目前做如下修改:

 

Preprocess.h文件中:

Preprocess类内:(只写明问题相关部分)
typedef vector<string>(Preprocess::*FUNCSEG)(string,set<string>);

int ConstructMap(map<string,vector<pair<int,int>>>&mymap,char *dbfield,FUNCSEG seg);

vector<string>goodWordsinPieceArticle(string rawtext,set<string> stopwords);

vector<string> mySplit(string s,set<string> stopwords);

void WriteTotalArff(int DFthreshlod,bool isbagOfwordsexsist);

Process.cpp中、

int Preprocess::ConstructMap(map<string,vector<pair<int,int>>>&mymap,char *dbfiled,Preprocess::FUNCSEG seg)
{
 //set<string>MakeStopSet();
 CoInitialize(NULL);
 _ConnectionPtr pConn(__uuidof(Connection));
 _RecordsetPtr pRst(__uuidof(Recordset));
 pConn->ConnectionString=dbconnection;
 pConn->Open("","","",adConnectUnspecified);
 pRst=pConn->Execute(dbselect,NULL,adCmdText);
 set<string>stopwords=MakeStopSet();
 while(!pRst->rsEOF)
 { vector<string>wordcollection;
    //string keywordstr=(_bstr_t)pRst->GetCollect("CKeyWord");
  string rawtext=(_bstr_t)pRst->GetCollect(dbfield);
  if(rawtext!="")
  {
   wordcollection=seg(rawtext,stopwords);
   string tempid=(_bstr_t)pRst->GetCollect("ArticleId");
   int articleid=atoi(tempid.c_str());
   for(vector<string>::iterator strit=wordcollection.begin();strit!=wordcollection.end();strit++)
   {
    vector<pair<int,int>>::iterator it;
    if(mymap[*strit].empty())
    {
     pair<int,int>mytemppair=make_pair(articleid,1);
     mymap[*strit].push_back(mytemppair);

    }
    else
    {
     for(it=mymap[*strit].begin();it!=mymap[*strit].end();it++)
     { 
      if(it->first==articleid)
      {
       it->second=++(it->second);
       break;
      }

    }
    if(it==mymap[*strit].end())
    {
     pair<int,int>mytemppair=make_pair(articleid,1);
     mymap[*strit].push_back(mytemppair);
    }

   }

  }


 }


 pRst->MoveNext();
 wordcollection.clear();
 }
 pRst->Close();
 pConn->Close();
 pRst.Release();
 pConn.Release();
 CoUninitialize();
 return 0;

}

最后报错报在了:


在调用调用ConstructMap的函数里

错如下Error 1 error C2064: term does not evaluate to a function taking 2 arguments e:\mycoding\wekapreprocess\wekapreprocess\preprocess.cpp 58 WekaPreprocess
错误指向ConstructMap中对应语句
wordcollection=seg(rawtext,stopwords)

调用ConstructMap的函数定义如下
void Preprocess::WriteTotalArff(int DFthreshold,bool isbagOfWordsExist)
{
char * dbfield="CAbstract";
FUNCSEG seg=&Preprocess::goodWordsinPieceArticle;
map<string,vector<pair<int,int>>> mymap;
if(!isbagOfWordsExist)
{
ConstructMap(mymap,dbfield,seg);
save(mymap);
cout<<"词袋子信息已经保存到硬盘"<<endl;
}
else
{
load(mymap);
}
DFcharicteristicWordSelection(mymap,DFthreshold);
WriteHeadArff();
VSMFormation(mymap);
cout<<"arff文件已经形成"<<endl;


string temp(infoFromWekaAddress);

cout<<"请您将使用weka聚类,并保存为"<<temp<<endl;
}

问题补充: 目前做了如下修改: 在Preprocess类内: typedef vector<string>(Preprocess::*FUNCSEG)(string,set<string>); 在调用调用ConstructMap的函数里 void Preprocess::WriteTotalArff(int DFthreshold,bool isbagOfWordsExist) { char * dbfield="CAbstract"; FUNCSEG seg=&Preprocess::goodWordsinPieceArticle; map<string,vector<pair<int,int>>> mymap; if(!isbagOfWordsExist) { ConstructMap(mymap,dbfield,seg); save(mymap); cout<<"词袋子信息已经保存到硬盘"<<endl; } else { load(mymap); } DFcharicteristicWordSelection(mymap,DFthreshold); WriteHeadArff(); VSMFormation(mymap); cout<<"arff文件已经形成"<<endl; string temp(infoFromWekaAddress); cout<<"请您将使用weka聚类,并保存为"<<temp<<endl; } 报错如下Error 1 error C2064: term does not evaluate to a function taking 2 arguments e:\mycoding\wekapreprocess\wekapreprocess\preprocess.cpp 58 WekaPreprocess 错误指向ConstructMap中对应语句 wordcollection=seg(rawtext,stopwords);
finallyly的主页 finallyly | 初学一级 | 园豆:100
提问于:2010-09-02 09:15
< >
分享
最佳答案
0

指向类成员函数的指针是个很蛋疼的东西。

class A{
public:
bool kk(int i){return i>1;}
};
typedef
bool (A::* XXX)(int);
int main(){
XXX p
= NULL;
p
= &A::kk; // pointer to member function kk in class A;
A *pa = new A;
(pa
->*p)(10); // to call a member function from pointer, must specific a pointer to class first
}

 

收获园豆:4
嗷嗷 | 小虾三级 |园豆:757 | 2010-09-02 10:33
根据嗷嗷的方法,我采用如下方法解决问题 给Preprocess类添加个默认构造函数 Preprocess(){} 然后 我在ConstructMap做了如下修改 Preprocess *p=new Preprocess(); wordcollection=(p->*seg)(rawtext,stopwords); delete p 另外大侠Galatica的答案为最佳答案,我刚才没有看到,十分抱歉,下次把分数补个给
finallyly | 园豆:100 (初学一级) | 2010-09-02 11:07
其他回答(1)
0

不能,静态函数可以.你申明友元函数时应该把 Preprocess* 作为参数传入.

 

 

class Preprocess
{
public:
Preprocess(
void);
~Preprocess(void);

public:
typedef vector
<string>(Preprocess::*FUNCSEG)(string,set<string>);
vector
<string> goodWordsinPieceArticle(string rawtext,set<string> stopwords);

int ConstructMap(map<string,vector<pair<int,int>>>&mymap,char *dbfield,FUNCSEG seg)
{
string rawtext;
set<string>stopwords;
vector
<string>wordcollection = (this->*seg)(rawtext,stopwords);
}

void WriteTotalArff(int DFthreshlod,bool isbagOfwordsexsist)
{
char * dbfield="CAbstract";

FUNCSEG seg
=&Preprocess::goodWordsinPieceArticle;
map
<string,vector<pair<int,int>>> mymap;

ConstructMap(mymap,dbfield,seg);
}
};

 

把你的代码简化了下,如上所示.

收获园豆:1
Launcher | 园豆:45050 (高人七级) | 2010-09-02 09:58
那我不声明成友元,声明称类的成员函数,通过指向类的成员函数的指针怎么搞呢?
支持(0) 反对(0) finallyly | 园豆:100 (初学一级) | 2010-09-02 10:16
@finallyliuyu:修改了下你的调用方式.
支持(0) 反对(0) Launcher | 园豆:45050 (高人七级) | 2010-09-02 10:40
非常感谢你,刚才更新没有及时看到你如此细致的修改,你这个应该是最佳答案才对。不好意思。下次把分数给你补上:)
支持(0) 反对(0) finallyly | 园豆:100 (初学一级) | 2010-09-02 10:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册