我的一部分是为一个非常古老的游戏(沃尔芬斯坦-3D)编写代码。它需要使用Borland C++ v3.1编译器。这是我目前的代码,但在Borland编译器中出现了一个错误。有什么想法吗?
编译器中的错误:

Neuron.h
#ifdef __cplusplus // only actually define the class if this is C++
class Neuron {
public:
void foo();
int bar(int x, int y);
};
#else // C doesn't know about classes, just say it's a struct
typedef struct Neuron Neuron;
#endif
// access functions
#ifdef __cplusplus
#define EXPORT_C extern "C"
#else
#define EXPORT_C
#endif
EXPORT_C Neuron* NeuronNew(void);
EXPORT_C void NeuronDelete(Neuron* n);
EXPORT_C void NeuronFoo(Neuron* n);
EXPORT_C int NeuronBar(Neuron* n, int x, int y);Neuron.cpp
#include "NEURON.h"
void Neuron::foo() {
}
int Neuron::bar(int x, int y) {
return x+y;
}
EXPORT_C Neuron* NeuronNew(void) {
return new Neuron();
}
EXPORT_C void NeuronDelete(Neuron* n) {
delete n;
}
EXPORT_C void NeuronFoo(Neuron* n) {
return n->foo();
}
EXPORT_C int NeuronBar(Neuron* n, int x, int y) {
return n->bar(x, y);
}C源文件中的用法
#include "NEURON.h"
...
void GameLoop (void)
{
...
Neuron* m = NeuronNew();
NeuronFoo(m);
NeuronDelete(m);
...
}我的假设是,即使编译器是C++编译器,C++代码中也有一些‘新’的东西,编译器无法处理。
发布于 2016-11-13 11:28:07
当其他编译器无法为您的类构建.cpp文件时,错误消息看起来非常类似于您从其他编译器获得的错误消息。它不是抱怨NeuronNew,而是抱怨_Neuron_new (注意小写'n‘和额外的下划线),所以这很可能是Borland命名的构造函数/析构函数吗?
它成功地编译了.cpp和as C++吗?文件后缀映射是否与编译器中的文件后缀挂钩?您是否在#ifdef __cplusplus行中添加了无效代码以验证是否已定义,而不总是定义(0或1)?您是否在Makefile/项目中对所有包含、文件名和项目使用相同的大小写,以便能够找到它们?
哦,你试过做:
typedef struct Neuron* NeuronPtr;然后在C包装器中使用NeuronPtr而不是Neuron*?C++编译器不应该关心(只要在__cplusplus部分执行typedef class Neuron* NeuronPtr; ),但这意味着它可能不再试图在任何C代码中解析前向声明的结构。
https://stackoverflow.com/questions/40564504
复制相似问题