gpt4 book ai didi

c++ - Apple Mach-O Linker (Id) 在 C++ 中创建运算符友元函数时出错

转载 作者:行者123 更新时间:2023-11-30 05:41:37 27 4
gpt4 key购买 nike

我是 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)

我已经尝试了所有方法,但无法修复此错误。只有当我让运算符(operator)成为 friend 功能时才会发生这种情况。如果我让他们成为成员,并删除其中一个参数,它就可以正常工作。我做错了什么吗?

最佳答案

你忘记了 cpp 文件中的 ThreeDPoint::before 运算符,并使用二元运算符而不是 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);

编辑:强制使用好友关键字

/* 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++ - Apple Mach-O Linker (Id) 在 C++ 中创建运算符友元函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31091597/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com