Point.h

Good Evening,
I created a Point class defined within two files, Point.h and Point.cpp.(following studies instructions) Then wrote a PointTest.cpp file that will initiate one Point object using the two-parameter constructor. When I run the PointTest.cpp file, I get the below error:

relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Point::getLocation()'
collect2: error: ld returned 1 exit status

Thanks' for the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef POINT_H
#define POINT_H
#include<string>
using namespace std;

class Point
{
    public:
    Point();
    Point(double, double);
    void setX(double);
    void setY(double);
    double getX();
    double getY();
    string getLocation();
    private:
    double x;
    double y;
};
#endif // ! POINT_H


#include"Point.h"

Point::Point()
{
   x = 0;
   y = 0;
}
Point::Point(double _x,double _y)
{
   x = _x;
   y = _y;
}

void Point::setX(double _x)
{
   x = _x;
}
void Point::setY(double _y)
{
   y = _y;
}
double Point::getX()
{
   return x;
}
double Point::getY()
{
   return y;
}
string Point::getLocation()
{
   if (x >= 0 && y >= 0)
       return "First";
   else if (x >= 0 && y < 0)
       return "Fourth";
   else if (x < 0 && y < 0)
       return "third";
   return "Second";
}


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

int main()
{
   double x, y;
   cout << "Enter point x and y values: ";
   cin >> x >> y;
   Point p(x,y);
   cout << "This Point is located at Quadrant " << p.getLocation() << endl;
   system("pause");
   return 1;
}


Last edited on
Hello studentlearningcplusplu,

Compiled your program in VS 2017 and with Code::Blocks, but could not duplicate your error. Both compiled and ran with not problem.

My questions would be:

What IDE/compiler are you using?

What C++ standard is it set for?

Is it a compile error or a linker error?

Andy
no ‘else’ in getLocation()
Good Evening Handy Andy,
For this class I am required to use TextPad 8
I am unsure how to response to below two question.

What C++ standard is it set for?

Is it a compile error or a linker error?
@studentlearningcplusplus

C++ standard = version. C++ 11, 17..

Compile error = compile time error(probably not the case)

Linker error = runtime error?
When I run the PointTest.cpp file
It would help if you familiarize yourself with the terminology. You don't run a file, you compile one or more files (compilation units), which are eventually linked together and formed into a final executable.
Here's a link: https://www.learncpp.com/cpp-tutorial/introduction-to-the-compiler-linker-and-libraries/

Yes, compile[r] error and compile-time error mean the same thing in this context.
A linker error is not a runtime error, it's all before the final executable is made. The error in your first post is specifically a linker error, but honestly that doesn't make too much of a difference here, the bigger point is that it isn't a run-time error.

What compiler are you using, and what is the command-line you are giving it?

________________________________________

Note that you didn't answer all of Handy Andy's questions.

I assume you have been able to successfully compile a "hello world" program?

If so, perhaps something has gone wrong with your existing files. If you're in a project, I suggest cleaning your project or making a new one and copying the code in manually. Since you have multiple files, make sure that all of your files are part of your project.
Last edited on
it is a compile error.
Hello studentlearningcplusplu,

I am not familiar with TextPad, but from I I did find it appears to be just an editor, which leads me to believe that you compile from the command line.

If the compiler is not set to any C++ standard you may have to add to the command line -std=C++11 or (C++14), no ().

I would suggest:
1
2
3
4
5
6
7
8
9
10
11
string Point::getLocation()
{
    if (x >= 0.0 && y >= 0.0)
        return "First";
    else if (x >= 0.0 && y < 0.0)
        return "Fourth";
    else if (x < 0.0 && y < 0.0)
        return "third";

    return "Second";
}

Otherwise you are comparing a double to an int and this could be a problem. It is always best to compare 2 things of the same type.

The "else" is not need, but I did add the blank line to make it easier to read.

Also it is best not to put using namespace std; in a header file. This could end up in a ".cpp" file where is is not wanted. See: http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Andy
it is a compile error.
I'm sorry to hear that. That gives us no more information than we had before, however.
Last edited on
Handy Andy wrote:
Otherwise you are comparing a double to an int and this could be a problem.

It is never a problem to compare a double and an int since the int will always be promoted to a double.

The "else" is not needed

None of the else's are needed since the code blocks all return.
@dutch,

Thanks. I did not realize that promotion would occur in a comparison. All I have known about is when doing a calculation between different types.

Andy
Thank you Everyone for the feedback.
I got the Point.h and PointTest.cpp to Compile and Run.
My last unresolved issue remains the Point.cpp file. It will not compile within TextPad8.

Error:against undefined symbol `WinMain'
collect2: error: ld returned 1 exit status

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include"Point.h"

Point::Point()
{
    x = 0;
    y = 0;
}

Point::Point(double _x,double _y)
{
    x = _x;
    y = _y;
}

void Point::setX(double _x)
{
    x = _x;
}

void Point::setY(double _y)
{
    y = _y;
}

double Point::getX()
{
    return x;
}

double Point::getY()
{
    return y;
}

//int main()

string Point::getLocation()
{
    if (x >= 0.0 && y >= 0.0)
        return "First";
    if (x >= 0.0 && y < 0.0)
        return "Fourth";
    if (x < 0.0 && y < 0.0)
        return "third";

    return "Second";
}
Last edited on
https://stackoverflow.com/questions/23918318/undefined-reference-to-winmain-in-cygwin

Perhaps Googling the message the same as I did might help :(
g++ -Wall PointTest.cpp Point.cpp -o program
Assuming PointTest.cpp contains main.

As againtry's link states, you can also do -c instead of -o to produce object files that you later link with a second command.
https://www.cs.bu.edu/teaching/cpp/separate-compilation/
Last edited on
Topic archived. No new replies allowed.