C ++奇怪的问题,未定义的引用 [英] C++ weird problem, Undefined reference

查看:91
本文介绍了C ++奇怪的问题,未定义的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取错误: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&运算符<< (ostream& valja,Vector2v);

#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 - vx)*(x - vx)+(y - vy)* - vy));
}

ostream&运算符<< (ostream& os,Vector2v){
return os< (<< v.x<<,<< v.y<<);
}


解决方案

C / C ++

似乎在 vector2.cpp neljastest.cpp 您必须更改包含:

  #include../include/Vector2.h

To:

  include../include/vector2.h

我将所有来源粘贴到同一个文件夹成功编译它们:

  g ++ neljastest.cpp vector2.cpp -o neljastest 



编辑



您的问题是, neljastest.cpp依赖于src / vector2.cpp,你不是在Makefile上这样做


getting error: neljastest.cpp: undefined reference to 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 << ")";
}

解决方案

C/C++ are case sensitive for headers too.

It seems that on vector2.cpp and neljastest.cpp you must change the include from:

#include "../include/Vector2.h"

To:

#include "../include/vector2.h"

I pasted all your sources on the same folder and successfully compiled them with:

g++ neljastest.cpp vector2.cpp -o neljastest

Edit:

Your problem is that the linking process of neljastest.cpp depends on src/vector2.cpp, and you are not doing that on the Makefile

这篇关于C ++奇怪的问题,未定义的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