简体   繁体   中英

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

This is the error i get with the g++ compiler:

  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

  #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

    #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

#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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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