gpt4 book ai didi

c++ - 编译错误: no class template, 初始化器太多,没有匹配的函数

转载 作者:行者123 更新时间:2023-11-28 08:16:40 25 4
gpt4 key购买 nike

我无法使用 boost::signals 消除编译错误。任何想法将不胜感激。

由于我一直在移植 2 年前编写的程序以适应当前环境,所以我对 boost::signals 还是个新手。下面的代码是我修改了原始程序以简化问题目的。

我想要直接解决我的问题。但除此之外,因为有很多关于 boost::signals 的问题(但我已经放弃弄清楚哪个是正确/最接近我的情况),我会投票给答案中建议的那些为这个问题设置更合适的标题,以使其成为一个更好的归档问题。

boostProve_Doc.h

#ifndef FC_H
#define FC_H

#include <vector>
#include "iostream"
#include "boost/signal.hpp"
typedef boost::signal<void()> PostUpdateSignal;
typedef PostUpdateSignal::slot_function_type PSlot;

class Doc {
public:
Doc(uint width, uint height) {
std::cout << "In constructor. width= " << width << ", height= "
<< height << std::endl;
}
~Doc() {
//Do ...
}

void connectPostUpdate(PSlot s) {
sig.connect(s);
}

protected:
PostUpdateSignal sig;
};
#endif

boostProve_View.cpp:

#include <string>
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include <boost/bind.hpp>
#include <boostProve_Doc.h>

using namespace std;
class View {
public:
View() {
setupCrowd();
}
~View() {
//do something...
}
private:
Doc *crowd_;
void setupCrowd() {
crowd_->connectPostUpdate(&View::correctR);
}
void correctR() {
// do something..
}
};

int main(int argc, char** argv) {
View c();
return 0;
}

错误:

$ g++ boostProve_View.cpp -I . /usr/lib/libboost_signals.so
In file included from /usr/include/boost/function/detail/maybe_include.hpp:13,
from /usr/include/boost/function/detail/function_iterate.hpp:14,
from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:47,
from /usr/include/boost/function.hpp:64,
from /usr/include/boost/thread/future.hpp:20,
from /usr/include/boost/thread.hpp:24,
from boostProve_View.cpp:3:
/usr/include/boost/function/function_template.hpp: In member function ‘void boost::function0<R>::assign_to(Functor) [with Functor = void (View::*)(), R = void]’:
/usr/include/boost/function/function_template.hpp:722: instantiated from ‘boost::function0<R>::function0(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = void (View::*)(), R = void]’
/usr/include/boost/function/function_template.hpp:1064: instantiated from ‘boost::function<R()>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = void (View::*)(), R = void]’
boostProve_View.cpp:20: instantiated from here
/usr/include/boost/function/function_template.hpp:903: error: no class template named ‘apply’ in ‘struct boost::detail::function::get_invoker0<boost::detail::function::member_ptr_tag>’
/usr/include/boost/function/function_template.hpp:913: error: too many initializers for ‘boost::detail::function::basic_vtable0<void>’
/usr/include/boost/function/function_template.hpp: In member function ‘bool boost::detail::function::basic_vtable0<R>::assign_to(F, boost::detail::function::function_buffer&) [with F = void (View::*)(), R = void]’:
/usr/include/boost/function/function_template.hpp:915: instantiated from ‘void boost::function0<R>::assign_to(Functor) [with Functor = void (View::*)(), R = void]’
/usr/include/boost/function/function_template.hpp:722: instantiated from ‘boost::function0<R>::function0(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = void (View::*)(), R = void]’
/usr/include/boost/function/function_template.hpp:1064: instantiated from ‘boost::function<R()>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = void (View::*)(), R = void]’
boostProve_View.cpp:20: instantiated from here
/usr/include/boost/function/function_template.hpp:492: error: no matching function for call to ‘boost::detail::function::basic_vtable0<void>::assign_to(void (View::*&)(), boost::detail::function::function_buffer&, boost::detail::function::basic_vtable0<R>::assign_to(F, boost::detail::function::function_buffer&) [with F = void (View::*)(), R = void]::tag)’

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


9/30/11 更新) 感谢建议,我通过这样做解决了问题:

crowd_->connectPostUpdate(boost::bind(&View::correctR, this));

最佳答案

问题是你的插槽需要一个 void (*)() 类型的函数,而你给它一个 void (View::*)() 类型的参数。不幸的是,成员函数指针与函数指针不同。解决此问题的一种方法是将 correctR 设置为静态,这将为其提供正确的签名。如果失败,您可能需要调查诸如 Boost 的 Bind 库之类的东西。

参见 this part of the C++ FAQ Lite有关指向成员函数的指针的更多信息。

关于c++ - 编译错误: no class template, 初始化器太多,没有匹配的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7523665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com