I keep getting a strange XCode error message when trying to prototype?

Oct 16, 2013 at 9:38pm
Hi guys, my code isn't fully written yet but when I try to use a prototype, I get an odd message.

Here is the code:

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
#include <cmath>
#include <math.h>
#include <iostream>

using namespace std;

int main()
{
    
    //Prototypes
    double randomDouble(const double&from, const double&to);
    
    double lineSegLen(const float& p0x, const float& p0y, const float& p1x, const float& p1y);
    
    bool lineInCircle(const float& lp0x, const float& lp0y, const float& lp1x, const float& lp1y, const float& cmpx, const float& cmpy, const float& radius);
    
    
    float epx1;
    float epy1;
    float epx2;
    float epy2;
    float radius;
    srand(time(0));
  
{
    //Declare all variables
    epx1 = randomDouble(199.9, - 99.9);
    epy1 = randomDouble(199.9, - 99.9);
    epx2 = randomDouble(199.9, - 99.9);
    epy2 = randomDouble(199.9, - 99.9);
    radius = randomDouble(0.1, 99.9);
}
    
    lineSegLen(epx1, epy1, epx2, epy2);
    
    return EXIT_SUCCESS;
    
}



Here is the error message I get:

Ld /Users/Michael/Library/Developer/Xcode/DerivedData/Class_program_3_(10-15-13)-ekcranvxtwakksfqwsylqhotdgbj/Build/Products/Debug/Class\ program\ 3\ (10-15-13) normal x86_64
    cd "/Users/Michael/Desktop/Computer Programming 1/Class program 3 (10-15-13)"
    setenv MACOSX_DEPLOYMENT_TARGET 10.8
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -L/Users/Michael/Library/Developer/Xcode/DerivedData/Class_program_3_(10-15-13)-ekcranvxtwakksfqwsylqhotdgbj/Build/Products/Debug -F/Users/Michael/Library/Developer/Xcode/DerivedData/Class_program_3_(10-15-13)-ekcranvxtwakksfqwsylqhotdgbj/Build/Products/Debug -filelist /Users/Michael/Library/Developer/Xcode/DerivedData/Class_program_3_(10-15-13)-ekcranvxtwakksfqwsylqhotdgbj/Build/Intermediates/Class\ program\ 3\ (10-15-13).build/Debug/Class\ program\ 3\ (10-15-13).build/Objects-normal/x86_64/Class\ program\ 3\ (10-15-13).LinkFileList -mmacosx-version-min=10.8 -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/Michael/Library/Developer/Xcode/DerivedData/Class_program_3_(10-15-13)-ekcranvxtwakksfqwsylqhotdgbj/Build/Intermediates/Class\ program\ 3\ (10-15-13).build/Debug/Class\ program\ 3\ (10-15-13).build/Objects-normal/x86_64/Class\ program\ 3\ (10-15-13)_dependency_info.dat -o /Users/Michael/Library/Developer/Xcode/DerivedData/Class_program_3_(10-15-13)-ekcranvxtwakksfqwsylqhotdgbj/Build/Products/Debug/Class\ program\ 3\ (10-15-13)

Undefined symbols for architecture x86_64: "lineSegLen(float const&, float const&, float const&, float const&)", referenced from: _main in p3v0.o "randomDouble(double const&, double const&)", referenced from: _main in p3v0.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)



What I quoted inside the output is what is really throwing me off! What am I doing wrong?

Thanks guys!
Last edited on Oct 16, 2013 at 9:38pm
Oct 16, 2013 at 10:08pm
Linker error. You don't have implementations for your functions.


PS. Do not declare functions inside (main) function's body.
Oct 16, 2013 at 11:32pm
How would I get implementations? I am a little confused on prototyping... Should I put the prototypes outside in the same place as say using namespace std;??
Oct 17, 2013 at 7:58am
Should I put the prototypes outside in the same place as say using namespace std;?

Yes.

What you call "prototyping" is "declaring". You declare that there exist a symbol "randomDouble" that refers to a function that takes two const double references as parameters and returns a double value. When you then call function randomDouble in your code (of function main) the compiler can check that you use correct types.

