Problem compiling multiple files

I am having trouble linking these 3 files. I copied them directly from the book, but i cant seem to find the problem. I did some research and i found out something about having to put the function prototypes in main. I am using Dev c++ and these are the errors that im getting.

[Linker error] undefined reference to `rect_to_polar(rect)'
[Linker error] undefined reference to `show_polar(polar)'

Can someone please tell me what im doing wrong
coordin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef COORDIN_H_
#define COORDIN_H_

struct polar
{
       double distance;
       double angle;
};
struct rect
{
       double x;
       double y;
};

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

#endif 


file1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream.h>
#include <conio.h>
#include "coordin.h"

using namespace std;
int main ()
{   
    rect rplace;
    polar pplace;
    
    cout << "Enter the x and y values: ";
    while (cin >> rplace.x >> rplace.y)
    {
          pplace = rect_to_polar(rplace);
          show_polar(pplace);
          cout << "Next two numbers (q to quit): ";
    }
    
    cout << "Bye!\n";
      
    getch();
    return 0;
}


file2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream.h>
#include <conio.h>
#include <cmath>
#include "coordin.h"

polar rect_to_polar(rect xypos)
{
      using namespace std;
      polar answer;
      answer.distance = sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
      answer.angle = atan2(xypos.y, xypos.x);
      return answer;
}

void show_polar(polar dapos)
{
     using namespace std;
     const double Rad_to_deg = 57.29577951;
     
     cout << "distance = " << dapos.distance;
     cout << ", angle = " << dapos.angle * Rad_to_deg;
     cout << " degrees\n";
}
Last edited on
Are both .cpp files set to be compiled? Linker errors are caused by the compiler finding a function declaration, but not the definition (i.e. the .cpp file with the definition wasn't compiled).
I tried compiling each one separately but i kept getting errors for both cpp files. Is there a way to compile them both in Dev C++
Is there an option for "Build Project", "Build Solution", "Build All" or something similar.

PS: It seems that many people on this forum (probably including me) would recommend against use of Dev C++, as you can see here http://www.cplusplus.com/forum/articles/36896/
You should try Visual C++ Express or Code::Blocks :)

Hope this helps.
There is only one option that says rebuild all. I have tried this but i keep getting the same errors.
There should be a button that looks like a box with an arrow pointing into it. Click on it and then add the other source file. You must have them in a project though, not just by themselves.
I tried putting them in a project but the same errors keep appearing. I downloaded code blocks and it still gives me the same errors. I guess it might be some error in my code or im not linking them properly.
Your code looks fine, are both files set to compile?
wow im such an idiot, can someone please slap me lol. I just got them to work both on dev c++ and code blocks. I had to add each file into a project file, and then just compile them. Thanks to everyone who helped me.
Everyone does stuff like that, especially me. lol
Topic archived. No new replies allowed.