c - 对 'addVectors(Vector,Vector)' 的 undefined reference

标签 c codeblocks glut

我无法解决这些“ undefined reference ”问题。我的头文件中可能存在错误。我的 IDE 是 CodeBlocks

#include<windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#include "maths.h"
#include <math.h>

int main(int argc, char *argv[])
{
    //stuff
    struct Vector v1;
    struct Vector v2;

    v1.x = 3;
    v1.y = 4;

    v2.x = 6;
    v2.y = 10;



    struct Vector v3 = addVectors(v1,v2);

    printf("%f %f",v3.x,v3.y);

}

数学.h

#include <math.h>
#include <stdio.h>

#ifndef MATHS_H_INCLUDED
#define MATHS_H_INCLUDED

struct Vector {

    float x;
    float y;

};

    void setVector(struct Vector *v, float x, float y);

    struct Vector addVectors(struct Vector v1, struct Vector v2);

    void makeUnit(Vector *v);

#endif // MATHS_H_INCLUDED
<小时/>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "maths.h"

void setVector(struct Vector *v, float x, float y){

    v->x = x;
    v->y = y;

}

struct Vector addVectors(struct Vector v1, struct Vector v2){

    int x1 = v1.x;
    int x2 = v2.x;
    int y1 = v1.y;
    int y2 = v2.y;

    struct Vector v3;

    v3.x = x1 + x2;
    v3.y = y1 + y2;

    return v3;

}

 void makeUnit(Vector *v){

    double a = (v->x * v->x) + (v->y * v->y);
    double lengthvector = sqrt(a);

    v->x = x/lengthvector;
    v->y = y/lengthvector;

}

我可以编译 struct Vector,但无法编译函数。

||=== Build: |192|undefined reference to `addVectors(Vector, Vector)'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

最佳答案

尝试像这样更改函数:

struct Vector* addVectors(struct Vector v1, struct Vector v2);

并且在这个函数中返回一个Vector的指针

关于c - 对 'addVectors(Vector,Vector)' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56277158/

相关文章:

C - 将 int 反转为数组

C 中 char 到 int 的结构体值

我们可以在代码块中不创建项目的情况下进行调试吗

codeblocks - Code::Blocks 报告另一个正在运行的实例,即使没有

c++ - glutMouseFunc 上没有滚轮回调

c++ - 在游戏 openGL 中更改场景

c++ - 在 OpenGL 中加载纹理时颜色错误

c - 在 C 语言中,为什么数组的地址等于它的值?

c - .NET 开发人员*真的*应该花时间学习 C 以进行低级接触吗?

mysql - CodeBlocks c++ 使用 SQLAPI++ 连接到 MySQL 时出错