Search code examples
c++headerxcode8declarationshared-ptr

Can't pass shared pointer as parameter to function definition?


If I place a function declaration with a shared pointer as a parameter and its definition in the header, everything compiles fine, but attempting to separate them into a .hpp and .cpp file causes compilation errors like:

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>

For example:

For this main: 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;
}

This Works:

example1.hpp

#ifndef example1_hpp
#define example1_hpp

#include <stdio.h>

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

#endif /* example1_hpp */

This does not work:

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 */

How do I separate the declaration and definition for this function into separate files successfully?


Solution

  • You have wrong includes. <stdio.h> is a C header. To use shared_ptr you need to include <memory>, to use uint8_t you need to include <cstdint>.