简体   繁体   English

使用 g++ 编译器的架构 arm64 的未定义符号

[英]Undefined symbols for architecture arm64 using g++ compiler

I'm trying to get some code to work but i keep getting the same error regardless of compiler.我正在尝试让一些代码工作,但无论编译器如何,我都会收到相同的错误。 I'm trying to overload the operators but i get errors.我试图重载运算符,但出现错误。

I have 3 files: main.cpp, vector2d.cpp and vector2d.h我有 3 个文件:main.cpp、vector2d.cpp 和 vector2d.h

This is the error i get with the g++ compiler:这是我用 g++ 编译器得到的错误:

  Undefined symbols for architecture arm64:
  "v2d::length()", referenced from:
      _main in main-c7db92.o
  "v2d::v2d(v2d const&)", referenced from:
      _main in main-c7db92.o
  "v2d::v2d(double, double)", referenced from:
      _main in main-c7db92.o
  "v2d::~v2d()", referenced from:
      _main in main-c7db92.o
  "v2d::operator=(v2d const&)", referenced from:
      _main in main-c7db92.o
  "v2d::operator*(double)", referenced from:
      _main in main-c7db92.o
  "v2d::operator+(v2d const&)", referenced from:
      _main in main-c7db92.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is my main.cpp这是我的 main.cpp

  #include <iostream>
#include "vector2d.h"

using namespace std;

int main() {
    // We crate some vectors with fixed values
    v2d v1(3,0); //(3,0)
    v2d v2(0,4);
    v2d v3(3,2);
    // We create v4 as vector which is like v2
    v2d v4(v2);
    
    cout << "Pythagoras holds on perpendicular triangles (a,b,c):" << endl;
    cout << "a = " << v1.length();
    cout << " , b = " << v2.length();
    
    // We create a new vector v5 by combining other vectors
    // This vector corresponds to the diagonal of the triangle defined by v1 and v2
    v2d v5 = v1 + v2 * (-1);
    
    cout << " , c = " << v5.length() << endl;
    
    cout << "...but not on non-perpendicular triangles (a,b,c):" << endl;
    cout << "a = " << v3.length();
    cout << " , b = " << v4.length();
    
    v5 = v3 + v4 * (-1);
    
    cout << " , c = " << v5.length() << endl;
    
  
    
    return 0;
}

Here is my vector2d.cpp这是我的 vector2d.cpp

    #include "vector2d.h"
#include <cmath>

v2d::v2d(double a, double b) {
    // Write your code here
}

v2d::v2d(const v2d & v) {
    // Write your code here
}

v2d::~v2d() {
    // Write your code here
}

v2d & v2d::operator=(const v2d &v) {
    // Write your code here
    return *this;
}

v2d & v2d::operator+(const v2d &v) {
    // Write your code here
    return *this;
}

double v2d::operator*(const v2d &v) {
    // Write your code here
    return 0;
}

v2d & v2d::operator*(double k) {
    // Write your code here
    return *this;
}

double v2d::length() {
    // Write your code here
    return 0;
}

Here is my vector2d.h这是我的 vector2d.h

#ifndef __v2d__
#define __v2d__

class v2d {

public:
    // Standard constructor: builds a vector (a,b)
    v2d(double a, double b);

    // Copy constructor: builds a vector that is exactly as v
    v2d(const v2d & v);

    // Destructor
    ~v2d(void);

    // Assignment operator: updates the vector to make it as v
    v2d & operator=(const v2d &v);

    // Vector addition: updates the vector by adding v
    v2d & operator+(const v2d &v);

    // Scalar multiplication: updates the vector by scaling by k
    v2d & operator*(double k);

    // Scalar product of the current vector by another vector v 
    double operator*(const v2d &v);

    // Returns the length of a vector
    double length(void);

private:
    // Internal representation of a vector with just two doubles x and y
    double x;
    double y;

};

#endif

I am really stuck...我真的卡住了...

My guess is that you have missed to add vector2d.cpp to your project.我的猜测是您没有将vector2d.cpp添加到您的项目中。 After adding that (with whatever build system you use) the errors should go away.添加后(使用您使用的任何构建系统)错误应该消失。

The errors indicate that the compiler tries to link the program without the symbols from that file present.错误表明编译器试图在没有该文件中的符号的情况下链接程序。

I cannot help with exactly how to add it since it is not specified in the question how the project is built, but if you would like to just compile from terminal it could be as simple as我无法帮助确切地如何添加它,因为它没有在项目如何构建的问题中指定,但是如果您只想从终端编译它可能就像

g++ main.cpp vector2d.cpp -o program_name
##              ^---- this was probably missing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM