gpt4 book ai didi

c++ - 构造函数的返回类型定义 - 如何修复此 header

转载 作者:行者123 更新时间:2023-11-30 03:04:18 25 4
gpt4 key购买 nike

我有一个头文件,用于 GNU radio 信号处理 block ,并定义一个 C++ API。正如我在评论中所写,它使用“友元”定义来以安全的方式处理 Boost 共享指针,这样就不会出现意外的原始指针(访问私有(private)构造函数)。

我真的不能说为什么会失败。它似乎与构造函数中使用的标识符有关。类定义顺便说一句。以“;”结尾;)

我的构建错误消息是(我用 [point n] 标记了有问题的行。

cogra_binary_slicer.h:64:66: error: return type specification for constructor invalid [point 1] cogra_binary_slicer.cc: In function ‘cogra_binary_slicer_sptr cogra_binary_slicer()’: cogra_binary_slicer.cc:42:41: error: expected type-specifier [POINT 2] before ‘cogra_binary_slicer’ cogra_binary_slicer.cc:42:41: error: expected ‘)’ before ‘cogra_binary_slicer’ cogra_binary_slicer.cc:42:63: error: could not convert ‘gnuradio::get_initial_sptr(T*) with T = int’ from ‘boost::shared_ptr’ to ‘cogra_binary_slicer_sptr {aka boost::shared_ptr}’

然而,这将它缩小到三个文件。

header ,cogra_binary_slicer.h:

#ifndef INCLUDED_COGRA_BINARY_SLICER
#define INCLUDED_COGRA_BINARY_SLICER

#include <cogra_api.h>
#include <gr_block.h>

class cogra_binary_slicer;

typedef boost::shared_ptr<cogra_binary_slicer> cogra_binary_slicer_sptr;

/*!
* \brief Return a shared_ptr to a new instance of cogra_binary_slicer.
*
* To avoid accidental use of raw pointers, cogra_binary_slicer's
* constructor is private. cogra_binary_slicer is the public
* interface for creating new instances.
*/
COGRA_API cogra_binary_slicer_sptr cogra_binary_slicer ();


class COGRA_API cogra_binary_slicer : public gr_block
{
private:
// The friend declaration allows cogra_binary_slicer to
// access the private constructor.


// [POINT 1]
friend COGRA_API cogra_binary_slicer_sptr cogra_binary_slicer ();

cogra_binary_slicer (); // private constructor

public:
~cogra_binary_slicer (); // public destructor

// Where all the action really happens

int general_work (int noutput_items,
gr_vector_int &ninput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items);
}; // yes

#endif /* INCLUDED_COGRA_BINARY_SLICER */

cogra_binary_slicer.cc,创建实例的地方(失败)。标有 [POINT 2] 的故障点:

#include <cogra_binary_slicer.h>
#include <gr_io_signature.h>

/*
* Create a new instance of cogra_binary_slicer and return
* a boost shared_ptr. This is effectively the public constructor.
*/
cogra_binary_slicer_sptr
cogra_binary_slicer ()
{
return gnuradio::get_initial_sptr(new cogra_binary_slicer ()); **[POINT 2]**
}

以及 API header cogra_api.h:

#ifndef INCLUDED_COGRA_API_H
#define INCLUDED_COGRA_API_H

#include <gruel/attributes.h>

#ifdef gnuradio_cogra_EXPORTS
# define COGRA_API __GR_ATTR_EXPORT
#else
# define COGRA_API __GR_ATTR_IMPORT
#endif

#endif /* INCLUDED_COGRA_API_H */

我附上了那么多源代码,因为我无法缩小编译在该特定位置失败的原因。

为什么我的返回类型无效的一些指示可能会有所帮助。

最佳答案

将您的创建函数重命名为类名以外的名称:

friend COGRA_API cogra_binary_slicer_sptr cogra_binary_slicer ();

否则,编译器可能会将其视为具有返回类型的构造函数。

关于c++ - 构造函数的返回类型定义 - 如何修复此 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8743033/

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