为什么我的#include文件不能编译
Why will my #include file not compile?
头文件将不会编译到我的主测试程序中。为什么会这样呢?我在网上查了一下,但没有找到简单的理由来解释为什么会这样。我已经尝试了几个#ifndef
和#define
,我仍然不确定为什么该文件不会包含在我的测试程序中。
下面是我在尝试编译测试程序时收到的错误消息。这与头文件有关,但我不确定如何解决这个简单的问题。奇怪的是,我以前用过c++,不记得头文件有这个问题。
错误:代码错误1:常量c:usersitpr13266desktopc++testprojecttestprojecttestproject.cpp 10 1 testproject
c:usersitpr13266desktopc++testprojecttestprojecttestproject.cpp 10 1 testprojectError 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"
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么使用 std::vector 的代码不能编译,但使用 std::unique_ptr 如果没有 noexcept
- 为什么我不能编译这个在Cygwin的Visual Studio中编写的C++文件?
- 为什么我不能编译这个简单的线程测试?
- 为什么 --string::end() 可以编译,而 --string.size() 不能编译?
- 为什么G 5.4不能编译此编译时质数代码
- 为什么最简单的模板模板不能编译
- 为什么这个 c++ 代码不能编译?
- 为什么我不能编译这个开源项目
- 为什么 g++ 不能编译一个简单的 GTK 程序?
- C++。为什么我不能编译此代码?使用const_cast去除恒常性有什么问题?
- 为什么这个模板函数不能编译C++
- 托管类型的成员函数不能编译为非托管函数
- 为什么这个简单的C++程序不能编译?
- 为什么这个C++代码不能编译
- 为什么没有enable_if就不能编译
- 使用重载的operator=不能编译
- CodeRunner——不能编译多个源c++程序
- 为什么[]可变{}不能编译
- 为什么自建的g++编译器不能编译我的代码