boost::函数模板类

发布于 2024-12-22 05:59:11 字数 1283 浏览 2 评论 0原文

该程序旨在制作一个库,以便C++程序员可以轻松地模拟电路。如有任何反馈,我们将不胜感激。我所面临的错误让我困惑不已。

wire.h 文件

template<typename S>
class wire
{
  public:
    // default constructor
    wire() : _s() {}

    // constructor with one parameter of type S
    wire(S s) : _s(&s) {}

    // converter to signal
    operator S() const { return *_s; }

    // resets the signal of the wire and return the signal
    S operator()(S s) { *_s = s; return s; }

  private:
    // pointer to signal being carried
    S *_s;
};

// functor, returns the negation of the negation of the signal of the wire as a wire
template<typename W>
struct ww_not
{
  W operator()(W w1) const { return ~w1; };
};

main.cpp

#include <iostream>
#include <boost/function.hpp>
#include "wire.h"
typedef unsigned sig_type;
typedef wire<sig_type> wire_type;

main()
{
  sig_type s1(0x2), s2(0x3), s3(0x5), s4(0x7), s5(0xb), s6(0xc), s7(0x11);
  wire_type w1(s1), w2(s2), w3(s3), w4(s4), w5(s5), w6(s6), w7(s7);

  boost::function<wire_type (wire_type)> _not;
  //_not = &ww_not;
  // error missing template type
  _not = &ww_not<wire_type>;
  // error: expected primary-expression before ';' token
}

我已经尝试了几种不同的方法,它应该可以工作。

This program is designed to make a library to make it easy for C++ programmers to simulate circuits. Any feedback is appreciated. The error that I'm faced with puzzles me to no end.

wire.h file

template<typename S>
class wire
{
  public:
    // default constructor
    wire() : _s() {}

    // constructor with one parameter of type S
    wire(S s) : _s(&s) {}

    // converter to signal
    operator S() const { return *_s; }

    // resets the signal of the wire and return the signal
    S operator()(S s) { *_s = s; return s; }

  private:
    // pointer to signal being carried
    S *_s;
};

// functor, returns the negation of the negation of the signal of the wire as a wire
template<typename W>
struct ww_not
{
  W operator()(W w1) const { return ~w1; };
};

main.cpp

#include <iostream>
#include <boost/function.hpp>
#include "wire.h"
typedef unsigned sig_type;
typedef wire<sig_type> wire_type;

main()
{
  sig_type s1(0x2), s2(0x3), s3(0x5), s4(0x7), s5(0xb), s6(0xc), s7(0x11);
  wire_type w1(s1), w2(s2), w3(s3), w4(s4), w5(s5), w6(s6), w7(s7);

  boost::function<wire_type (wire_type)> _not;
  //_not = &ww_not;
  // error missing template type
  _not = &ww_not<wire_type>;
  // error: expected primary-expression before ';' token
}

I've tried it several different ways, and it should work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2024-12-29 05:59:11

ww_not 是一个需要实例化的仿函数类,而不是一个带有地址的函数:

_not = ww_not<wire_type>();

ww_not is a functor class which needs to be instantiated, not a function with an address:

_not = ww_not<wire_type>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文