无法将共享指针作为参数传递给函数定义?
Can't pass shared pointer as parameter to function definition?
如果我将共享指针作为参数的函数声明及其定义放在标头中,则一切都可以正常编译,但是尝试将它们分成.hpp
和.cpp
文件会导致编译错误,例如:
Use of undeclared identifier 'std' //Solved via including <memory> & <cstdint>
Variable has incomplete type 'void' //Solved via including <memory> & <cstdint>
Use of undeclared identifier 'uint8_t' //Solved via including <memory> & <cstdint>
例如:
对于这个主要:主要.cpp
#include <iostream>
#include "example1.hpp" //Switch out with "example2.hpp"
int main(int argc, const char * argv[]) {
std::shared_ptr<uint8_t> image = 0;
foo(image);
return 0;
}
这有效:
示例1.hpp
#ifndef example1_hpp
#define example1_hpp
#include <stdio.h>
void foo(std::shared_ptr<uint8_t> variable) { }
#endif /* example1_hpp */
这不起作用:
例2.cpp
#include "example2.hpp"
void foo(std::shared_ptr<uint8_t> variable) { }
示例2.hpp
#ifndef example2_hpp
#define example2_hpp
#include <stdio.h>
void foo(std::shared_ptr<uint8_t> variable);
#endif /* example2_hpp */
如何将此函数的声明和定义成功分离到单独的文件中?
你有错误的包含。 <stdio.h>
是一个 C 标头。要使用shared_ptr
您需要包含<memory>
,要使用uint8_t
您需要包含<cstdint>
。
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 使用指向成员的指针将成员函数作为参数传递
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 修改函数中的指针(将另一个指针作为参数传递)
- 将成员函数指针作为参数传递给模板方法
- 如何在C++中将迭代器作为函数参数传递
- 将附加参数传递给使用 beast::bind_front_handler 调用的函数
- 如何将一个类的函数作为另一个类的另一个函数的参数传递
- 如何传递带有通过引用传递的结构参数的函数?
- 如何在 c++ 中将函数作为参数传递?
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
- 为什么我不能将引用作为 std::async 的函数参数传递
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 将__device__ lambda 作为参数传递给 __global__ 函数
- 是否可以在不填充自己的参数的情况下将模板函数作为参数传递?
- 如何将成员函数作为参数传递并在派生对象上执行方法列表
- 将函数作为参数传递以避免重复代码
- 如何在类(C )中作为参数传递函数