0

So, this is the first time I actually separated a single program into a header and two .cpp files . But I think I am getting an linkage error . Heres how the directory looks . (heres a link to my image I dont have enough rep to post image in the question)

The main.cpp is my main source file where all the calling functions and other important stuff goes . In functions.cpp I have all my functions , in the coordin.h file I have the function prototypes and structures and Constants . Everything is ok no typo nothing I have checked everything . But I am getting an undefined reference to function error. I have included the coordin.h file too . Do you think the functions.cpp file needs to go somewhere else I mean is the compiler not looking inside that file ?

Hers the code :

main.cpp

#include <iostream>
#include "coordin.h"

using namespace std;

int main()
{
    rect rplace ;
    polar pplace;

    rect test = {45.89,25.4};
    pplace = rect_to_polar(test);

    cout<<pplace.angle<<endl;


    return 0;
}

coordin.h

#ifndef COORDIN_H_INCLUDED
#define COORDIN_H_INCLUDED 


/* Constants */

const double RAD_TO_DEG = 57.29577951;

struct polar{
    double distance; //distance from the origin
    double angle; //the angle from the origin
};

struct rect {
    double x; //horizontal distance form the origin
    double y; //vertical distance from the origin
};

/* Function prototypes */

polar rect_to_polar(rect xypos);
void show_polar(polar dapos);

#endif // COORDIN_H_INCLUDED

functions.cpp

/*
    functions.cpp
    contains all the function declarations
*/

#include <iostream>
#include "coordin.h" 
#include <cmath>

using namespace std;

//convert rectangular to polar coordinates
polar rect_to_polar(rect xypos){

    polar answer;

    answer.distance =
        sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
    answer.angle = atan2(xypos.y, xypos.x);

    return answer;
}

//show polar coordinate, converting angle to degrees

void show_polar(polar dapos){

    cout<<"Distance : " << dapos.distance <<endl;
    cout<<"Angle    : " << dapos.angle * RAD_TO_DEG <<" degrees."<<endl;
}

Heres the error :

Synxis
  • 9,236
  • 2
  • 42
  • 64
Nash Vail
  • 848
  • 2
  • 11
  • 27
  • Where did you include coordin.h? – Denis Ermolin Nov 19 '12 at 14:06
  • What exactly is the error? Can you show us your code? – wkl Nov 19 '12 at 14:06
  • You probably have a function for which you have provided a prototype in `coordin.h`, but no implementation in `functions.cpp`. Make sure that the exact function (with the same parameters and all) for which you are getting the error is implemented in `functions.cpp`. – Gorpik Nov 19 '12 at 14:09
  • Perhaps this answer will help: http://stackoverflow.com/questions/10803918/undefined-reference-to-sdl-main/10821071#10821071 – jrok Nov 19 '12 at 14:16
  • Deja vu... seen this before but I can't find the question – Caribou Nov 19 '12 at 14:18
  • @jrok I have all the chekcboxes ticked. – Nash Vail Nov 19 '12 at 14:19
  • @Lou I am new I don't really new what link line is :P – Nash Vail Nov 19 '12 at 14:20
  • The only possible conclusion from the various pieces of information is that the copy of functions.cpp that your compiler is compiling and your linker is linking is not the same as the copy you have posted. Everything else is correct. – john Nov 19 '12 at 14:24
  • Please show error messages as (code formatted) text, not images. – πάντα ῥεῖ Nov 19 '12 at 14:26
  • Perhaps you could post the content of the Build Log, might be useful – john Nov 19 '12 at 14:26
  • @nashmaniac Well I would check what I said. Are you sure you are posting the same code as your compiler is compiling? Do you perhaps have two copies of functions.cpp, you are posting one here but the compiler is compiling the other. There is nothing wrong with your code, it's how you are operating your compiler that is wrong. – john Nov 19 '12 at 14:27
  • Hey thanks everybody ! I right clicked on functions.cpp went to the properties and ticked the build and debug checkboxes its working fine now . @jrok thanks that helped. – Nash Vail Nov 19 '12 at 14:35

1 Answers1

1

Does Code::Blocks actually compile your functions.cpp file? Right-click functions.cpp in the project tree, select "Properties" and make sure that check-boxes "Build", "Release" are set — otherwise, Code::Block will ignore this file during the build and won't compile it.

Joker_vD
  • 3,715
  • 1
  • 28
  • 42