前向声明无法解决循环依赖时该怎么办?

时间:2017-08-16 18:26:56

标签: c++ c++11

我有这些文件:

pi_utilities.h

    #ifndef UTILITIES_H_
#define UTILITIES_H_

#include <set>
#include <vector>

typedef unsigned short int MYTYPE2;

namespace pi
{
   class dfa;
}

using namespace std;

struct ir_statistical_measures
{
    // True positive, and so on.
    int tp = 0;
    int tn = 0;
    int fp = 0;
    int fn = 0;

    int dim_positive=0; 
    int dim_negative=0; 

    double accuracy  = 0; //(number in [0 1])
    double precision = 0;
    double recall = 0;
    double f_measure = 0;
    double matthews = 0;
    double specificity = 0; //this is recall- or true negative rate
    double bcr = 0;
};


void compute_ir_stats(const pi::dfa* dfa1 ,const pi::dfa* target,const set<vector<MYTYPE2>> &test_set,ir_statistical_measures &stats);
#endif

pi_utilities.cpp

 #include "pi_utilities.h"

void compute_ir_stats(const pi::dfa* dfa1 ,const pi::dfa* target,const set<vector<MYTYPE2>> &test_set,ir_statistical_measures &stats)
{
   bool result;
   result = dfa1->mq(true);
   result = target->mq(false);
}

pi_dfa.h

    #ifndef DFA_H_
#define DFA_H_

#include <string>
#include <vector>
#include <set>

#include "pi_utilities.h"

typedef unsigned short int MYTYPE;

using namespace std;

extern struct ir_statistical_measures &stats; //I say to compiler that definition isn't here

namespace pi
{
   class dfa
   {
        public:
        bool compare_dfa(const dfa *dfa_to_compare,string method,ir_statistical_measures &stats1,ir_statistical_measures &stats2) const;

        bool m_q(bool flag) const;

    };
}
#endif

pi_dfa.cpp

#include "pi_dfa.h"



bool pi::dfa::compare_dfa(const dfa *dfa_to_compare , string method , ir_statistical_measures &stats1 , ir_statistical_measures &stats2) const
{
   set<vector<MYTYPE>> test_set;
   compute_ir_stats(dfa_to_compare,this,test_set,stats1); 

   return true;
}

bool pi::dfa::m_q(bool flag) const
{
   return flag;
}

的main.cpp

#include "pi_dfa.h"
#include "pi_utilities.h"

int main()
{
   pi::dfa* L_dfa=NULL;  
   L_dfa = new pi::dfa();
   pi::dfa* O_dfa=new pi::dfa();

   bool value_return;
   struct ir_statistical_measures test_set_ir_statistical_measures; //struct defined in pi_utilities.h
   struct ir_statistical_measures test_set_ir_statistical_measures2;
   value_return = L_dfa->compare_dfa(O_dfa,"method",test_set_ir_statistical_measures,test_set_ir_statistical_measures2);
}

存在循环依赖问题。要解决我使用前向声明,但是使用类dfa的指针我也要调用类的函数,因此定义没有,编译器遇到麻烦, 我得到了这些编译错误:

    pi_utilities.cpp: In function ‘void compute_ir_stats(const pi::dfa*, const pi::dfa*, const std::set<std::vector<short unsigned int> >&, ir_statistical_measures&)’:
pi_utilities.cpp:6:17: error: invalid use of incomplete type ‘const class pi::dfa’
    result = dfa1->mq(true);
                 ^
In file included from pi_utilities.cpp:1:0:
pi_utilities.h:11:10: error: forward declaration of ‘const class pi::dfa’
    class dfa;
          ^
pi_utilities.cpp:7:19: error: invalid use of incomplete type ‘const class pi::dfa’
    result = target->mq(false);
                   ^
In file included from pi_utilities.cpp:1:0:
pi_utilities.h:11:10: error: forward declaration of ‘const class pi::dfa’
    class dfa;

因此,前瞻声明是不够的 我怎么能解决它?

我正在使用C ++ 11 (此处的代码重新排列为最小的可执行版本)

0 个答案:

没有答案