我对C++相当陌生,所以也许我做错了什么,但是我无法弄清楚它是什么。
我对数据结构类的分配是实现以下头文件:
#ifndef __CS20A__Test__
#define __CS20A__Test__
#include <stdio.h>
namespace cs20a
{
class ThreeDPoint
{
public:
ThreeDPoint(int x, int y, int z);
double Distance() const;
friend bool operator ==(const ThreeDPoint &pt1, const ThreeDPoint &pt2);
friend bool operator !=(const ThreeDPoint &pt1, const ThreeDPoint &pt2);
friend ThreeDPoint operator +(const ThreeDPoint& pt1, const ThreeDPoint& pt2);
friend ThreeDPoint operator -(const ThreeDPoint& pt1, const ThreeDPoint& pt2);
//dot product
friend int operator *(const ThreeDPoint& pt1, const ThreeDPoint& pt2);
int getX() const;
int getY() const;
int getZ() const;
private:
int x;
int y;
int z;
};
}
#endif /* defined(__CS20A__Test__) */
这是我的实现:
#include“ThreeDPoint.h”
#包括
#包括
using namespace cs20a;
ThreeDPoint::ThreeDPoint(int x, int y, int z)
{
this->x = x;
this->y = y;
this->z = z;
}
double ThreeDPoint::Distance() const
{
return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
}
bool operator== (ThreeDPoint &pt1, ThreeDPoint &pt2)
{
return ((pt1.getX() == pt2.getX()) && (pt1.getY() == pt2.getY()) && (pt1.getZ() == pt2.getZ()));
}
bool operator !=(const ThreeDPoint &pt1, const ThreeDPoint &pt2)
{
return !(pt1.getX() == pt2.getX() && pt1.getY() == pt2.getY() && pt1.getZ() == pt2.getZ());
}
ThreeDPoint operator +(const ThreeDPoint& pt1, const ThreeDPoint& pt2)
{
return ThreeDPoint(pt1.getX() + pt2.getX(), pt1.getY() + pt2.getY(), pt1.getZ() + pt2.getZ());
}
ThreeDPoint operator -(const ThreeDPoint& pt1, const ThreeDPoint& pt2)
{
return ThreeDPoint(pt1.getX() - pt2.getX(), pt1.getY() - pt2.getY(), pt1.getZ() - pt2.getZ());
}
int operator *(const ThreeDPoint& pt1, const ThreeDPoint& pt2)
{
return pt1.getX() * pt2.getX() + pt1.getY() * pt2.getY() + pt1.getZ() * pt2.getZ();
}
int ThreeDPoint::getX() const {
return x;
}
int ThreeDPoint::getY() const {
return y;
}
int ThreeDPoint::getZ() const {
return z;
}
这两个文件本身可以很好地编译,但是当我添加类似
#include <iostream>
#include "ThreeDPoint.h"
int main(int argc, const char * argv[]) {
cs20a::ThreeDPoint pt1(1,2,3);
cs20a::ThreeDPoint pt2(1,2,3);
// insert code here...
std::cout << (pt1 == pt2);
return 0;
}
或任何带有ThreeDPoint == ThreeDPoint或ThreeDPoint + ThreeDPoint的驱动程序,当我尝试构建它时,出现以下错误:
Ld /Users/jaredjensen/Library/Developer/Xcode/DerivedData/CS20A-fztpdusrphrftebtzmkhjpjcduym/Build/Products/Debug/CS20A normal x86_64
cd "/Users/jaredjensen/Google Drive/XCode/CS20A"
export MACOSX_DEPLOYMENT_TARGET=10.9 /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.10.sdk -L/Users/jaredjensen/Library/Developer/Xcode/DerivedData/CS20A-fztpdusrphrftebtzmkhjpjcduym/Build/Products/Debug -F/Users/jaredjensen/Library/Developer/Xcode/DerivedData/CS20A-fztpdusrphrftebtzmkhjpjcduym/Build/Products/Debug -filelist /Users/jaredjensen/Library/Developer/Xcode/DerivedData/CS20A-fztpdusrphrftebtzmkhjpjcduym/Build/Intermediates/CS20A.build/Debug/CS20A.build/Objects-normal/x86_64/CS20A.LinkFileList -mmacosx-version-min=10.9 -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/jaredjensen/Library/Developer/Xcode/DerivedData/CS20A-fztpdusrphrftebtzmkhjpjcduym/Build/Intermediates/CS20A.build/Debug/CS20A.build/Objects-normal/x86_64/CS20A_dependency_info.dat -o /Users/jaredjensen/Library/Developer/Xcode/DerivedData/CS20A-fztpdusrphrftebtzmkhjpjcduym/Build/Products/Debug/CS20A
Undefined symbols for architecture x86_64:
"cs20a::operator==(cs20a::ThreeDPoint const&, cs20a::ThreeDPoint const&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我已经尝试了所有方法,但无法修复此错误。仅当我使操作员成为 friend 功能时才会发生这种情况。如果我让他们成为成员,然后删除其中一个参数,它就可以正常工作。我做错什么了吗?
最佳答案
您在cpp
文件中忘记ThreeDPoint::之前的运算符,并使用二进制运算符代替了friend
/*.cpp add ThreaDproint before operator*/
bool ThreeDPoint::operator== (ThreeDPoint const & pt2)
{
return ((this->x == pt2.getX()) && (this->y == pt2.getY()) && (this->z == pt2.getZ()));
}
bool ThreeDPoint::operator !=(const ThreeDPoint &pt2)
{
return !(this->x == pt2.getX() && this->y == pt2.getY() && this->z == pt2.getZ());
/* here you can do this
return !(this->operator==(pt2)); */
}
/* .h change friend for binary operator*/
bool operator ==(const ThreeDPoint &pt2);
bool operator !=(const ThreeDPoint &pt2);
编辑:强制使用 friend 关键字
/* add cs20a:: before operator== */
bool cs20a::operator==(const ThreeDPoint &pt1, const ThreeDPoint &pt2)
{
return (pt1.x == pt2.x && pt1.y == pt2.y && pt1.z == pt2.z);
}
祝好运 :)
关于c++ - 在C++中使运算符成为 friend 时,Apple Mach-O Linker(Id)错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31091597/