【问题标题】:C++ how to properly use #include?C++如何正确使用#include?
【发布时间】:2014-01-28 14:02:07
【问题描述】:

我想在我的 main.cpp 程序中包含 printstuff.h。我得到没有这样的文件或目录错误。我不想把整个目录用双引号引起来。我只想简单地把 printstuff.h 我怎么做到这一点?

我正在使用 Visual Studio 2012。

main.cpp

#include<iostream>
#include<cstdlib>
#include<printstuff.h>
using namespace std;

inline void swap( int *x, int *y ) {
    int *z = x;
    *x = *y;
    *y = *z;
}

int main( ) {
    /*int x = 0, y = 1;
    swap( x, y );
    cout << x << endl << y << endl;*/

    printStuff( );

    system( "pause" );

    return 0;
}

printstuff.h

#include<iostream>
using namespace std;

void printStuff( );

void printStuff( ) {
    int count[ ] = { 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
    char symb[ ] = "abcdefghijk";

    for ( int j = 1; j < 12; j++ ) {
        char c = symb[ j ];

        for ( int i = 0; i < 11; i++ ) {
            int times = count[ i ];

            while ( times != 0 ) {
                cout << c;
                times--;
            }

            cout << endl;
        }
    }
}

【问题讨论】:

  • 在你的项目设置中添加头文件的路径
  • #include "printstuff.h"

标签: c++ header active-directory include


【解决方案1】:

在项目的属性页中,将“printstuff.h”的路径放在“附加包含目录”中。这样就可以通过#include &lt;printstuff.h&gt;使用头文件了

假设“some.h”文件路径在“C:\ref\include”中,而您的项目路径在“C:\Projects”中。

  • 如果将'C:\ref\include'的路径放在'Additional Include Directories'中,可以使用#include &lt;some.h&gt;
  • 如果不放路径,则必须使用相对路径,如#include "..\ref\include\some.h"

【讨论】:

    【解决方案2】:

    您可以通过告诉 Visual Studio 在哪里搜索包含指令来完成此操作。

    从解决方案资源管理器中右键单击项目并选择属性。

    在属性中编辑配置属性> 包含指令。添加放置头文件的目录。

    查看快照。 Image Showing Include Directive Customization Option

    【讨论】:

      【解决方案3】:

      //printstuff.h

      #ifndef ADD_H_INCLUDED
      #define ADD_H_INCLUDED
      
      void printStuff( );
      #endif
      

      //printstuff.cpp

      #include<printstuff.h>
      
      void printStuff( ) {
      int count[ ] = { 1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
      char symb[ ] = "abcdefghijk";
      
      for ( int j = 1; j < 12; j++ ) {
          char c = symb[ j ];
      
          for ( int i = 0; i < 11; i++ ) {
              int times = count[ i ];
      
              while ( times != 0 ) {
                  cout << c;
                  times--;
              }
      
              cout << endl;
          }
      }
      }
      

      //main.cpp

      #include <iostream>
      #include <cstdlib>
      #include <printstuff.h>
      using namespace std;
      
      inline void swap( int *x, int *y ) {
      int *z = x;
      *x = *y;
      *y = *z;
      }
      
      int main( ) {
      /*int x = 0, y = 1;
      swap( x, y );
      cout << x << endl << y << endl;*/
      
      printStuff( );
      
      system("PAUSE");
      
      return 0;
      }
      

      【讨论】:

        【解决方案4】:

        如果您只想添加您的 printstuff.h,请将您的目录添加到项目的包含属性中: 右键单击您的项目 -> 属性 -> C/C++ -> 常规 -> 附加包含目录

        然后添加您的目录并#include 您的标题;)

        【讨论】:

          猜你喜欢
          • 2015-01-25
          • 2010-10-02
          • 2013-10-13
          • 2016-10-01
          • 1970-01-01
          • 2021-12-13
          • 2020-11-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多