不能将共享指针作为参数传递给函数定义吗?

时间:2017-06-22 07:20:44

标签: c++ header xcode8 declaration shared-ptr

如果我将一个带有共享指针的函数声明作为参数及其定义放在头文件中,那么一切编译都很好,但是尝试将它们分成.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>

例如:

对于这个主要: main.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;
}

本作品:

example1.hpp

#ifndef example1_hpp
#define example1_hpp

#include <stdio.h>

    void foo(std::shared_ptr<uint8_t> variable) { }

#endif /* example1_hpp */

这不起作用:

example2.cpp

#include "example2.hpp"

    void foo(std::shared_ptr<uint8_t> variable) {  }

example2.hpp

#ifndef example2_hpp
#define example2_hpp

#include <stdio.h>

    void foo(std::shared_ptr<uint8_t> variable);

#endif /* example2_hpp */

如何将此功能的声明和定义成功分离到单独的文件中?

1 个答案:

答案 0 :(得分:2)

你有错误的包含。 <stdio.h>是C标头。要使用shared_ptr,您需要包含<memory>,要使用uint8_t,您需要添加<cstdint>