得到错误:neljastest.cpp:对Vector2 :: Vector2的未定义引用(float,float)
neljastest.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;
}
vector2.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
vector2.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 << ")";
}
答案 0 :(得分:4)
C / C ++ 对标题也区分大小写。
似乎在 vector2.cpp 和 neljastest.cpp 上,您必须更改以下内容:
#include "../include/Vector2.h"
要:
#include "../include/vector2.h"
我将所有来源粘贴到同一个文件夹中,然后使用以下代码成功编译它们:
g++ neljastest.cpp vector2.cpp -o neljastest
修改强>:
您的问题是neljastest.cpp的链接过程依赖于src / vector2.cpp,而您在Makefile上没有这样做