locked
Custom Header File Trouble with #typedef RRS feed

  • Question

  • Hello,

    I've been working on a custom header file of vector calculation functions, like adding vectors and finding the dot product. Unfortunately, I've hit a snag. To make things easier, I made a type called 'Vector', and it's a struct of three floats: x, y, and z. I use a return type of 'Vector' a lot in my functions. The problem I ran in to was that when I tried to compile some simple code, I got these errors:

    1>main.obj : error LNK2019: unresolved external symbol "struct Vector __cdecl vecAdd(struct Vector,struct Vector)" (?vecAdd@@YA?AUVector@@U1@0@Z) referenced in function _main

    1>main.obj : error LNK2019: unresolved external symbol "struct Vector __cdecl vecInit(float,float,float)" (?vecInit@@YA?AUVector@@MMM@Z) referenced in function _main

    I'll post my code, along with my header file and .cpp file that goes along with the header file.

    Thanks in advance for your help.

    Main code:
    #include <iostream> 
    #include <Vectors.h> 
     
    using namespace std; 
     
    int main() 
        Vector vec1; 
        Vector vec2; 
        Vector vec3; 
     
        vec1 = vecInit(5, 6, 7); 
        vec2 = vecInit(7, 8, 9); 
        vec3 = vecAdd(vec1, vec2); 
     
        system("pause"); 
        return 0; 

    This is the header file:
    struct Vector { 
        float x; 
        float y; 
        float z; 
    }; 
    typedef struct Vector Vector; 
     
    Vector vecInit(float x, float y, float z); 
    Vector vecSetAngle(float angle); 
     
    Vector vecNormalize(Vector vector); 
     
    float vecGetMagnitude(Vector vector); 
    float vecGetMagnitudeSquared(Vector vector); 
    Vector vecAdd(Vector vector1, Vector vector2); 
    Vector vecSubtract(Vector vector1, Vector vector2); 
    float vecDotProduct(Vector vector1, Vector vector2); 
    Vector vecCrossProduct(Vector vector1, Vector vector2); 
    Vector vecPerpendicular2D(Vector vector); 
     



    And this is the .cpp file:
    #include "Vectors.h" 
    #include <math.h> 
     
    Vector vecInit(float x, float y, float z)  
        Vector vector; 
         
        vector.x = x; 
        vector.y = y; 
        vector.z = z; 
     
        return vector; 
     
    Vector vecSetAngle(float angle)  
        Vector vector; 
         
        vector.x = cos(angle); 
        vector.y = sin(angle); 
        vector.z = 0.0f;; 
     
        return vector; 
     
    Vector vecNormalize(Vector vector)  
        float magnitude; 
         
        magnitude = sqrt((vector->x * vector->x) + 
                         (vector->y * vector->y) + 
                         (vector->z * vector->z)); 
        vector.x /= magnitude; 
        vector.y /= magnitude; 
        vector.z /= magnitude; 
     
    float vecGetMagnitude(Vector vector)  
        return sqrt((vector.x * vector.x) + 
                    (vector.y * vector.y) + 
                    (vector.z * vector.z)); 
     
    float vecGetMagnitudeSquared(Vector vector)  
        return ((vector.x * vector.x) + 
                (vector.y * vector.y) + 
                (vector.z * vector.z)); 
     
    Vector vecAdd(Vector vector1, Vector vector2)  
        Vector vector; 
         
        vector.x = (vector1.x + vector2.x); 
        vector.y = (vector1.y + vector2.y); 
        vector.z = (vector1.z + vector2.z); 
     
        return vector; 
     
    Vector vecSubtract(Vector vector1, Vector vector2)  
        Vector vector; 
         
        vector.x = (vector1.x - vector2.x); 
        vector.y = (vector1.y - vector2.y); 
        vector.z = (vector1.z - vector2.z); 
     
        return vector; 
     
    float vecDotProduct(Vector vector1, Vector vector2)  
        return ((vector1.x * vector2.x) + 
                (vector1.y * vector2.y) + 
                (vector1.z * vector2.z)); 
     
    Vector vecCrossProduct(Vector vector1, Vector vector2)  
        Vector vector; 
         
        vector.x = ((vector1.y * vector2.z) - (vector1.z * vector2.y)); 
        vector.y = ((vector1.z * vector2.x) - (vector1.x * vector2.z)); 
        vector.z = ((vector1.x * vector2.y) - (vector1.y * vector2.x)); 
     
        return result; 
     
    Vector vecPerpendicular2D(Vector vector) 
        Vector temp; 
     
        temp.x = -vector.y; 
        temp.y = vector.x; 
        temp.z = 0.0f; 
     
        return temp; 
     



    • Edited by Lil_Ozzy Tuesday, September 30, 2008 1:43 PM typo
    Tuesday, September 30, 2008 5:55 AM

Answers

  • Right-click the "Source Files" folder in the Solution Explorer window.  Add + Existing Item, select your vectors.cpp file (or whatever it is called).
    Hans Passant.
    • Marked as answer by Lil_Ozzy Tuesday, September 30, 2008 2:50 PM
    Tuesday, September 30, 2008 2:44 PM

All replies

  • Yes Because these Two Function are Undeclared in your <Vector.h> and you are trying to invoke them That's why you are getting error inside your program.At least in your code no where i had found this function Vector_withValues()

    Thanx

    Rupesh Shukla
    • Edited by Pintu Shukla Tuesday, September 30, 2008 7:17 AM
    Tuesday, September 30, 2008 7:15 AM
  • Could you confirm that all of your Vector.* and Vectors.* files (H and CPP) are inserted into your project? (Right click on your project in Solution Explorer, then Add -> Existing Item).

    Tuesday, September 30, 2008 8:04 AM
  • Change Vector_withValues to vecInit and Vector_add to vecAdd.  You'll need to use the function names as declared in the .h file, you can't arbitrarily make up more readable names.
    Hans Passant.
    Tuesday, September 30, 2008 1:06 PM
  • Oh, oops. I had forgotten that I edited those. I guess I must have posted an old version of the script, the names of the functions in my program are now vecInit and vecAdd.

    Tuesday, September 30, 2008 1:36 PM
  • I just tried inserting the files into my project, but to no avail.

    • Edited by Lil_Ozzy Tuesday, September 30, 2008 1:51 PM
    Tuesday, September 30, 2008 1:50 PM
  • Right-click the "Source Files" folder in the Solution Explorer window.  Add + Existing Item, select your vectors.cpp file (or whatever it is called).
    Hans Passant.
    • Marked as answer by Lil_Ozzy Tuesday, September 30, 2008 2:50 PM
    Tuesday, September 30, 2008 2:44 PM
  • Yes! It worked! Thank you so much.
    Tuesday, September 30, 2008 2:50 PM
  • Lil_Ozzy said:

    Oh, oops. I had forgotten that I edited those. I guess I must have posted an old version of the script, the names of the functions in my program are now vecInit and vecAdd.


    Instead of modify your old Post put new code in new post .otherwise all previous suggestion will become wrong here and they will create wrong meaning with respect to your new code.And this is not a good idea as i think to write the code better if you use some class concept here and put your all function and variable inside your class and then with the help of class object perform your operation

    Thanx

    Rupesh Shukla
    • Edited by Pintu Shukla Tuesday, September 30, 2008 3:01 PM
    Tuesday, September 30, 2008 2:52 PM