错误:函数返回一个函数

发布于 2024-12-05 14:30:12 字数 4086 浏览 2 评论 0原文

虽然至少有一个类似的问题,但我仍然问我的问题,因为那个问题还没有得到解决了并且看起来更复杂。我正在尝试简化我的。

我有一个使用 .h 的 .cpp 文件,如下所示,编译这些棚屋错误如下。任何想法表示赞赏。请注意,代码已被简化,以便最大限度地仅显示有问题的部分。

FC_boost_prove.h:

#ifndef FC_H
#define FC_H

#include <vector>
#include "iostream"

#include "boost/signal.hpp"
#include "boost/bind.hpp"
#include <boost/random.hpp>

typedef boost::signal0<void()> PreUpdateSignal;
typedef PreUpdateSignal::slot_function_type PreUpdateSlot;
typedef boost::signal0<void()> PostUpdateSignal;
typedef PostUpdateSignal::slot_function_type PostUpdateSlot;

class FC {
public:
    FC(uint width, uint height) {
        std::cout << "In constructor." << std::endl;
    }
    ~FC() {
        //Do ...
    }
    void connectPreUpdate(PreUpdateSlot s) {
        preUpdateSignal_.connect(s);
    }
    void connectPostUpdate(PostUpdateSlot s) {
        postUpdateSignal_.connect(s);
    }

protected:
    PreUpdateSignal preUpdateSignal_;
    PostUpdateSignal postUpdateSignal_;
};
#endif

FC_boost_prove.cpp:

#include <iostream>
#include <string>
#include "FC_boost_prove.h"

int main() {
    std::cout << "test." << std::endl;
}

编译错误:

$ g++ FC_boost_prove.cpp 
In file included from /usr/include/boost/signals/signal_template.hpp:22,
                 from /usr/include/boost/signals/signal0.hpp:24,
                 from /usr/include/boost/signal.hpp:19,
                 from FC_boost_prove.h:7,
                 from FC_boost_prove.cpp:3:
/usr/include/boost/last_value.hpp: In instantiation of ‘boost::last_value<void()>’:
/usr/include/boost/signals/signal_template.hpp:178:   instantiated from ‘boost::signal0<void(), boost::last_value<void()>, int, std::less<int>, boost::function0<void()> >’
FC_boost_prove.h:12:   instantiated from here
/usr/include/boost/last_value.hpp:22: error: function returning a function
In file included from /usr/include/boost/signals/signal0.hpp:24,
                 from /usr/include/boost/signal.hpp:19,
                 from FC_boost_prove.h:7,
                 from FC_boost_prove.cpp:3:
/usr/include/boost/signals/signal_template.hpp: In instantiation of ‘boost::signal0<void(), boost::last_value<void()>, int, std::less<int>, boost::function0<void()> >’:
FC_boost_prove.h:12:   instantiated from here
/usr/include/boost/signals/signal_template.hpp:330: error: function returning a function
/usr/include/boost/signals/signal_template.hpp:370: error: function returning a function
In file included from /usr/include/boost/function/detail/maybe_include.hpp:13,
                 from /usr/include/boost/function/function0.hpp:11,
                 from /usr/include/boost/signals/signal_template.hpp:38,
                 from /usr/include/boost/signals/signal0.hpp:24,
                 from /usr/include/boost/signal.hpp:19,
                 from FC_boost_prove.h:7,
                 from FC_boost_prove.cpp:3:
/usr/include/boost/function/function_template.hpp: In instantiation of ‘boost::function0<void()>’:
FC_boost_prove.h:24:   instantiated from here
/usr/include/boost/function/function_template.hpp:1006: error: function returning a function
/usr/include/boost/function/function_template.hpp: In instantiation of ‘boost::detail::function::basic_vtable0<void()>’:
/usr/include/boost/function/function_template.hpp:856:   instantiated from ‘void boost::function0<R>::clear() [with R = void()]’
/usr/include/boost/function/function_template.hpp:752:   instantiated from ‘boost::function0<R>::~function0() [with R = void()]’
/usr/include/boost/signals/slot.hpp:105:   instantiated from here
/usr/include/boost/function/function_template.hpp:486: error: function returning a function
/usr/include/boost/function/function_template.hpp:643: error: function returning a function

环境:Ubuntu 10.10,g++(Ubuntu/Linaro 4.4.4-14ubuntu5)4.4.5

Although there is at least one similar question, I still ask mine since that one hasn't got solved and seems more complicated. I'm trying to simplify mine.

I have a .cpp file that uses .h as below, and compiling these sheds error as follows. Any idea is appreciated. Note that codes are simplified in order to minimally show the problematic parts only.

FC_boost_prove.h:

#ifndef FC_H
#define FC_H

