Link error with really simple functions C++ on .h file

后端 未结 4 1014
离开以前
离开以前 2021-01-05 19:09

I\'ve made two functions to \'cast\' a 32/64 bit pointer into a double. The code worked when used alone (Just the .h and a .cpp including it) but when using the .h somewhere

4条回答
  •  囚心锁ツ
    2021-01-05 19:30

    The canonical way to would be:

    floatcast.h

    #pragma once
    #ifndef __FLOATCAST_H
    #define __FLOATCAST_H
    
    //A quick and dirty way of casting pointers into doubles and back
    //Should work with BOTH 64bit and 32bit pointers
    union ptr_u {
        double d;
        void* p;
    };
    
    double ptr2double(void* pv);
    void* double2ptr(double dv);
    
    #endif
    

    floatcast.cpp

    #include "floatcast.h"
    
    double ptr2double(void* pv){
        ptr_u ptr;
        ptr.p = pv;
        return (ptr.d);
    };
    
    void* double2ptr(double dv){
        ptr_u ptr;
        ptr.d = dv;
        return(ptr.p);
    };    
    

提交回复
热议问题