Why does omission of “#include ” only sometimes cause compilation failures?

后端 未结 7 2327
清歌不尽
清歌不尽 2020-11-22 14:23

I am a beginner with C++. When I write the code sometimes I write #include and the code works, other times I don\'t write #include

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 15:06

    If you're just using a pointer/reference to a user defined type, the type only needs to be declared:

    class my_class;
    void foo(const my_class& c);
    

    But when you're using the value, the compiler needs to know the size and with that the definition of the type.

    And keep in mind that standard headers may include other ones, which doesn't automaticly mean that all implementations do that, so you can't rely on that.

提交回复
热议问题