使用结构作为库的全局

Use struct as a global for libraries

我正在制作自己的库,但遇到了问题。当我在 .h 文件中预先声明一个函数时,无法识别它,因为它是合乎逻辑的。我不知道该怎么做。

在 Vector2Lib.cpp 我有一个结构:

struct Vector2{
   float x; 
   float y; 
};

还有一个函数,它等于一个向量:

    Vector2 sumaVector(Vector2 x, Vector2 y) {
       Vector2 vectorSumado; 

       vectorSumado.x = x.x + y.x;
       vectorSumado.y = x.y + y.y;

       return vectorSumado;
    }

当我在 .h 文件中预先声明时:

Vector2 sumaVector(Vector2 x, Vector2 y);

它无法识别 Vector2 结构。我该怎么办?

抱歉我的英语不好,我提前道歉。谢谢

struct 的定义也放在 ​​.h 文件中。

.h 文件应如下所示:

#pragma once

// Define the struct.
struct Vector2{
   float x; 
   float y; 
};

// Declare the function.
Vector2 sumaVector(Vector2 x, Vector2 y);

您可以通过两种方式解决您的问题,您可以编写

typedef struct {
                float x; 
                float y; }Vector2;

int main(){

    Vector2 tmpVec;
    tmpVec.x=10.0;
    tmpVec.y=30.0;
    //// 
 }

或者您可以像这样编写代码示例

struct Vector2{
     float x;     
     float x;   
};
int main(){

    stuct Vector2 tmpVec;
    tmpVec.x=10.0;
    tmpVec.y=30.0;
    //// 
 }