出现错误:neljastest.cpp:对 Vector2::Vector2(float, float) 的未定义引用
内尔贾斯特.cpp:
#include <cstdlib>
#include <iostream>
#include "../include/Vector2.h"
#include "../include/neljas.h"
using namespace std;
int main (int argc, char* argv[]) {
Vector2 p1 (1.0, 2.0);
Vector2 p2 (0.0, 0.0);
Vector2 p3 (5.0, 2.0);
return EXIT_SUCCESS;
}
矢量2.h:
#ifndef VECTOR2_H
#define VECTOR2_H
#include <iostream>
using std::ostream;
class Vector2 {
public:
float x;
float y;
Vector2();
Vector2(float nx, float ny);
float distanceFrom(Vector2 v);
};
ostream& operator << (ostream& valja, Vector2 v);
#endif
矢量2.cpp:
#include "../include/Vector2.h"
#include <cmath>
using namespace std;
Vector2::Vector2() {
x = 0;
y = 0;
}
Vector2::Vector2(float nx, float ny) {
x = nx;
y = ny;
}
float Vector2::distanceFrom(Vector2 v) {
return sqrt( (x - v.x)*(x - v.x) + (y - v.y)*(y - v.y) );
}
ostream& operator << (ostream& os, Vector2 v) {
return os << "(" << v.x << "," << v.y << ")";
}
C/C++标题也区分大小写。
似乎在矢量2.cpp and 内尔贾斯特测试.cpp您必须将包含更改为:
#include "../include/Vector2.h"
To:
#include "../include/vector2.h"
我将所有源代码粘贴到同一个文件夹中,并使用以下命令成功编译它们:
g++ neljastest.cpp vector2.cpp -o neljastest
Edit:
您的问题是 neljastest.cpp 的链接过程依赖于 src/vector2.cpp,并且您没有在 Makefile 上执行此操作
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)