Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在c+中访问c-struct+;_C++_C_Struct - Fatal编程技术网

C++ 在c+中访问c-struct+;

C++ 在c+中访问c-struct+;,c++,c,struct,C++,C,Struct,我需要访问C++文件中的头文件中定义的结构的内容。 头文件struct.h当前具有以下格式: typedef struct { int x; int y; } structTypeName; extern structTypeName structName; 在文件A.c #include "struct.h" structTypeName structName; int main(){ structName.x = xyz; structName.y

我需要访问C++文件中的头文件中定义的结构的内容。

头文件
struct.h
当前具有以下格式:

typedef struct {
    int x;
    int y;
} structTypeName;

extern structTypeName structName;
在文件
A.c

#include "struct.h"

structTypeName structName;

int main(){

    structName.x = xyz;
    structName.y = zyx;
}

现在我需要访问C++文件中的Stutt,代码> B.cpp < /C>(是的,它必须是C++)。我尝试了很多不同的想法,但到目前为止都没有成功。有人能告诉我如何实现这一点吗

编辑:

< > C++文件遵循ATM:< /P>
#include <iostream>

extern "C"{
    #include "struct.h"
}

int main(){

  while(true){

    std::cout << structName.x << "\n";

  }
}
#包括
外部“C”{
#包括“struct.h”
}
int main(){
while(true){

std::cout我看不出您遇到的真正问题。但是为了使示例起作用,我做了以下更改

在A.c

#包括“struct.h”
structTypeName structName;
int c_main(void){//新名称,因为我们不能有两个“main”函数:-)
structName.x=123;//设置一些已定义的值
structName.y=321;
}
在B.cpp中:

#包括
#include“struct.h”//这声明没有函数,因此不需要外部“C”
外部“C”int
c_main(void);//将c-main重命名为c_main,并在此处声明它
int main(){
c_main();//调用重命名的c-main来初始化structName。
//我们走了!

虽然(正确的)std::不可能重复您所需要的全部内容,但您可能需要在
struct.h
文件中添加
extern“C”{…}
,或者,如果您周围不可能这样做,则包括“struct.h”
我已经通过
extern“C”{
。我仍然无法访问变量的值。在我的想象中,像
var=structName.x
这样的东西应该足够了this@KBS请添加一个C++ C++代码的例子,当你查看源代码时,我会更容易识别问题。<代码>主<代码>函数应在C++代码中。