gpt4 book ai didi

c++ - 不同翻译单元中的多个定义

转载 作者:搜寻专家 更新时间:2023-10-31 00:39:19 31 4
gpt4 key购买 nike

我知道对于一个定义规则,如果它们具有相同顺序的相同标记,我可以在多个翻译单元中定义一个类,但是这个程序对我来说很奇怪

文件ma​​in.cpp

#include "Source.h"

struct mystructure
{
int field1;
float field2;
};

int main()
{

mystructure myvar;
myvar.field2= 2.0f;

myCustomFunction(myvar);

return 0;
}

文件Source.h

struct mystructure;

void myCustomFunction(mystructure& vv);

文件 Source.cpp

#include "Source.h"

struct mystructure
{
char otherfield;
int anotherone;
bool anotheranotherone;
};

void myCustomFunction(mystructure& vv)
{
vv.otherfield = 'A';
}

我使用的是 MSVC2012,编译器没有报错。这是为什么?这正常吗?

最佳答案

编译器很难提示,因为它在同一编译过程中从未看到冲突的声明。但是这段代码是不正确的,不幸的是编译器不会发现每一个错误,标准也没有要求他们这样做。

关于c++ - 不同翻译单元中的多个定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16376668/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com