0

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;
}
4

2 回答 2

1

您还没有为LOCATION该类编写构造函数。您声明了 a LOCATIONnameda和 aSensorNode其中包含 a LOCATION,但链接器无法确定LOCATION构造函数的代码在哪里,因此无法链接。为该类编写一个构造函数,LOCATION您应该会很好。

于 2013-05-02T05:11:27.353 回答
0

你好像忘记执行了SensorNode。在 SensorNode.h 中,您声明了一个SensorNode具有数据和公共方法的类,但在 SensorNode.cpp 中,您没有提供SensorNode(构造函数)print等的实现。链接器无法找到这些实现,因为它们尚未实现,因此链接器错误。

这是您可以开始使用的一些样板:

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)
{
}
于 2013-05-02T07:15:58.247 回答