Later in compilation process a linker collects the object files generated by compiler and makes sure that the function call in main() actually jumps to the code that does whatever the function randomDouble does. Links.

What does the randomDouble do? Who has written the code for it?
Oct 17, 2013 at 4:23pm
Thank you for that explanation. I have rewritten it (not fully) to what I think it should be. Now my only problem is that something within the code itself isn't working which I will point out.

It is something I am writing for a homework assignment, here is the objective:

Purpose: Create a program which randomly generates 3 sets of x and y coordinates and one randomly generated Real. Two of the sets of coordinates will represent the end points of a line segment. The other set of coordinates and the Real will represent the midpoint of a circle and the circle's radius.
The coordinates should be randomly generated using a user defined function to return a number from -99.9 to 99.9. The radius should be a randomly generated number (using the same function) from 0.1 to 99.9. Determine if the line segment is wholly within the circle using a second user defined function. The program should display all of the generated data and one of the messages regarding the location of the line as shown below in Output Layout section. Create a third function to return the length of a line segment. Make sure the decimal places line up in your output.



Here is my code so far:
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
#include <cmath>
#include <math.h>
#include <iostream>

using namespace std;
 //Prototypes
    double randomDouble(const double&from, const double&to);
    
    double lineSegLen(const float& p0x, const float& p0y, const float& p1x, const float& p1y);
    
    bool lineInCircle(const float& lp0x, const float& lp0y, const float& lp1x, const float& lp1y, const float& cmpx, const float& cmpy, const float& radius);

int main()
{
    //Variables being used
    float mpx;
    float mpy;
    float lsx;
    float lsy;
    float radius;
    srand(time(0));
  
    //Declare all variables
    mpx = randomDouble(-99.9, 99.9);
    mpy = randomDouble(-99.9, 99.9);
    lsx = randomDouble(-99.9, 99.9);
    lsy = randomDouble(-99.9, 99.9);
    radius = randomDouble(0.1, 99.9);
    
    return EXIT_SUCCESS;
    
}
//Define randomDouble prototype
double randomDouble(const double& from, const double& to)
{
    randomDouble(-99.9, 99.9);
    
    return (rand());
}
//Define lineSegLen prototype
double lineSegLen(const float& epx1, const float& epy1, const float& epx2, const float& epy2)
{
    lineSegLen(mpx, lsx, mpy, lsy);
    
    return sqrt(pow(mpx – lsx, 2.0) + pow(mpy – lsy, 2.0);
}
//Define lineInCircle prototype
bool lineInCircle(const float& lp0x, const float& lp0y, const float& lp1x, const float& lp1y, const float& cmpx, const float& cmpy, const float& radius)
{
    return ();
}


Now when I run it, I get this
 1) Use of undeclared identifier 'mpx'; did you mean 'max'?
How is it undeclared? Didn't I declare it in my main function??
2)Non-ASCII characters are not allowed outside of literals and identifiers
This is referring to my using the minus sign "-"
3) Expected ')'
Where am I missing a close parenthesis?
4) Use of undeclared identifier 'mpy'
Once again, haven't I declared that already along with everything else?


Thanks for the help so far and thanks again in advance for the help to come.

This is really grinding my chops cause I keep looking it over and referring to my textbook and I cannot figure this one out.

To me, this is the code in full minus the outputs. I just need to fill in the blanks. If you think I need even more, then please by all means throw all your criticism right in my face like you want to beat me for stealing the last burger at the bbq.

EDIT: Before I forget, when I run it I get a warning about my srand(time(0)) line. It states
Implicit conversion loses integer precision: 'time_t' (aka 'long') to 'unsigned int'
but when I change it to 'unsigned int' I get the exact same "random" number each time. Little confused on this one, but not the main problem :)
Last edited on Oct 17, 2013 at 4:31pm
Oct 17, 2013 at 4:29pm
line 43: lineSegLen(mpx, lsx, mpy, lsy);

None of these variables (all defined in main) is visible in your function.

You have to somehow pass them to the function, either by changing it's signature and calling it differently, or making them global (not recommended).

Perhaps you meant to call the function and pass the variables using the exisitng signature, then you should be using epx1, epx2, epy1, epy2 instead.

