简体   繁体   English

项目中无法识别的CodeBlocks文件

[英]CodeBlocks files not recognized in project

I've been using code blocks for a long time, but never really made my programs into actual code blocks projects. 我已经使用代码块很长时间了,但是从未真正将我的程序变成实际的代码块项目。 I tried to do it today, and I kept getting errors due to code blocks not recognizing my files. 我今天尝试这样做,但由于代码块无法识别我的文件,我不断出现错误。 Here is what I have : ----> CodeBlocks Include Error 这是我所拥有的:----> CodeBlocks包含错误

When I try to buiild my project I get that cout,cin and my class objects are not defined in my menu.cpp file. 当我尝试建立我的项目时,我得到了cout,cin和我的menu.cpp文件中未定义类对象的信息。 So I can only guess code blocks is not properly handling the files. 因此,我只能猜测代码块无法正确处理文件。

I would love if someone could help me out as to why this is happening. 如果有人可以帮助我解决此问题,我将非常乐意。

Thanks a ton in advance :) 在此先感谢一吨:)

When I try to buiild my project I get that cout,cin and my class objects are not defined in my menu.cpp file. 当我尝试建立我的项目时,我得到了cout,cin和我的menu.cpp文件中未定义类对象的信息。

That's because they're not. 那是因为他们不是。 You #include d neither iostream nor class.h in menu.cpp , so you can't access the declarations therein. #include Ð既不iostream也不class.hmenu.cpp ,所以你不能访问其中的声明。

Note that Code Blocks (just like any properly set up build tools) will compile each cpp file separately. 请注意,代码块(就像任何正确设置的构建工具一样)将分别编译每个cpp文件。 This means that not only will it compile menu.cpp as part of the compilation of main.cpp (because you include it), it will also compile it on its own. 这意味着它不仅将menu.cpp作为main.cpp的一部分进行编译(因为您将其包含在内),而且还将自行编译。 In the latter case the includes from main.cpp will not be available, so menu.cpp needs its own includes. 在后一种情况下,main.cpp中的include将不可用,因此menu.cpp需要它自己的include。

This also means that once it does compile (ie once you added the includes), you'll get a linker error because the definitions from menu.cpp are now defined twice (once in main.o -- because you included menu.cpp in main.cpp -- and once in menu.o). 这也意味着一旦编译完成(即,一旦添加了includes),就会收到链接器错误,因为menu.cpp的定义现在被定义了两次(一次在main.o中-因为您在menu.cpp中包括了main.cpp-并进入menu.o)。 That's the reason why you should never include cpp files into each other. 这就是为什么您不应该相互包含cpp文件的原因。

PS: This is unrelated to your problem, but it's considered bad practice to use using namespace in a header file. PS:这与您的问题无关,但是在头文件中using namespace被认为是不好的做法。 You should put that in your cpp files instead (if you want to use it at all). 您应该将其放在cpp文件中(如果要使用它)。 You should also put the #include <iostream> in those files where you actually need it, rather than the header file. 您还应该将#include <iostream>放在实际需要的文件中,而不是头文件中。

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

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