基于Visual Studio开发C++项目的错误解决笔记

开发环境:win7/10;VS201/2015;Qt4.8.6/5.7.1;CMake

1、error LNK2005: "double * X" 已经在 main.obj 中定义;

参考1:https://www.cnblogs.com/MuyouSome/p/3332699.html   https://blog.csdn.net/yan_less/article/details/73149578

1.重复定义全局变量。可能存在两种情况:  
 A、对于一些初学编程的程序员,有时候会以为需要使用全局变量的地方就可以使用定义申明一下。其实这是错误的,全局变量是针对整个工程的。
  正确的应该是在一个CPP文件中定义如下:
  int   g_Test;
  那么在使用的CPP文件中就应该使用:
  extern   int   g_Test
  即可,如果还是使用int   g_Test,那么就会产生LNK2005错误,一般错误错误信息类似:
  *.obj   error   LNK2005   int   book   c?   already   defined   in   *.obj
  切记的就是不能给变量赋值否则还是会有LNK2005错误。  
  这里需要的是“声明”,不是“定义”!根据C++标准的规定,一个变量是声明,必须同时满足两个条件,否则就是定义:  
  (1)声明必须使用extern关键字
  (2)不能给变量赋初值  
  所以,下面的是声明:  
    extern   int   a;  
  下面的是定义  
    int   a;  
    int   a   =   0;  
    extern   int   a   =0;  
  B、对于那么编程不是那么严谨的程序员,总是在需要使用变量的文件中随意定义一个全局变量,并且对于变量名也不予考虑,这也往往容易造成变量名重复,而造成LNK2005错误。  

2.头文件的包含重复。
  往往需要包含的头文件中含有变量、函数、类的定义,在其它使用的地方又不得不多次包含之,如果头文件中没有相关的宏等防止重复链接的措施,那么就会产生LNK2005错误。解决办法是在需要包含的头文件中做类似的处理:
  #ifndef   MY_H_FILE       //如果没有定义这个宏  
  #define   MY_H_FILE       //定义这个宏  
  …….       //头文件主体内容  
  …….  
  #endif  
  上面是使用宏来做的,也可以使用预编译来做,在头文件中加入:  
  #pragma   once  
  //头文件主体  
 3.使用第三方的库造成的。
  这种情况主要是C运行期函数库和MFC的库冲突造成的。具体的办法就是将那个提示出错的库放到另外一个库的前面。另外选择不同的C函数库,可能会引起这个错误。微软和C有两种C运行期函数库,一种是普通的函数库:LIBC.LIB,不支持多线程。另外一种是支持多线程的:msvcrt.lib。如果一个工程里,这两种函数库混合使用,可能会引起这个错误,一般情况下它需要MFC的库先于C运行期函数库被链接,因此建议使用支持多线程的msvcrt.lib。所以在使用第三方的库之前首先要知道它链接的是什么库,否则就可能造成LNK2005错误。如果不得不使用第三方的库,可以尝试按下面所说的方法修改,但不能保证一定能解决问题,前两种方法是微软提供的:  
  A、选择VC菜单Project->Settings->Link->Catagory选择Input,再在Ignore   libraries   的Edit栏中填入你需要忽略的库,如:Nafxcwd.lib;Libcmtd.lib。然后在Object/library   Modules的Edit栏中填入正确的库的顺序,这里需要你能确定什么是正确的顺序,呵呵,God   bless   you!  
  B、选择VC菜单Project->Settings->Link页,然后在Project   Options的Edit栏中输入/verbose:lib,这样就可以在编译链接程序过程中在输出窗口看到链接的顺序了。  
  C、选择VC菜单Project->Settings->C/C++页,Catagory选择Code   Generation后再在User   Runtime   libraray中选择MultiThread   DLL等其他库,逐一尝试。  


综上,你应该把要申明的变量放在一个1.h中,然后在其中写如下代码

#pragma once 
int i=0;

然后新建的.cpp文件中包含#include<1.h>

参考2:https://blog.csdn.net/zhaoyong26/article/details/84635383

经过仔细看代码发现,一个全局变量在一个头文件中定义,比如:head1.h中定义了全局变量int a = 10;

在一个类的头文件class.h中包含了头文件head1.h如:include "head1.h"

于是将include "head1.h"从class.h中移到了class.cpp中,问题解决。

分析,因为class.cpp中include "class.h"class.h中include "head1.h",发现没,会造成head1.h的重复引用,虽然用pragma等也无法解决,移到class.cpp中,它生成obj文件时,只引用一次,问题解决。

 2、warning C4482: 使用了非标准扩展: 限定名中使用了枚举

来源: http://blog.sina.com.cn/s/blog_6dc6b05d0100r3nh.html   http://dev.firnow.com/course/3_program/c++/cppsl/2008619/126863.html

MSDN:
compiler warning (level 1) C4482
warning description:
使用了非标准扩展: 限定名中使用了枚举“enum”
当引用类型内的枚举时,无需指定枚举的名称。
文件范围的枚举定义相当于常量,也不需要限定名。
example: C4482(MS Visual Studio)
// C4482.cpp
// compile with: /c /W1
struct S {
   enum E { a };
};

int i = S::E::a; // C4482
int j = S::a; // OK

NOTE:
warring C4482: nonstandard extension used: enum **** used in qualified name

enumeration:
1. Standard C++ does not allow enumeration names to be used as scopes (GCC: error)
Visual C++ is half-hearted on the matter. (warrning)
2. C# support enumeration names as scopes.

Workaround: (for MS Visual Stdio)
if you want to get rid of those obnoxious error message and you’re using MS Visual Studio, encompass your statement in #pragma directives. For example,

#progma warning(push)
#progma warning(disable : 4482)
... (your statement here) ...
#progma warning(pop)

That tells the compiler not to report the C4482 warning for the code within the block.
You are essentially saying to the compiler, “Yes, I’m doing this intentionally. I know it’s not comply with the standard, but I want it done anyway. So build my project and quit your whining!”

3、Qt捕获了事件处理程序引发的异常(Qt has caught an exception thrown from an event handler. Throwing exceptions from an event handler is not supported in Qt. You must reimplement QApplication::notify() and catch all exceptions there.)

来源: https://stackoverflow.com/questions/10075792/how-to-catch-exceptions-in-qt  http://www.02.246.ne.jp/~torutk/cxx/qt/QtMemo.html

//(未测试!)
//继承QApplication并重写notify成员函数 //定义一个派生类,该派生类重写Qt消息中的QApplication :: notify。 //MyApplication.h #include
<QApplication> class MyApplication : public QApplication { public: MyApplication(int& argc, char** argv); bool notify(QObject* receiver, QEvent* event); };
//MyApplication.cpp #include
"MyApplication.h" #include <exception> MyApplication::MyApplication(int& argc, char** argv) : QApplication(argc, argv) {} bool MyApplication::notify(QObject* receiver, QEvent* event) { bool done = true; try { done = QApplication::notify(receiver, event); } catch (const std::exception& ex) { // 日志或某些恢复过程 } catch (...) { // 日志或某些恢复过程 } return done; } //当捕获到异常时,记录日志,屏幕上的弹出窗口等,并通知异常发生,如果可以处理异常,则执行恢复处理。对于屏幕上的弹出窗口,通常使用QMessageBox :: critical(...)。

原文地址:https://www.cnblogs.com/JoyPoint/p/14257687.html