简体   繁体   English

如何在Visual Studio中包含头文件?

[英]How to include a header file in Visual Studio?

I have a problem in VS2012, where I want to include a header file: 我在VS2012中遇到问题,我想在其中包含头文件:

#include <FileReader>

The problem is that I want to read a file and store it into a matrix. 问题是我想读取文件并将其存储到矩阵中。 However, the include directive fails so that I am unable to read it: 但是,include指令失败,因此我无法阅读它:

#include <mpi.h>
#include <iostream>
#include <FileReader>

int main(int argc, char* argv[]) 
{
int rows, cols;
float a[10];

//std::vector<float> vec;

FileReader fr("./SampleData.txt");

fr.getSize(rows, cols);

fr.getVector(a);

for (int i = 0; i < cols; i++)
    fr.getNextRow(a);

int i;

std::cin >> i;

return 0;
};

Could you please explain why this program does not compile? 您能否解释为什么该程序无法编译?

The answer depends on what FileReader refers to. 答案取决于FileReader指的是什么。

Assuming that it refers to a header file for a class you designed yourself: 假设它引用了您自己设计的类的头文件:

  1. Your should rename this header file into FileReader.h , as names without extension are by convention reserved to the compiler libraries; 您应该将此头文件重命名为FileReader.h ,因为按惯例,不带扩展名的名称保留给编译器库;
  2. You should then enclose the header file name between double-quotes rather than angular brackets, as angular brackets are to be used for compiler libraries, or possibly for third-party libraries used in your project ( boost ...) 然后,您应该将标头文件名放在双引号之间,而不是用尖括号括起来,因为尖括号将用于编译器库,或者可能用于项目中使用的第三方库( boost ...)

Thus, once FileReader is renamed into FileReader.h , the include directive should looks as shown below: 因此,一旦将FileReader重命名为FileReader.h ,则include指令应如下所示:

`#include "FileReader.h"`

UPDATE 更新

The additional errors you encounter are *linker errors'. 您遇到的其他错误是*“链接器错误”。 They mean that: 他们的意思是:

  • Compilation of your source files works fine; 您的源文件的编译工作正常;
  • However, some methods are declared in FileReader.h but no compiled code can be found for these methods. 但是,某些方法在FileReader.h中声明,但是找不到这些方法的编译代码。

The most likely explanation is that the source code for these methods is not included in the project. 最可能的解释是这些方法的源代码未包含在项目中。 Thus, you must add this source code to your VS project. 因此,您必须将此源代码添加到VS项目中。 It is probably a file named FileReader.c or FileReader.cpp . 它可能是一个名为FileReader.cFileReader.cpp的文件。

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

相关问题 Visual Studio不能包含头文件。 - Visual Studio cannot include header file. 如何在Visual Studio 2008中包含头文件? - How to include header files in Visual Studio 2008? 不能在visual studio 2008中包含std头文件 - Cannot include std header file in visual studio 2008 Visual Studio代码:C / C ++:显示的标头/包含文件的多个定义; 怎么修? - Visual Studio Code: C/C++: Multiple definitions for a header/include file shown; how to fix? 如何在Visual Studio中的所有cpp文件中自动包含标头? - How to include an header automatically in all cpp files in visual studio? Visual Studio不包含标题 - Visual Studio doesn't include header Visual Studio 2013标头包含失败 - Visual Studio 2013 header include failed Visual Studio强制在项目的所有编译单元中包含预编译的头文件? - Visual studio forces to include precompiled header file in all compilation units of the project? 如何#include<torch.extension.h> 在 Visual Studio 的 .cpp 文件中? - how to #include<torch.extension.h> in a .cpp file with Visual Studio? 我的项目的属性配置正确,但visual studio仍然无法打开包含头文件 - the property of the my project is configured right , but visual studio still can not open include header file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM