首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于缺省构造函数解决思路

2012-02-16 
关于缺省构造函数#includestdafx.h #includeconst.h #includeCorpStruct.h #includemath.h #inclu

关于缺省构造函数
#include   "stdafx.h "
#include   "const.h "
#include   "CorpStruct.h "
#include   "math.h "
#include   "readfilev.h "
#include   "ByteGram.h "
#include   <fstream>
#include   <iostream>   //头文件部分,调用多个类定义
using   namespace   std;

extern   CCorpStructInterpolate   *g_pTrigram;   //调用CorpStruct.h头文件定义的类CCorpStructInterpolate
extern   ByteGram   g_bytegram;//调用ByteGram.h头文件定义的类ByteGram

class   Get_PP{
public:
        Get_PP(double   =0.0,double   =0.0,   double   =0.0,   int   =g_pTrigram-> getTag());//缺省构造函数,链接时报错
                错误原因:error   LNK2001:   unresolved   external   symbol   "public:   __thiscall   Get_PP::Get_PP(double,double,double,int)
        ~Get_PP();//析构函数

        double   get_bi_pp();
        double   get_tri_pp();//以上两个功能函数
private:
        double   b_pp;
        double   t_pp;
        double   total_log_prob;
        int   total_tag;
};

Get_PP::~Get_PP()
{
        cout < < "PP   object   destructor! " < <endl;
}

double   Get_PP::get_bi_pp()
{
        for(int   i=0;i <total_tag;i++)
        {
                for(int   j=0;j <total_tag;j++)
                        total_log_prob+=g_bytegram.GetBigram(i,j);
        }
          return   b_pp=exp(-(1/total_tag)*total_log_prob);

}

double   Get_PP::get_tri_pp()
{
        for(int   i=0;i <total_tag;i++)
        {
                for(int   j=0;j <total_tag;j++)
                        for(int   k=0;k <total_tag;k++)
                                total_log_prob+=g_bytegram.GetTrigram(i,j,k);
        }
        return   t_pp=exp(-(1/total_tag)*total_log_prob);
       
}
//////////////////////////////////////////以上为Get_PP头文件定义部分///////////////////
/////////////////////////////////////////以下为main函数调用部分///////////////////////
#include   "stdafx.h "
#include   <iostream>
#include   <fstream>
#include   <time.h>
#include   "const.h "
#include   "ToolFuncs.h "
#include   "interface.h "
#include   "readfilev.h "
//////////////////////包含所需要的头文件//////////
#include   "Get_PP.h "   //调用Get_PP头文件
using   namespace   std;
void   main()
{
        批处理函数,省略。
        //////////////////////////////////////////////////////////////////////////
                ofstream   out_pp( "PP_Result.csv ",ios::app);


        Get_PP   get_pp;   //调用缺省构造函数,链接是报错,
                错误原因:error   LNK2001:   unresolved   external   symbol   "public:   __thiscall   Get_PP::Get_PP(double,double,double,int)
       
                out_pp < < "Bigream   pp " < < ', ' < < ', ' < < "Trigram   pp " < <endl;
        out_pp < <get_pp.get_bi_pp() < < ', ' < < ', ' < <get_pp.get_tri_pp();
}

稍微有时间的大侠可以帮忙看下,不是很复杂的代码
但是就是链接是出错,错误地点我已经标出来了
十分感谢



[解决办法]
Get_PP get_pp;
这句默认是调用Get_PP::GetPP();
Get_PP(double =0.0,double =0.0, double =0.0, int =g_pTrigram-> getTag()){
....
};//这才叫定义

[解决办法]
很明显,
Get_PP::Get_PP(double d_b_pp, double d_t_pp, double d_total_log_prob, int g_pTrigram-> getTag() )
定义时,这里的g_pTrigram-> getTag()你想作默认值,不应让他做参数(形参要是个左操作数),另起个名字做参数就成,如

Get_PP::Get_PP(double d_b_pp, double d_t_pp, double d_total_log_prob, int tag ){
b_pp=d_b_pp;
t_pp=d_t_pp;
total_log_prob=d_total_log_prob;
total_tag=g_pTrigram-> getTag();
}
按照你的想法,这里的形参tag没用上,可能会有“警告”
如果total_tag只想从g_pTrigram-> getTag()得到,不妨将其默认值设为0,专门定义一个函数:
void set_total_tag(CCorpStructInterpolate *g_pTrigram){
total_tag=g_pTrigram-> getTag();//如果g_pTrigram定义成全局变量的话,可省去形参
//这里这样写是为了更灵活,也许日后要用此类(CCorpStructInterpolate)的其他对象
}
而构造函数写成:
Get_PP::Get_PP(double d_b_pp, double d_t_pp, double d_total_log_prob, int tag ){
b_pp=d_b_pp;
t_pp=d_t_pp;
total_log_prob=d_total_log_prob;
total_tag=tag;
}

另外,总觉得这个声明有问题:
Get_PP(double =0.0,double =0.0, double =0.0, int =g_pTrigram-> getTag());
这里的g_pTrigram-> getTag()是常量吗?
这里的默认值应该是常量

热点排行