简体   繁体   中英

Including files in C++

I am trying to include my class header in main and my class implementation. The weird thing is main finds the header file perfectly, but with the class implementation I get "no such file or directory". Why is this happenning? I mean what are the possible reasons one file in my project is able to find a header file perfectly, but another file in the same project can't find the same identical header file.

I am using CodeBlocks 10.05

Thanks

The list of paths searched by the compiler for headers must be different when compiling the class implementation.

Are the class implementation and main files in the same directory? If not then the problem could be that on most compilers the first path searched is the directory containing the file being compiled*, and the header is in the same file as the main file, but not the class implementation file.

I don't know if your compiler offers it, but some have a verbose mode that will list the paths being search for headers. If my above comment doesn't help then maybe that will.

  • Assuming you're including the file using quotes. The only difference between using quotes and angle brackets in your includes is that using quotes means the directory containing the .cpp file is searched for headers before all the other paths that have been listed (on the command line, in environment variables, in the implicate system include paths).

Make sure your directories are configured right, and as long as the implementation is included in the project it should compile properly. Also as Fred Larson said, make sure you put "quotes" around the file, not angled brackets, otherwise it looks in the standard directories where it won't find your file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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