Xcode给了我3“Apple Mach-O Linker(Id)Error”错误。但是,当我点击它们时,它并没有指向我的代码中的一行,所以我不知道问题是什么/在哪里。我知道其他人已经问过这个问题,但我能找到的所有解决方案都是针对每个人的代码而定的。我正在学习C ++,所以这些错误是我正在研究的初学者计划的一部分。
Apple Mach-O链接器(Id)错误 “SensorNode :: SensorNode(char *,float,float,float,int,float)”,引自: Apple Mach-O链接器(Id)错误 “LOCATION :: LOCATION()”,引自: Apple Mach-O链接器(Id)错误 链接器命令失败,退出代码为1(使用-v查看调用)
如果有帮助,我会把我的代码放在这里:
这是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__) */
这是我的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 << " )";
}
这是我的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;
}
答案 0 :(得分:1)
您尚未编写LOCATION
类的构造函数。您声明名为LOCATION
的{{1}}和包含a
的{{1}},但链接器无法确定SensorNode
构造函数的代码在哪里,所以它无法链接。为LOCATION
类编写一个构造函数,你应该很好。
答案 1 :(得分: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)
{
}