0

I have a big class A that has dozens included headers, each header has its own included header as well.

I'm creating a new class that would use a function which is also used in class A. I do not want to include the whole class A in my new class, so I try to find the header who brought that function to class A.

What's the best way to do it?

LunaticJape
  • 1,446
  • 4
  • 21
  • 39
  • 4
    Are you using an IDE? Most have an option to show where a function is declared/defined. – NathanOliver Oct 12 '22 at 21:13
  • Did your check your compiler's documentation to see how to get the preprocessed output, which you can then search for the function's declaration, and then scroll up and see which header file it is in? – Sam Varshavchik Oct 12 '22 at 21:14
  • Related: [How do I find declarations/definitions without an IDE?](https://stackoverflow.com/q/43004338) [How can I quickly search all included header files in a project for a specific symbol?](https://stackoverflow.com/q/9888245) [How to determine which include header file a function comes from?](https://stackoverflow.com/q/21064039) – SuperStormer Oct 12 '22 at 21:15
  • 2
    The best way is *to look it up in the documentation*. A function may be defined in a header you are not supposed to include directly. Doing it anyway will cause all kinds of pain. – n. m. could be an AI Oct 12 '22 at 21:19

1 Answers1

3

If you are not using an IDE or an appropriate editor plugin (you should), then the easiest way is to add a deliberate error to a file and look at the error message. Note, this may or may not work with your compiler.

int foo(); // defined somewhere but we don't know where

// ask the compiler
foo(42);

Error messages:

test.cpp:42:8: error: too many arguments to function ‘int foo()’
   42 |     foo(42);
      |     ~~~^~~~
foo.h:38:5: note: declared here
   38 | int foo();
      |      ^~~

You should not blindly #include <foo.h> if it comes from a third party library. It might be a file that end users are not supposed to include directly. Double check.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243