0
votes

Xcode gives me 3 "Apple Mach-O Linker (Id) Error" errors. But, when I click them it doesn't direct me to a line in my code, so I don't know what/where the problem is. I know others have asked this question, but all of the solutions that I could find were specific to each individual's code. I am learning C++, so these errors are coming as part of a beginner program I'm working on.

Apple Mach-O Linker (Id) Error "SensorNode::SensorNode(char*, float, float, float, int, float)", referenced from: Apple Mach-O Linker (Id) Error "LOCATION::LOCATION()", referenced from: Apple Mach-O Linker (Id) Error Linker command failed with exit code 1 (use -v to see invocation)

If it helps, I'll put my code here: here's my "sensor_node.h"

#ifndef SENSORNODE_H
#define SENSORNODE_H

#include <iostream>

class LOCATION {
    float lat, longi, height;

public:
    LOCATION ();
    void setx(float xx);
    void sety(float yy);
    void setz(float zz);
    void print();
};

class SensorNode {
    char* NodeName;
    int NodeID;
    LOCATION Node1;
    float batt;
    int func;


public:
    SensorNode(char *n, float x, float y, float z, int i, float ah);
    void print();
    void setOK(int o);
    int getOK();
    void setLOC(float longi, float lat, float h);
};

#endif /* defined(__Project_3__sensor_node__) */

here's my sensor_node.cpp:

#include "sensor_node.h"
//#include <iostream>
using namespace std;


void LOCATION::setx(float xx) {
    lat = xx;
    if (lat > 180.0 || lat < -180.0) {
        cout << "Latitude is not in the -180 to 180 degree range";
        lat = 0.0;
    }
}

void LOCATION::sety(float yy) {
    longi = yy;
    if (longi > 180.0 || longi < -180.0) {
        cout << "Latitude is not in the -180 to 180 degree range";
        longi = 0.0;
    }


}
void LOCATION::setz(float zz) {
    height = zz;
}

void LOCATION::print() {

    cout << "(LONGITUDE: " << longi << " ,LATITUDE: " << lat << " ,HEIGHT: " << height << " )";
}

and here's my main.cpp:

#include <iostream>
using namespace std;

#include "sensor_node.h"

int main() {

    LOCATION a; SensorNode s1("Pulse",15.9,-30.1,0,157,2.0);

    cout << "Beginning LOCATION tests.\n\n";
    cout << "  After initial construction:  ";
    a.print();
    cout << "\n";
    a.setx(-45.3);
    a.sety(27.6);
    a.setz(3.5);
    cout << "  After setting x/y/z to -45.3/27.6/3.5:  ";
    a.print();
    cout << "\n";

    cout << "  After attempting to set longitude to 180.1:  ";
    a.setx(180.1);
    a.print();
    cout << "\n";

    cout << "  After attempting to set longitude to -180.1:  ";
    a.setx(-180.1);
    a.print();
    cout << "\n";

    cout << "  After attempting to set latitude to 180.1:  ";
    a.sety(180.1);
    a.print();
    cout << "\n";

    cout << "  After attempting to set latitude to -180.1:  ";
    a.sety(-180.1);
    a.print();
    cout << "\n";
/*
    cout << "\n\n\n\nBeginning sensor node tests.\n\n";
    cout << "  After initial construction:";
    s1.print();
    cout << "\n  Printing the value returned by getOK: " << s1.getOK();
    cout << "\n  After changing location to 20/30/40:";
    s1.setLOC(20,30,40);
    s1.print();
    cout << "\n  After trying to set location illegally:";
    s1.setLOC(181, -181, 10);
    s1.print();
    cout << "\n  Node fails, then try to change location:";
    s1.setOK(0);
    s1.setLOC(5,10,15);
    s1.print();
    cout << "\n  Printing the value returned by getOK: " << s1.getOK();
    cout << "\n\n\n  End of tests.\n";

    cout << "Enter an integer to quit: ";
    cin >> hold;
*/
return 0;
}
2

2 Answers

1
votes

You haven't written the constructor for the LOCATION class. You declare a LOCATION named a and a SensorNode which contains a LOCATION, but the linker can't figure out where the code for the LOCATION constructor is, so it can't link. Write a constructor for the LOCATION class and you should be good.

0
votes

You seem to have forgotten to implement SensorNode. In SensorNode.h you declare a class, SensorNode which has data and public methods, but in SensorNode.cpp you are not providing an implementation of SensorNode (constructor), print etc. The linker is unable to find implementations of these since they haven't been implemented, hence the linker error.

Here's some boilerplate you can start out with:

SensorNode::SensorNode(char *n, float x, float y, float z, int i, float ah)
{
}

void SensorNode::print()
{
}

void SensorNode::setOK(int o)
{
}

int SensorNode::getOK()
{
}

void SensorNode::setLOC(float longi, float lat, float h)
{
}