#include <vector>
#include "iostream"

#include "boost/signal.hpp"
#include "boost/bind.hpp"
#include <boost/random.hpp>

typedef boost::signal0<void()> PreUpdateSignal;
typedef PreUpdateSignal::slot_function_type PreUpdateSlot;
typedef boost::signal0<void()> PostUpdateSignal;
typedef PostUpdateSignal::slot_function_type PostUpdateSlot;

class FC {
public:
    FC(uint width, uint height) {
        std::cout << "In constructor." << std::endl;
    }
    ~FC() {
        //Do ...
    }
    void connectPreUpdate(PreUpdateSlot s) {
        preUpdateSignal_.connect(s);
    }
    void connectPostUpdate(PostUpdateSlot s) {
        postUpdateSignal_.connect(s);
    }

protected:
    PreUpdateSignal preUpdateSignal_;
    PostUpdateSignal postUpdateSignal_;
};
#endif

FC_boost_prove.cpp:

#include <iostream>
#include <string>
#include "FC_boost_prove.h"

int main() {
    std::cout << "test." << std::endl;
}

Compile error:

$ g++ FC_boost_prove.cpp 
In file included from /usr/include/boost/signals/signal_template.hpp:22,
                 from /usr/include/boost/signals/signal0.hpp:24,
                 from /usr/include/boost/signal.hpp:19,
                 from FC_boost_prove.h:7,
                 from FC_boost_prove.cpp:3:
/usr/include/boost/last_value.hpp: In instantiation of ‘boost::last_value<void()>’:
/usr/include/boost/signals/signal_template.hpp:178:   instantiated from ‘boost::signal0<void(), boost::last_value<void()>, int, std::less<int>, boost::function0<void()> >’
FC_boost_prove.h:12:   instantiated from here
/usr/include/boost/last_value.hpp:22: error: function returning a function
In file included from /usr/include/boost/signals/signal0.hpp:24,
                 from /usr/include/boost/signal.hpp:19,
                 from FC_boost_prove.h:7,
                 from FC_boost_prove.cpp:3:
/usr/include/boost/signals/signal_template.hpp: In instantiation of ‘boost::signal0<void(), boost::last_value<void()>, int, std::less<int>, boost::function0<void()> >’:
FC_boost_prove.h:12:   instantiated from here
/usr/include/boost/signals/signal_template.hpp:330: error: function returning a function
/usr/include/boost/signals/signal_template.hpp:370: error: function returning a function
In file included from /usr/include/boost/function/detail/maybe_include.hpp:13,
                 from /usr/include/boost/function/function0.hpp:11,
                 from /usr/include/boost/signals/signal_template.hpp:38,
                 from /usr/include/boost/signals/signal0.hpp:24,
                 from /usr/include/boost/signal.hpp:19,
                 from FC_boost_prove.h:7,
                 from FC_boost_prove.cpp:3:
/usr/include/boost/function/function_template.hpp: In instantiation of ‘boost::function0<void()>’:
FC_boost_prove.h:24:   instantiated from here
/usr/include/boost/function/function_template.hpp:1006: error: function returning a function
/usr/include/boost/function/function_template.hpp: In instantiation of ‘boost::detail::function::basic_vtable0<void()>’:
/usr/include/boost/function/function_template.hpp:856:   instantiated from ‘void boost::function0<R>::clear() [with R = void()]’
/usr/include/boost/function/function_template.hpp:752:   instantiated from ‘boost::function0<R>::~function0() [with R = void()]’
/usr/include/boost/signals/slot.hpp:105:   instantiated from here
/usr/include/boost/function/function_template.hpp:486: error: function returning a function
/usr/include/boost/function/function_template.hpp:643: error: function returning a function

Environment: Ubuntu 10.10, g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5

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

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

发布评论

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

评论(1

故事未完 2024-12-12 14:30:12

为什么要指定 boost::signal0?信号N模板适用于无法正确解析函数签名的有缺陷的编译器。

按照现代编译器的建议,使用信号并指定函数签名:

typedef boost::signal<void()> PreUpdateSignal;
typedef boost::signal<void()> PostUpdateSignal;

或者根据有缺陷的编译器的需要,使用信号N并显式指定返回类型(以及每个参数类型):

typedef boost::signal0<void> PreUpdateSignal;
typedef boost::signal0<void> PostUpdateSignal;

Why are you specifying boost::signal0<>? The signalN templates are for deficient compilers that can't properly parse function signatures.

Either use signal and specify the function signature, as recommended for modern compilers:

typedef boost::signal<void()> PreUpdateSignal;
typedef boost::signal<void()> PostUpdateSignal;

or use signalN and specify the return type (and every argument type) explicitly, as needed for deficient compilers:

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