Hello,

I was trying to work a problem from the book and decided to compile and run sample code that is already supposed to work. The problem is it doesn't. The concept of breaking these files up is new to me. Can someone please let me know what to do? The idea behind this code is to take 2 files and get them to run with a header. Does it matter which order these are compiled or exactly how does this go? I have read through the book and it isn't really listed. Sample output can be:

Enter the x and y value: 120 80
distance= 144.222, angle = 33.6901 degrees
Next two numbers (q to quit): 120 50
distance= 130, angle= 22.6199 degrees
Next two numbers (q to quit): q

When I try to compile file2.cpp, I get:

[Linker error] undefined reference to `WinMain@16'
and
ld returned 1 exit status

When I try to compile file1.cpp, I get:

C:\DOCUME~1\STACYA~1\LOCALS~1\Temp\ccSGbaaa.o(.text+0x1a0) In function `main':   
and
[Linker error] undefined reference to `rect_to_polar(rect)' 
and
[Linker error] undefined reference to `show_polar(polar)' 
and
 C:\DOCUME~1\STACYA~1\LOCALS~1\Temp\ccSGbaaa.o(.text+0x1a0) ld returned 1 exit status 

coordin.h

#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

#include <iostream>
#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";
    system("PAUSE");
    return EXIT_SUCCESS;
}

file2.cpp

#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";
      }

I am using Dev-C++.

Any help will be greatly appreciated.

Did u save the header & the implementation files in the same folder?

Member Avatar for iamthwee

The problem isn't with the addition of your headers files but with your code!

Yup, I can see them now....Thanks iamthwee

How do you save it in the same folder?

What is wrong with the code? This is straight from the book.

Thanks

Hey folks, I got this one figured out. Sorry for asking. I will try harder next time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.