简体   繁体   English

一个非常奇怪的C ++行为

[英]A very strange C++ behavior

While I was trying to make my DX11 engine, I had a very strange problem. 当我尝试制造DX11引擎时,我遇到了一个非常奇怪的问题。 I couln't find what was wrong. 我找不到任何问题。 So, I decided to make some simpler code and try to get the same error. 因此,我决定制作一些更简单的代码,并尝试获得相同的错误。 This: 这个:

Source.cpp Source.cpp

#include <iostream>
#include "Header.h"

using namespace std;

struct1 g_struct1(12);
struct2 g_struct2(&g_struct1);

int main()
{
    cout << "Value of g_struct1.num: " << g_struct1.num << endl;
    cout << "Value of g_struct2.copynum: " << g_struct2.copynum << endl;
    getchar();
    return 0;
}

Header.h 标头

#ifndef _HEADER_H_
#define _HEADER_H_

#include "Header1.h"
#include "Header2.h"

#endif

Source1.cpp Source1.cpp

#include "Header1.h"

struct1::struct1(int number)
{
    num = number;
};

Header1.h 头文件1.h

#ifndef _HEADER1_H_
#define _HEADER1_H_

#include "Header1.h"
#include "Header2.h"

struct struct1
{
    struct1(int number);
    int num;
};

#endif

Source2.cpp Source2.cpp

#include "Header2.h"

struct2::struct2(struct1 *par1)
{
    copynum = par1->num;
};

Header2.h Header2.h

#ifndef _HEADER2_H_
#define _HEADER2_H_

#include "Header1.h"
#include "Header2.h"

struct struct2
{
    struct2(struct1 *par1);
    int copynum;
};

#endif

Error 错误

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1>  Source.cpp
1>c:\users\<My Username>\desktop\consoleapplication1\consoleapplication1\header2.h(10): error C2061: syntax error : identifier 'struct1'
1>c:\users\<My Username>\desktop\consoleapplication1\consoleapplication1\source.cpp(8): error C2664: 'struct2::struct2(const struct2 &)' : cannot convert argument 1 from 'struct1 *' to 'const struct2 &'
1>          Reason: cannot convert from 'struct1 *' to 'const struct2'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Of course, the second error doesn't matter because the compiler thinks it's a copy constructor. 当然,第二个错误无关紧要,因为编译器认为它是一个副本构造函数。

If I change struct2::struct2(struct1 *par1) to struct2::struct2(void *par1) and then cast void *par1 to struct1* , it works fine. 如果我将struct2::struct2(struct1 *par1)更改为struct2::struct2(void *par1) ,然后将void *par1struct1* ,则效果很好。

Well, if you made it this far, thanks. 好吧,如果你走了这么远,谢谢。 And sorry for bad English. 对不起,英语不好。

Your headers have circular dependencies. 您的标头具有循环依赖关系。 They even include themself which is silly. 他们甚至包括他们自己,这很愚蠢。 The end result is that neither header gets processed properly. 最终结果是两个标头均未得到正确处理。

In Header1 remove both #include lines. Header1删除两个#include行。 In Header2 remove #include "Header2.h" . Header2删除#include "Header2.h" This should fix the problem. 这应该可以解决问题。

It does not work because during the definition of struct2 the struct1 is not defined and you are using the non-defined struct poniter in your construction. 它不起作用,因为在struct2的定义期间未定义struct1,并且您在构造中使用了未定义的struct poniter。

Simply fix would be 只需解决将是

struct struct2
{
    struct2(struct struct1 *par1); // note the struct struct1 *
    int copynum;
};

Also you don't have to depend on Header1.h in Header2.h (but you do in Source2.cpp) if you do use this. 另外,如果您确实使用了Header2.h,那么您不必依赖Header2.h中的Header1.h(但您必须在Source2.cpp中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM