C++常见错误

  • 格式:pdf
  • 大小:178.90 KB
  • 文档页数:26

VC编译常见错误--1、Fatal Error C1010unexpected end of file while looking for precompiled header di rective这一般是由于使用了参数/Yu"stdafx.h",意思是在每个文件中都应该使用#include来包含这个头文件。

一般改正,就是在每个。

CPP文件中包含这个文件就可以。

2、LNK2001on__beginthreadex and__endthreadex这是一个非常常见的链接错误。

原因是由于在VC3。

0以后所有的M FC类都是线程安全的,在MFC类库中使用了Thread Local Stora ge(TLS)提供预处理的数据。

因此如果程序中包含了"stfafx.h"头文件或者使用了,MFC类库中的类就会使用MSVCRTx0.DLL来进行链接。

改正方法如下:On Visual C2.x,5.0,and6.0:Select the Project menu,and then continue Steps2throug h5below.On Visual C4.x:Select the Build menu.Select the Settings...option.Select the C/C++tab.Select Code Generation on the Category list box.Finally,make a selection other than Single-Threaded on th e Use Run Time Library list box.如果使用了"Multithreaded using DLL"方式还要求在预处理符号中加上_AFXDLL符号.还在LIBC。

LIB是C Runtime的静态链接库。

MSVCRTx0.DLL是C Runtime的动态链接库。

如果程序中包含了任何MFC代码,或者编译使用了/MT选项,都要求使用多线程库。

为什么在程序中包含了"StdAfx.h"文件也会出现这个连接错误呢?是由于在"StdAfx.h"中使用了MFC类,并且重载了new等操作符,如果在程序中使用了NEW等就会出现链接错误。

1、Fatal Error C1010unexpected end of file while looking for precompiled header di rective这一般是由于使用了参数/Yu"stdafx.h",意思是在每个文件中都应该使用#include来包含这个头文件。

一般改正,就是在每个。

CPP文件中包含这个文件就可以。

2、LNK2001on__beginthreadex and__endthreadex这是一个非常常见的链接错误。

原因是由于在VC3。

0以后所有的M FC类都是线程安全的,在MFC类库中使用了Thread Local Stora ge(TLS)提供预处理的数据。

因此如果程序中包含了"stfafx.h"头文件或者使用了,MFC类库中的类就会使用MSVCRTx0.DLL来进行链接。

改正方法如下:On Visual C2.x,5.0,and6.0:Select the Project menu,and then continue Steps2throug h5below.On Visual C4.x:Select the Build menu.Select the Settings...option.Select the C/C++tab.Select Code Generation on the Category list box.Finally,make a selection other than Single-Threaded on th e Use Run Time Library list box.如果使用了"Multithreaded using DLL"方式还要求在预处理符号中加上_AFXDLL符号.还在LIBC。

LIB是C Runtime的静态链接库。

MSVCRTx0.DLL是C Runtime的动态链接库。

如果程序中包含了任何MFC代码,或者编译使用了/MT选项,都要求使用多线程库。

为什么在程序中包含了"StdAfx.h"文件也会出现这个连接错误呢?是由于在"StdAfx.h"中使用了MFC类,并且重载了new等操作符,如果在程序中使用了NEW等就会出现链接错误。