Also, if this worked, wouldn't you be calling that function recursively until your stack blows up?
Last edited on Oct 17, 2013 at 4:32pm
Oct 17, 2013 at 5:42pm
Perhaps you meant to call the function and pass the variables using the exisitng signature, then you should be using epx1, epx2, epy1, epy2 instead.


Fudge. I changed the identifiers like 3 times because I kept wanting to label it differently. I forgot to change "epx1 back to mpx" and so on... I will try that when I am back in front of my laptop.

Also, how would I stop the program after running everything once?

Say: Give me my 3 random x and y points, and then display my bool and stop? I thought return EXIT_SUCCESS would do that?


EDIT: btw it should look like this right?

double lineSegLen(const float& mpx, const float& mpy, const float& lsx, const float& lsy)

instead of this?
double lineSegLen(const float& epx1, const float& epy1, const float& epx2, const float& epy2)
Last edited on Oct 17, 2013 at 5:48pm
Oct 18, 2013 at 12:55am
I fixed it guys!

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
#include <cmath>
#include <math.h>
#include <iostream>
#include <iomanip>

using namespace std;
 //prototypes
    double randomDouble(const double& from, const double& to);
    
    double lineSegLen(const float& lp0x, const float& lp0y, const float& lp1x, const float& lp1y);
    
    bool lineInCircle(const float& lp0x, const float& lp0y, const float& lp1x, const float& lp1y, const float& cmpx, const float& cmpy, const float& radius);

int main()
{
    //variables being used
    float cmpx;
    float cmpy;
    float lp0x;
    float lp0y;
    float lp1x;
    float lp1y;
    float radius;

    srand(time(0));
  
    //assign variable
    cmpx = randomDouble(-99.9, 99.9);
    cmpy = randomDouble(-99.9, 99.9);
    lp0x = randomDouble(-99.9, 99.9);
    lp0y = randomDouble(-99.9, 99.9);
    lp1x = randomDouble(-99.9, 99.9);
    lp1y = randomDouble(-99.9, 99.9);
    radius = randomDouble(0.1, 99.9);
    
    //output line segment data
    cout << right;
    cout << "Line segment's 1st x coordinate: " << setw(5) << lp0x << endl;
    cout << "Line segment's 1st y coordinate: " << setw(5) << lp0y << endl;
    cout << "Line segment's 2nd x coordinate: " << setw(5) << lp1x << endl;
    cout << "Line segment's 2nd y coordinate: " << setw(5) << lp1y << endl;
    
    //output midpoint data
    cout << "\nCircle's Midpoint x coordinate: " << cmpx << endl;
    cout << "Circle's Midpoint y coordinate: " << cmpy << endl;
    
    //output radius data
    cout << "\nCircle's Radius: " << radius << endl;
    
    //output bool statement
    if (lineInCircle(lp0x, lp0y, lp1x, lp1y, cmpx, cmpy, radius))
        cout << "\nThe line segment is in the circle!" << endl;
        
        else
            cout<< "\nThe line segment is not in the circle!" << endl;
    
    
    return EXIT_SUCCESS;
    
}
//define randomDouble prototype
double randomDouble(const double& from, const double& to)
{
    return rand() % static_cast<int>((to - from -.1) * 10.0) / 10.0 + from;
}

//define lineSegLen prototype
double lineSegLen(const float& lp0x, const float& lp0y, const float& lp1x, const float& lp1y)
{
    return sqrt(pow(lp0x - lp1x, 2.0) + pow(lp0y - lp1y, 2.0));
}

//define lineInCircle prototype
bool lineInCircle(const float& lp0x, const float& lp0y, const float& lp1x, const float& lp1y, const float& cmpx, const float& cmpy, const float& radius)
{
    return lineSegLen(lp0x, lp0y, cmpx, cmpy) <= radius && lineSegLen(lp1x, lp1y, cmpx, cmpy) <= radius;
}



This is the code in full
Oct 18, 2013 at 12:58am
closed account (jwkNwA7f)
#include <cmath>
#include <math.h>

Aren't these two the same?
Oct 23, 2013 at 4:24pm
retsgorf297, yes they are. I forgot to take out the second math. Thank you for pointing that out!
Topic archived. No new replies allowed.