对于编译还是没弄明白,尤其是多个头文件之间的相互包含。
  比如,为什么一个工程中每个CPP文件都要包含#include "stdafx.h"
  今天遇到一个问题,不知道怎么弄了。
  error C2011: 'CCriticalSection' : 'class' type redefinition
类似这样的错误。
  ForeSightDlg.h中添加一个#include "TimeThread.h" 就会报上边的错误,否则不会 ForeSightDlg.h
 中 #include "TimeThread.h"  ForeSightDlg.cpp  中
#include "stdafx.h"
#include "Foresight.h"
#include "ForesightDlg.h"
#include "Utils.h"  其中  Utils.h中包含 CCriticalSection的定义。
 
 TimeThread.h中
#if !defined(AFX_GETTimeTHREAD_H__E556DA03_4987_4013_802A_C2A42E953C5C__INCLUDED_)
#define AFX_GETTimeTHREAD_H__E556DA03_4987_4013_802A_C2A42E953C5C__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// GetTimeThread.h : header file
//#include <afxmt.h>  //CEVENT TimeThread.cpp中
 
 #include "stdafx.h"
#include "ForeSight.h"
#include "ForeSightDlg.h"
#include "TimeThread.h"
 我就不明白,为什么  ForeSightDlg.h中添加一个#include "TimeThread.h" 就会报error C2011: 'CCriticalSection' : 'class' type redefinitio的错误。
 TimeThread.h 会带来什么影响?

解决方案 »

  1.   

    预编译就是在编译之前先做一些工作, 你用#include时它就把相关的代码截出来, 如果多个源文件中前一部分相同, 那么一个好的编译器会把它们的公共部分取出来单独编译, 从而减少编译时间.
      

  2.   

    预编译就是预处理器做的工作。#include以后就是定义了,所以重报重定义的错误
      

  3.   

    redefinition是重定义错误,如果真的是包含那个文件导致的,就是那个文件中有'CCriticalSection' 类的定义,而当前文件中也有这个类的定义,或者是包含的文件中包含的文件中有这个定义,总之就是重复定义,仔细检查看看吧,要有耐心