为什么我的#include文件无法编译?

时间:2014-03-25 19:51:33

标签: c++

头文件不会编译到我的主测试程序中。为什么会这样呢?我已经上线,但没有找到简单的理由为什么会出现这种情况。我尝试了几个#ifndef#define,但我仍然不确定该文件为什么不包含在我的测试程序中。

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

错误:

  

错误1错误C2015:常量c:\ users \ itpr13266 \ desktop \ c ++ \ testproject \ testproject \ testproject.cpp中的字符太多10 1 TestProject

     

错误2错误C2006:' #include' :期望一个文件名,找到'常数' c:\ users \ itpr13266 \ desktop \ c ++ \ testproject \ testproject \ testproject.cpp 10 1 TestProject

     

错误3错误C1083:无法打开包含文件:'':没有这样的文件或目录c:\ users \ itpr13266 \ desktop \ c ++ \ testproject \ testproject \ testproject.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;
}

代码(头文件)

#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;
}

4 个答案:

答案 0 :(得分:8)

此行无效:

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

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

#include "myDataStructures.h"

错误信息的效果稍差,因为它实际上可能有一个multi-character constant, but its value is implementation-defined,使得它们的使用不是很便携,因此很少见。

答案 1 :(得分:2)

您需要使用双引号括起来的文件名("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文件,则这些包含警卫旨在避免“多个声明/定义”编译器错误。如果预处理器一次看到它,它将阻止再次呈现代码。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

后者导致语法错误。