八、Visual Studio中,执行文件和库文件环境变量不一致
1、Debug还是Release
2、64位还是32位
3、平台工具集 v80 v90 v100 v110 v120
九、使用库文件,如果该库有命名空间,最好在预编译头文件中声明该头文件
//stdafx.h
#include <PushFrameworkInc.h>
using namespace PushFramework;
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/repeated_field.h>
#include <google/protobuf/extension_set.h>
#include <google/protobuf/generated_message_reflection.h>
using namespace google;
#include <TestSuiteMessagesInc.h>
using namespace testsuitemessages;
#include <TCPSocketInclude.h>
十、动态库、静态库混合生成
如:ChatAPI.dll ==> 依赖于==> ProtobufProtocol.lib ==> 依赖于PushFramewokr.dll
这种情况很容易编译不过,最好要么统一用动态库,要么统一用静态库。
十一、继承类中,派生类的定义.h文件要包含基类.h文件
如果只在派生类.cpp文件中包含基类的.h文件,而在.h文件中不包含基类的.h文件,则会报错
出现错误为:errorC2504未定义基类。
//四个头文件
class A{
public:
A();
~A();
};
//
//应该添加#include "classA.h"
class A;
class A1:public A
{
public:
A1();
~A1();
};
//
class B{
public:
B();
~B();
};
//
//应添加#include "classB.h"
class B;
class B1:public B{
public:
B1();
~B1();
};
///
四个源文件
#include "classA.h"
#include "iostream"
using namespace std;
A::A()
{
cout<<"constructor A.\n";
}
A::~A()
{
cout<<"destructor A.\n";
}
//
#include "classA1.h"
#include "iostream"
using namespace std;
A1::A1()
{
cout<<"constructor A1.\n";
}
A1::~A1()
{
cout<<"destructor A1.\n";
}
///
#include "classB.h"
#include "iostream"
using namespace std;
B::B()
{
cout<<"constructor B.\n";
}
B::~B()
{
cout<<"destructor B.\n";
}
/
#include "classB1.h"
#include "iostream"
using namespace std;
B1::B1()
{
cout<<"constructor B1.\n";
}
B1::~B1()
{
cout<<"destructor B1.\n";
}
//
主函数
#include "iostream"
using namespace std;
#include "classA1.h"
#include "classB1.h"
void main()
{
A1 objA;
B1 objB;
system("pause");
}
出现错误为
errorC2504未定义基类
十、while死循环,占用CPU时间过多。
十一、静态库、动态库使用
四步必须都做:
1、通用属性 --> 引用 ==》选择所需要的库
2、配置属性 --> C/C++ --> 常规 --> 附加包含目录 ==》添加库头文件所在目录
3、配置属性 --> 链接器 --> 常规 --> 库目录 ==》添加库文件所在的目录
4、配置属性 --> 链接器 --> 输入 --> 附加依赖项 ==》添加库的文件名
十二、在MFC项目中定义全局变量
只能在.cpp文件中定义,不能在.h文件中定义,这应该算是VS2013中的一个bug
否则如报错:fatal error LNK1169: 找到一个或多个多重定义的符号
十三、使用STL时,遍历容器,删除某个元素时,迭代器指针忘记指向首个元素。
for (std::deque<INFO_CONN_CTRL>::iterator itConn = g_connCtrlQueue.begin(); itConn != g_connCtrlQueue.end(); itConn++)
{
//if (itConn->cstrPseudo == cstrClient)
if (cstrClient.Compare(itConn->cstrPseudo) == 0)
{
for (std::vector<CONTROL_TIME>::iterator itTime = itConn->vectCtrlTime.begin(); itTime != itConn->vectCtrlTime.end(); itTime++)
{
CString cstrFormerTime;
cstrFormerTime.Format(_T("%02d:%02d -- %02d:%02d"), itTime->nBeginHour, itTime->nBeginMin, itTime->nEndHour, itTime->nEndMin);
//if (cstrFormerTime == cstrCtrlTime)
if (cstrCtrlTime.Compare(cstrFormerTime) == 0)
{
AfxMessageBox(cstrFormerTime);
itConn->vectCtrlTime.erase(itTime);
//一定要,否则删除最后一条记录是报错
itTime = itConn->vectCtrlTime.begin();
}
}
}
}
十四、头文件相互包含、导致无限递归
解决的办法是:A.h在头文件中包含B.h,B.h在头文中的声明类class A;,在B.cpp文件中包含A.h
//A.h
#pragma once
#include "B.h"
class A
{
private:
B* m_pB;
};
//B.h
class A;
class B
{
public:
A* CreateA();
}
//B.cpp
#include "A.h"
A* B::CreateA()
{
return new A();
}
十五、VS2013,如果修改代码后,一定要重新生成,再执行,不能直接Ctrl+F5,这样可能会导致有些代码没有被编译,出现意想不到的Bug。