/user2/shuquanying/archives/2006/1327664. shtmlVisual C++Error MessagesVisual C++Error MessagesThis page contains a listing of"difficult to diagnose"error mes sages and possible fixes.I haven't taught a programming clas s that uses Visual C++in several years so this list is probabl y out of date by now.It was valid for Microsoft Visual C++ve rsion6.0service pack3.C1001:INTERNAL COMPILER ERROR(compiler file'msc1.cp p',line1786)Please choose the Technical Support command on the Visual C++Help menu,or open the Technical Support help file for more informationThis error results from leaving off the parentheses immediately following the function name in a function header.To correct t he error simply add()to the end of the function name.C1010:unexpected end of file while looking for precompiled h eader directiveIf your project is an MFC AppWizard created project then this error results from not#including StdAfx.h as the first#i nclu de statement(before any other#i ncludes,data declarations, or executable program code).C1083:Cannot open precompiled header file:'Debug/<Project-Name>.pch':No such file or directoryThis error results from a missing file-the compiled version of StdAfx.cpp.Visual C++does a poor job of keeping track of t his file and frequently"forgets"how to build it.This problem o ften occurs after restoring a saved workspace from diskette wi thout the Debug directory.To fix the error select StdAfx.cpp fr om the workspace file list them choose Compile from the Buil d menu.If that doesn't work the go to Project->Settings,sel ect the C/C++tab,and click the radio button labeled Create Precompiled Headers.C2001:newline in constantThis error is usually caused by a string or character constant that is missing its closing'or"symbol.C2065:'<data-member name>':undeclared identifierIf this error occurs in one of your member functions then it is generally the result of forgetting the class scope operator in f ront of the function name in your.cpp file.C2143:syntax error:missing';'before'PCH creation point' Check each of the#i nclude files to ensure that the closing brace of each class declaration is followed by a semicolon.C2143:syntax error:missing';'before'*'If this error is followed by two C2501errors then the problem is an undeclared class name within a pointer declaration.For example,the declaration:CClass*pObject;will generate the above error message followed by a C2501e rror message for'CClass'and another C2501message for'pO bject'.The problem is that the compiler isn't recognizing CClas s as a valid class/type name.To correct the problem add a#i nclude of the file containing the declaration of CClass(e.g.,#i nclude CClass.h)C2447:missing function header(old-style formal list?)This error usually results from a missing{or use of a;inste ad of a{following the parameter list of a function header.C2511:'<function-name>':overloaded member function not fo und in'<class-name>'This error results from a mismatch in the parameter list of a member function declaration(.h file)and definition(.ccp file). Check the forward declaration of the function in the.h file and its definition in the.cpp file and ensure that the number of p arameters and the type of each parameter match exactly.C2512:'<constructor-function-name>':no appropriate default c onstructor availableThis error usually occurs when you implement the constructor function of a derived class and forget to include parameter pa ssing to the base class constructor function.For example ass ume that CDerived is derived from CBase and that the CBase constructor function requires one parameter(e.g.,int A).If yo u define the CDerived constructor function as:CDerived::CDerived(int A,int B){...}the compiler will issue the above error message on the line c ontaining the function header of CDerived::CDerived()because you haven't provided instructions for routing the parameter A to CBase::CBase().Because you didn't provide instructions the compiler assumes that CBase::CBase()requires no arguments and it complains because no version of CBase::CBase()has been defined that accepts zero arguments.If you intended to provide a version of CBase::CBase()that re quires no arguments then the error message indicates that yo u forgot to declare that function in your base class declaration (e.g.,in CBase.h).If CBase::CBase()does require one or more arguments then y ou must correct the problem by including explicit instructions for passing parameters from the derived class constructor funct ion to the base class constructor function.The correction for t he example above is:CDerived::CDerived(int A,int B):CBase(A){...}C2556:'<function-name>':overloaded functions only differ by return typeC2371:'<function-name>':redefinition;different basic types These errors usually result from a mismatch of function type b etween a.h and.cpp file.Check the forward declaration of th e function in the.h file and its definition in the.cpp file and make the function return type identical in both files.C2601:'<function-name>':local function definitions are illegal This error results from defining one function inside the body of another function.It usually means that you omitted one or more}symbols in the function just before the function named in the error message.C2653:'<Class-Name>':is not a class or namespace name This error usually results from not having#i nclude"StdAfx.h" as the first#i nclude statement in your class.cpp file.It can also occur if your class definition is in a.h file and you forget to#i nclude that.h file in another file that refers to the cl ass name.C2661:'<Class-Name>::<Function-Name>':no overloaded func tion takes n parametersThis error indicates a mismatch between the parameters used in a function call(e.g.,from main.cpp)and the declaration of the function.The function call is passing n parameters and t here is no function declaration that uses that number of para meters.LNK1104:Cannot open file nafxcwd.libThis error sometimes occurs when a project uses a class fro m the MFC but the project settings don't explicitly tell the link editor to look in the MFC libraries.Go to Project-->Settings(Build-->Settings in Visual C++4.0).On the General tab check the box that says"Use MFC ina Shared DLL".LNK1168:cannot open Debug\<Project-Name>.exe for writing This error occurs when the link editor attempts to write to a. exe file that is currently in use.The.exe file of an executing program is write protected until the program is terminated.Loo k at the status bar at the bottom of your screen and find theicon representing your executable application.Open the applic ation and exit from it.Then select Build.LNK2001:unresolved external symbol__endthreadexLNK2001:unresolved external symbol__beginthreadexThese errors result from using an MFC object or function with out telling the link editor to search the MFC libraries.Go to Project-->Settings(Build-->Settings in Visual C++4.0).On the General tab check the box that says"Use MFC ina Shared DLL".LNK2001:unresolved external symbol_mainYour project doesn't contain a function called main().The error usually results from forgeting to add main.cpp to the project workspace.<File>.obj:error LNK2001:unresolved external symbol"public: void__thiscall<Class1>::<Function1>(<Type>)"This a generic form of a LNK2001error where<File>.obj can be any object file in your project and<Class1>::<Function1>(< Type>)can be any function in any class.Substitute the specifi c<File>,<Class>,<Function>,and<Type>in your message i nto the instructions below to diagnose and correct the proble m.An LNK2001error means that the link editor is looking for a compiled function and can't find it.The call to the"missing fu nction"is somewhere in<File>.cpp.Unfortunately,double-clicki ng on the error message won't take you to the point in<File. cpp>where the function is called but you can search for it wit h Find or Find In Files.The function the link editor can't find i s a member of<Class>,its name is<Function1>,and its retu rn type is<Type>.There are two common reasons for a LNK2001error:The call in<File>.cpp doesn't match the function prototype in <Class>.h and/or the implementation in<Class>.cpp.The mis match may be in the function name,return type,or number a nd/or type of parameters.Correction strategies include: Check that the function name is spelled the same(case sensi tive)in all three files(File.cpp,Class.h,and Class.cpp). Check that the function is actually declared and defined within <Class>-perhaps you defined it as a member of a different class or perhaps you tried to call the function(in<File>.cpp) using an object or object pointer of a different class.Check that the number and type of parameters in the function implementation(in<Class>.cpp)matches the number and typ e of parameters declared in the function declaration in<Class>.h.Check that the number and type of parameters in the function call(in<File>.cpp)matches the number and type of paramet ers declared in the function header in<Class>.cpp.The function was never declared or was declared but never d efined.To see if either is the case go to the ClassView windo w of the Workspace view.Click the+next to<Class>and fin d<Function>in the list of member functions.If<Function>is NOT in the list then it was never declared or defined-add a declaration to the class declaraion in<Clas s>.h and implement the function in<Class>.cpp.If<Function>is in the list then right click on it and select Go To Definition from the pop-up menu.If you get the error mes sage Cannot find definition(implementation)of this function th en the function was declared but never defined(implemented). Implement the function to<Class>.cpp.LNK2005:<some-long-string-of-mostly-garbage>already defined in<name>.lib(<name>.obj)This error usually results from including a source code file mul tiple times.If you recognize any of the names in the message then it probably results from multiple inclusion of one of your own header files.Check to be sure that you've used#ifndef/#define/#endif properly your header files.If you don't recognize the name then it's probably multiple inclusion of a system file (e.g.,afxwin.h).Make sure that you haven't explicitly included something in main.cpp that is already included in one of your own header files.Also check that you haven't#i ncluded a .cpp file where you should have#i ncluded a.h file./5383739_d.html作者:happyparrot出处:[url]/Expert/t opic/4258/4258967.xml?temp=.1825373[/url]1.Linking...nafxcwd.lib(thrdcore.obj):error LNK2001:unresolved extern al symbol__endthreadexnafxcwd.lib(thrdcore.obj):error LNK2001:unresolved extern al symbol__beginthreadexlibcd.lib(crt0.obj):error LNK2001:unresolved external symb ol_mainQ:VC++默认的工程设置是单线程的,而你使用了多线程,所以要修改设置。