我正在嘗試讓一些代碼作業,但無論編譯器如何,我都會收到相同的錯誤。我試圖多載運算子,但出現錯誤。
我有 3 個檔案:main.cpp、vector2d.cpp 和 vector2d.h
這是我用 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)
這是我的 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;
}
這是我的 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;
}
這是我的 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
我真的卡住了...
uj5u.com熱心網友回復:
我的猜測是你錯過了添加vector2d.cpp到你的專案中。添加后(使用您使用的任何構建系統)錯誤應該消失。
錯誤表明編譯器試圖在沒有來自該檔案的符號的情況下鏈接程式。
我無法幫助確切地如何添加它,因為它沒有在專案如何構建的問題中指定,但是如果您只想從終端編譯它可能就像
g main.cpp vector2d.cpp -o program_name
## ^---- this was probably missing
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/337809.html
標籤:C
