1:
在查找预编译头使用时跳过解决
环境:VS2008
微软官方解释:
Visual C++Concepts: Building a C/C++ ProgramCompiler Warning (level 1) C4627Error Message
”: skipped when looking for precompiled header use
While searching for the location where a precompiled header is used, thecompiler encountered an #include directive for the include file. The compilerignores the #include directive, but issues warning C4627 if the precompiledheader does not already contain the include file.
解决方案:
1、去掉预编译头
项目->属性->配置属性->c/c++->预编译头->创建使用预编译头->不使用预编译头
2、将包含文件加到预编译头stdafx.h文件中
3、在每个.cpp中包含预编译头stdafx.h文件
2:
#include “包含文件名” 或 #include <包含文件名>
两种格式的区别仅在于:
①使用双引号:系统首先到当前目录下查找被包含文件,如果没找到,再到系统指定的“包含文件目录”(由用户在配置环境时设置)去查找。
②使用尖括号:直接到系统指定的“包含文件目录”去查找。一般地说,使用双引号比较保险。
多文件结构
大程序倾向于分成多个源文件,其理由为:
(1)避免重复编译函数。
(2)使程序更加容量管理。
(3)把相关函数放到一特定源文件中。
3:具体多文件架构
1, 在每个.cpp文件中都要加入#include "stdafx.h" 才不会出现“在查找预编译头使用时跳过”的错误。或者直接去掉预编译头:项目->属性->配置属性->c/c++->预编译头->创建使用预编译头->不使用预编译头
2, A.cpp文件中要#include “A.h”
在A.cpp文件中如果出现了cout不识别,请加入语句:
#include <iostream>
using namespace std;
这些都是在哪编译错误了,需要用的时候再加。
3, 举例:
node.h:
#defineMAX_LEN 10 #include <string> using namespace std; #ifndefNODE #defineNODE classnode { public: node(); node(stringname); stringgetName() const; void setName(string name); private: stringname; node*ports[MAX_LEN]; }; #endif
node.cpp:
#include "stdafx.h" #include "node.h" #include <iostream> using namespace std; //构造函数 node::node() { this->name="abcd"; for(inti=0;i<MAX_LEN;i++) { ports[i]=NULL; } } //构造函数 node::node(string name) { this->name=name; for(inti=0;i<MAX_LEN;i++) { ports[i]=NULL; } } //成员函数 string node::getName()const { cout<<name.c_str()<<endl; return name; } voidnode::setName(string name) { this->name=name; }
更一般的:
A.h:
/*用ifndef是为了避免其他文件重复#include“A.h”从而是的A.h中变量等被重复声明了。*/ #ifndefA_H #defineA_H classA { int a; public: A(); A(int a); void printA(); }; #endif
A.cpp
/* 在每个.cpp文件中都要加入#include "stdafx.h" 才不会出现“在查找预编译头使用时跳过”的错误。或者直接去掉预编译头:项目->属性->配置属性->c/c++->预编译头->创建使用预编译头->不使用预编译头 */ #include "stdafx.h" /*A.cpp文件中要#include “A.h”*/ #include "A.h" A::A() { a=0; } A::A(inta) { this->a=a; } voidA::printA() { //....cout... }
关于继承:
node.h
#defineMAX_LEN 10 #include <string> using namespace std; #ifndefNODE_H #defineNODE_H classnode { public: node(); node(stringname); stringgetName() const; void setName(string name); private: stringname; node*ports[MAX_LEN]; }; #endif
node.cpp
#include "stdafx.h" #include "node.h" #include <iostream> using namespace std; //构造函数 node::node() { this->name="abcd"; for(inti=0;i<MAX_LEN;i++) { ports[i]=NULL; } } //构造函数 node::node(string name) { this->name=name; for(inti=0;i<MAX_LEN;i++) { ports[i]=NULL; } } //成员函数 string node::getName()const { cout<<name.c_str()<<endl; return name; } voidnode::setName(string name) { this->name=name; }
host继承node
host.h
#include "node.h" #ifndefHOST_H #defineHOST_H classhost:public node { }; #endif
host.cpp
#include "stdafx.h" #include "host.h"
#include "stdafx.h"这个文件主要的作用是用来把公共的头文件诸如#include <iostream>以及其他自定义头文件诸如#include“node.h”包含在其中。这样在每个cpp文件都只需要#include "stdafx.h"就可以了,而无需每个.cpp或.h文件在用到的时候都要包含这些东西了。
//******************************* //* 头文件:StdAfx.h * //* 描 述:包含所有头文件 * //******************************* // 条件编译 #ifndef STDAFX_H #define STDAFX_H // 包含工程所需所有头文件 #include <iostream> #include <stdlib.h> using namespace std; #include "CPoint.h" #include "CLine.h" #include “test.h” #endif STDAFX_H
在我的代码中:
// stdafx.h : 标准系统包含文件的包含文件, // 或是经常使用但不常更改的 // 特定于项目的包含文件 // #pragma once #include "targetver.h" #include <stdio.h> #include <tchar.h> //以下是自己增加的头文件 //1,系统头文件 #include <iostream> #include <string> using namespace std; //2,自定义头文件 #include "A.h" #include "host.h" #include "level1Switch.h" #include "level2Switch.h" #include "level3Switch.h" #include "node.h" #include "Switch.h" // TODO: 在此处引用程序需要的其他头文件