为什么我的#include文件不能编译

Why will my #include file not compile?

本文关键字:不能 编译 文件 #include 我的 为什么      更新时间:2023-10-16

头文件将不会编译到我的主测试程序中。为什么会这样呢?我在网上查了一下,但没有找到简单的理由来解释为什么会这样。我已经尝试了几个#ifndef#define,我仍然不确定为什么该文件不会包含在我的测试程序中。

下面是我在尝试编译测试程序时收到的错误消息。这与头文件有关,但我不确定如何解决这个简单的问题。奇怪的是,我以前用过c++,不记得头文件有这个问题。

错误:

错误1:常量c:usersitpr13266desktopc++testprojecttestprojecttestproject.cpp 10 1 testproject

c:usersitpr13266desktopc++testprojecttestprojecttestproject.cpp 10 1 testproject

Error 3: Cannot open include file: ": No such file or directory c:usersitpr13266desktopc++testprojecttestprojecttestproject.cpp 10 1 testproject

代码
#include "stdafx.h"
#include "iostream"
#include <iostream>
#include <fstream>
#include <math.h>
#include <iostream>
#ifndef MYDATESTRUCTURES_H
#define MYDATESTRUCTURES_H
    #include'myDataStructures.h' <-- name of my include file
#endif
using namespace std;
#define MY_NAME "Alex"
void f(int);
void DoSome(int, char);
enum color  { red, green, blue };
enum color2 { r, g=5, b };
class CVector {
  public:
    int x,y;
    CVector () {}
    CVector (int a, int b) : x(a), y(b) {}
    void printVector()
    {
        std::cout << "X--> " << x << std::endl;
        std::cout << "Y--> " << y << std::endl;
    }
};
CVector operator+ (const CVector& lhs, const CVector& rhs) {
  CVector temp;
  temp.x = lhs.x + rhs.x;
  temp.y = lhs.y + rhs.y;
  return temp;
}
template<typename T>
void f(T s)
{
    std::cout << s << 'n';
}
template<typename P, typename N>
void DoSome(P a, N b)
{
     std::cout << "P--> " << a << 'n';
     std::cout << "N--> " << b << 'n';
}
void testMath()
{
    int result = ceil(2.3) - cos(.2) + sin(8.0) + abs(3.44);
    cos(4.1);
}
void testStorageTypes() 
{
   int a;
   register int b;
   extern int c;
   static int y;
}
color temp = blue;
color2 temp2 = r;
int _tmain(int argc, _TCHAR* argv[])
{
    std::getchar();
    return 0;
}

code(头文件)

#include <iostream>
int myAdd1(int, int);
int myAdd2(int, int, int, int, int);
struct myFirst1
{
}
struct myFirst2
{
}
int myAdd1(int x, int y)
{
    return x + y;
}
int myAdd2(int x, int y, int z, int m, int y)
{
    return x + y;
}

这一行不合法:

#include'myDataStructures.h' <-- name of my include file

在C/c++中,单引号用于引用字符量,而不是字符串量。你需要使用双引号:

#include "myDataStructures.h"

错误消息的帮助比它们可能的要小一些,因为实际上可能有一个多字符常量,但它的值是实现定义的,使得它们的使用不太可移植,因此很少使用。

您需要用双引号("include_filename")或尖括号(<include_filename>)括起文件名,并使用#include语句:

 #include "myDataStructures.h"

使用双引号查找通过编译器的-I(例如GCC)或等效选项(包括发出文件的目录)给出的其他目录,然后返回到尖括号搜索顺序(也查找当前工具链的内在目录)。

关于你的(固定)样本的另一个重要的点:

#ifndef MYDATESTRUCTURES_H
#define MYDATESTRUCTURES_H
    #include "myDataStructures.h"
#endif

这些预处理器条件应该放在你的myDataStructures.h文件中,而不是你包含它的地方:

myDataStructures.h

#ifndef MYDATESTRUCTURES_H
#define MYDATESTRUCTURES_H
#include <iostream>
int myAdd1(int, int);
int myAdd2(int, int, int, int, int);
// ...
#endif

这些包含保护是为了避免在myDataStructures.h文件多次包含来自不同头文件的情况下出现"多个声明/定义"编译器错误。它将防止再次呈现代码,如果它被预处理器看到过一次。

#include "myDataStructures.h"代替#include 'myDataStructures.h'

包含文件时需要使用双引号"MyIncludeFile.h"