ld:找不到体系结构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看调用) [英] ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

查看:74
本文介绍了ld:找不到体系结构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在c ++中做一个自定义的向量"类,作为我大学的作业,但是我在模板上苦苦挣扎.

I have to do a custom "vector" class in c++ as homework for my university but I'm struggling with the templates.

将所有变量的类型更改为类型模板"typename T"后,出现此错误.事实是,编译器仅指向声明为朋友"函数的函数.从编译器的消息中可以看到(即运算符'=='和'<<'):

I get this error after I change all the variables' types to the type template 'typename T'. The thing is that the compiler only points at the functions that are declared as "friend" functions. Namely (operators '==' and '<<') as seen from compiler's message:

体系结构x86_64的未定义符号:运算符==(向量const& ;,向量const&)",引用自:_main在main.o中运算符<(std :: __ 1 :: basic_ostream>& Vector)",引用自:_main在main.o中ld:找不到架构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

Undefined symbols for architecture x86_64: "operator==(Vector const&, Vector const&)", referenced from: _main in main.o "operator<<(std::__1::basic_ostream >&, Vector)", 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运算符函数及其实现的声明.

Here's the declaration of these two friend operator functions in the header file and their implementation.

friend ostream& operator <<(ostream& out, Vector<T> x);
friend bool operator == (const Vector<T> &lop, const Vector<T> &rop);

template <typename T>
 bool operator == (const Vector<T> &lop, const Vector<T> &rop){
    if(lop.size() != rop.size()){
 return false;
}
else{
    int counter = 0;
    for(int i = 0; i < lop.size(); i++){
        for(int j = 0; j < rop.size(); j++){
            if(lop.values[i] == rop.values[j]){
                counter++;
            }
        }
if((counter == lop.size()) && (counter == rop.size())){
            return true;
        }
    }
}
return false;

}




template <typename T>
ostream& operator <<(ostream& out, Vector<T> x)
{
out << "[";
for(int i = 0; i < x.size(); i++){
    out << x.values[i];
    if(i + 1 != x.size()){
        out << ", ";
    }
}
out << "]";
return out;
}

在main()函数中,我刚刚测试了这两个运算符:

In the main() function I just tested these two operators:

#include <iostream>
#include "vector.hpp"
#include "vector.cpp"
using namespace std;

int main (){

    Vector<double> second(10);
    Vector<double> third {1.0, 2.0, 3.0, 4.0, 5.0};
    cout << third << endl;

    Vector<double> v(10);
    Vector<double> k(10);

    if(k == v){
    cout << "YES" << endl;
}
    else{
    cout << "NO" << endl;
}

    return 0;
}

我不明白为什么会收到此错误,因此,非常感谢您的帮助!

I don't understand why I get this error so I would really appreciate your help!

推荐答案

您应该在 Vector类的 friend 声明中添加 template< typename T> .

template <typename T>
friend ostream& operator <<(ostream& out, Vector<T> x);

template <typename T>
friend bool operator == (const Vector<T> &lop, const Vector<T> &rop);

这篇关于ld:找不到体系结构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