CPP 项目中包含的自定义 C typedef 结构出现错误; LNK2019 [重复]

问题描述 投票:0回答:1

我的一个朋友正在一些非常旧的硬件上开发游戏,他们没有接受过程序员的培训,所以我这样做是为了帮助他们向他们的这个资源有限的项目介绍一些软件工程原理和抽象。问题是,C# 是我使用过的唯一 C 的衍生版本,而且我目前主要学习 Java、C# 和 Python,因此正在学习 C++。

我正在尝试通过利用具有精确命名约定的结构和方法,在 C 中(例如......原始 C)对一些 OOP 原则进行建模。我觉得这最终是必要的,因为他们需要一个碰撞检测系统,并且我希望将来可能为他们建立八叉树模型。但那是更进一步的事情,因为我需要三角形,而点则需要。

我已经对点进行了建模,因为除了一些创建/构造函数方法之外,它们不需要任何行为,但我定义结构的方式或将其链接到主函数的方式似乎有问题文件。

/**
 * @title XYZ_POINT_H
 *
 * Implementation of a three dimensional point using struct
 *     as a facsimile of a class.
 */

#ifndef XYZ_POINT_H
#define XYZ_POINT_H

typedef struct XYZ_POINT {
    // variables
    double x;
    double y;
    double z;
} XYZ_POINT;

// functions
extern XYZ_POINT xyz_point__create(void);
extern XYZ_POINT xyz_point__copy(const XYZ_POINT xyz_point);
extern XYZ_POINT xyz_point__create_from_doubles(const double x, const double y, const double z);

#endif
/**
 * @title XYZ_POINT_C
 */

#include "XyzPoint.h"

// functions
XYZ_POINT xyz_point__create(void) {
    XYZ_POINT new_xyz_point;
    new_xyz_point.x = 0.0;
    new_xyz_point.y = 0.0;
    new_xyz_point.z = 0.0;
    return new_xyz_point;
}

XYZ_POINT xyz_point__create_from_doubles(const double x, const double y, const double z) {
    XYZ_POINT new_xyz_point;
    new_xyz_point.x = x;
    new_xyz_point.y = y;
    new_xyz_point.z = z;
    return new_xyz_point;
}

XYZ_POINT xyz_point__copy(const XYZ_POINT xyz_point) {
    XYZ_POINT new_xyz_point;
    new_xyz_point.x = xyz_point.x;
    new_xyz_point.y = xyz_point.y;
    new_xyz_point.z = xyz_point.z;
    return new_xyz_point;
}
/**
 * @title MAIN_CPP
 */

#include "XyzPoint.h"

/**
 * MAIN DOCUMENTATION
 */
int main() {
    XYZ_POINT point1 = xyz_point__create_from_doubles(0.0, 1.1, 7.4);
    XYZ_POINT point2 = xyz_point__create_from_doubles(5.3, -7.3, 0.5);
    XYZ_POINT point3 = xyz_point__create_from_doubles(-6.7, 6.7, -16.6);
    XYZ_POINT point4 = xyz_point__create_from_doubles(-8.1, 12.2, 0.8);

    return 0;
}

运行/构建项目会导致“LNK2019”错误:

错误 LNK2019 无法解析的外部符号“struct XYZ_POINT __cdecl xyz_point__create_from_doubles(double,double,double)”(?xyz_point__create_from_doubles@@YA?AUXYZ_POINT@@NNN@Z) 在函数 main 中引用

您能提供的任何建议将不胜感激。

c++ c struct linker-errors
1个回答
0
投票

在标题中添加

extern "C"

// XYZ_POINT_H

#ifdef __cplusplus
extern  "C" {
#endif
// Your C declarations:

#ifdef __cplusplus
} // extern  "C"
#endif
© www.soinside.com 2019 - 2024. All rights reserved.