Typedef 期望在" "之前';'
Typedef expecting ';' before ""
(不要因为使用std::auto_ptr<>而让我抓狂,这不是我的代码,它是自动生成的。我只是想和它连接。)
我有一个签名如下的函数:
std::auto_ptr<T> gFunction(const std::string&, int, const int&)
这个函数被重载了588次,所以将它赋值给boost::function
是有歧义的。好的,我将重新定义它,然后赋值。
像这样键入很麻烦:
std::auto_ptr<T> (*func)(const std::string&, int, const int&) = &gFunction
我想typedef
这个,所以我可以使用它在以下方式:
function_type func = &gFunction
所以我尝试键入pedef
typedef std::auto_ptr<T>(*funct)(const std::string&, int, const int&) function_type;
但是我的typedef返回的是error: expected ‘;’ before ‘function_type’
任何想法?我可能遗漏了一些简单的东西。
就这样做:
typedef std::auto_ptr<T>(*function_type)(const std::string&, int, const int&);
还请记住,自c++ 11以来,std::auto_ptr
已被弃用。如果可以,您应该使用std::unique_ptr
。
在c++ 11中,您还可以使用std::add_pointer
类型trait来添加指向函数类型的指针(如果这对您来说更直观):
#include <type_traits>
typedef typename std::add_pointer<
std::shared_ptr<T>(const std::string&, int, const int&)
>::type function_type;
另外,正如Mike Seymour在评论中提到的,您可以考虑将类型别名function_type
定义为函数类型(正如其名称所暗示的那样),而不是使其成为函数指针类型(只需删除*
即可)。
声明类型定义的正确语法应该模仿变量声明。在语法上,它们是相同的,除了你用'typedef'作为前缀。从语义上讲,声明的名称具有非常不同的目的(一个创建变量,一个创建类型)
就像您输入下面的语句来声明变量
一样std::auto_ptr<T> (*func)(const std::string&, int, const int&);
你可以把它设为typedef只要在前面加一个"typedef"就行了
typedef std::auto_ptr<T> (*func)(const std::string&, int, const int&);
- C++:TypeDef使用元组
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 为什么在使用typedef时类推导指南会失败
- 从类型std::函数传递变量失败,尽管调用方期望的类型完全相同
- 尝试根据类中 typedef 的存在来专门化模板函数
- 如何重新定义MPI_FLOAT,MPI_DOUBLE以 typedef 的方式
- typedef 枚举和枚举类有什么区别?
- 一元*运算符的操作数是否期望一个 prvalue
- 在类模板中使用 typedef 时出错
- 模板类中的 typedef 语句
- 在 typedef 内部使用 const 关键字和在 typedef 外部使用 const 关键字之间有区别吗?
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 什么 /可以/ 你从一个函数返回. 并期望它在到达时还活着?
- C++中"typedef"、"using"、"namespace"和"using namespace"有什么区别?
- 向前声明指向类的 typedef 指针
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 如何创建 typedef 结构的前向声明
- 从 Typedef 数据类型中删除常量
- C++在标准库容器中的typedef不是您所期望的
- Typedef 期望在" "之前';'