在课堂上转发enum宣言?

时间:2018-01-14 21:27:48

标签: c++ c++11 enums forward-declaration

我有两个抽象类,彼此拥有另一个指针。我需要在其中一个中使用另一个类的枚举,比如显示例子。 AFoo持有ABar,而ABar需要一个指向AFoo的指针来更新一些数据,还需要一个会使用AFoo枚举的成员函数。

我记得曾经有过这个问题,但没有使用枚举,我最终做了内联声明。

我可以做一个嵌套类,但还有另一种方法可以避免吗?

AFoo.hpp:

#include ...

class AFoo;
enum AFoo::poo; --> not possible

#include "ABar.hpp"

class AFoo {
public:
  ...
 virtual void func() = O;
 enum poo {
  H,
  I,
  ...
 }
protected:
 ABar *bar_;
};

ABar.hpp

#include ...

class Abar;

#include "AFoo.hpp"

class ABar {
public:
 ...
 virtual AFoo::poo doing_some_stuff() = 0; --> Here is my problem (if i replace the return type with basic type i have no compilation problem)
protected:
 AFoo *foo_;
};

2 个答案:

答案 0 :(得分:0)

在AFoo.hpp中,删除

class ABar;

并替换

enum

JTextField

此外,您的JButton声明缺少分号。

答案 1 :(得分:0)

在AFoo.hpp中,不要包含ABar.hpp,只能向前声明该类:

$.getJSON('https://discovrbookings.innocraft.cloud/index.php?module=API&method=DevicesDetection.getBrowsers&idSite=2&period=day&date=2017-12-17,2018-01-05&format=json&token_auth=68aa5bd12137f13255dcb98794b65dff', (browser_data) => {
  Array.prototype.groupBy = function(key) {
    var path = key.split('.');
    var result = {};
    try {
      this.forEach(function(item) {
        // es6: path.reduce((a, b) => a[b], item)
        type = path.reduce(function(a, b) {
          return a[b]
        }, item) || 'null';
        if (!result[type])
          result[type] = [];
        result[type].push(item);
      });
      return result;
    } catch (err) {
      console.log(err);
      return {};
    }
  };

  function getPropertySum(key, arr) {
    return arr.reduce((a, b) => (key in b ? a + b[key] : a), 0)
  }
  // one array of all dates
  let browserCode = ('segment');
  let flattenArr = [].concat.apply([], Object.values(browser_data));
  // object of grouped dates by device
  let groups = flattenArr.groupBy('label');
  let table = document.getElementById('browserTable');
  table.innerHTML = '';
  Object.keys(groups).forEach(function(label) {
    let row = document.createElement('tr');
    let logw = document.createElement('td');
    let logo = document.createElement('img');
    let lab = document.createElement('td');
    let nbv = document.createElement('td');
    let nbu = document.createElement('td');
    row.appendChild(logw);
    row.appendChild(lab);
    row.appendChild(nbv);
    row.appendChild(nbu);
    logw.appendChild(logo);

    // Get the logo property and use split on the string
    logoProperty = getPropertySum('logo',groups[label]);
    var logoPropSplit = logoProperty.split("/");
    var logoName  = logoPropSplit [logoPropSplit.length-1];
    // End of edit

    logo.src = 'https://discovrbookings.com/wp-content/themes/discovr-application/assets/img/browser-icons/' + logoName + '.png';
    lab.innerHTML = label;
    nbv.innerHTML = getPropertySum('nb_visits', groups[label]);
    nbu.innerHTML = getPropertySum('nb_uniq_visitors', groups[label]);
    table.appendChild(row);
  });
});

此外,在ABar.hpp中,请包含AFoo.hpp,不要转发<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="browserTable"></table>$($('div.split > div.horizonChart .group rect')[0]).click()