C ++ Common Errors) and Common Errors

Source: Internet
Author: User

C ++ Common Errors

0. XXXX "is not a class or namespace" error
The most strange error indicates that the name you wrote is neither a class nor a namespace. Although my C ++ level is not very high, however, it is no longer stupid to connect to the class format class MyClass {....}; I cannot understand the cause of the error. It obviously does not matter. What is the problem?
The answer is: # include "stdafx. h" is not at the beginning of the code !!!
Stdafx. h:
Header file pre-compilation refers to some of the MFC standard header files used in a Project (such as Windows. h. Afxwin. h) pre-compilation. Later, this header file will not be compiled and only the pre-compilation results will be used. This will speed up compilation and save time.
The precompiled header file is generated by compiling stdafx. cpp and named after the project name. Because the precompiled header file is suffixed with "pch", the compilation result file is projectname. pch.
Didn't put # include "stdafx. h" at the beginning of the code? The compiler considers that all commands in the # include "stdafx. h "the previous code is pre-compiled, and it will skip # include" stdafx. h "command, use projectname. pch compiles all the code after this command. Therefore, of course, a class error cannot be found.
Therefore, the first statement of all CPP implementation files should be: # include "stdafx. h ".

I. LINK2001 error:

This type of error VS prompts, but double-click does not locate the error location, and the prompt is also inexplicable.

(1) error LNK2001: unresolved external symbol "void _ cdecl gameshudown (void )"(? Gameshudown @ YAXXZ)

Cause: the header file (such as main. h) declares the method gameshudown (), but it is not implemented in main. cpp.

Troubleshooting: search for "gameshudown" in the project, locate the. h file, and add the implementation method to the corresponding. cpp file.

(2) The subclass does not implement the parent class pure virtual function. Error:

D3DRenderer. obj: error LNK2001: unresolved external symbol "public: virtual void _ thiscall CD3DRenderer: SetMultiTexture (void )"(? SetMultiTexture @ CD3DRenderer @ UAEXXZ)
Debug/GameProject4.exe: fatal error LNK1120: 1 unresolved externals

Cause: Same as above, in D3DRenderer. cpp (D3DRenderer. obj corresponds to. in cpp), the virtual method SetMultiTexture () is not implemented. The virtual method is declared in the parent class of CD3DRenderer of the class. The virtual method must be implemented; otherwise, the unresolved externals error may occur.

Troubleshooting: simple. You can implement this virtual method in the class CD3DRenderer (D3DRenderer. cpp.

// If the C ++ subclass does not implement the pure virtual function of the parent class, the subclass also becomes an abstract class and the subclass cannot instantiate the object.
/* When designing the base class, it is difficult to determine the specific behavior of future behavior, but it is required.
Classes containing pure virtual functions are called abstract classes. An abstract class cannot instantiate its object, but can only serve its derived class.
If the subclass does not implement the pure virtual function of the parent class, the subclass also becomes an abstract class and it cannot instantiate the object.
*/

2. LINK2019 cannot parse the error of the external symbol "_ declspec (dllimport )"

Error 1 error LNK2019: the external symbol that cannot be parsed "_ declspec (dllimport) public: _ thiscall CEGUI: Unknown: OgreCEGUIRenderer (class Ogre: RenderWindow *, unsigned char, bool, unsigned int, class Ogre: SceneManager *) "(_ imp _?? Signature @ CEGUI @ QAE @ PAVRenderWindow @ Ogre @ E_NIPAVSceneManager @ 3 @ Z). This symbol is used in the function "protected: virtual void _ thiscall MouseQueryApplication: createScene (void) "(? In createScene @ MouseQueryApplication @ MAEXXZ), SampleAPP. obj is referenced.

The typical dll error cannot be found. The method called by the program is defined in the dll. There are two steps to solve the problem:

1. find the dll file and the corresponding lib with the same name (if it is a static link library). According to the above error, find the dll file starting with OgreCEGUIRenderer, find and put it in your workspace (probably \ bin) directory.

2. if the problem persists, the corresponding lib file is not referenced in the project, because other people's program code is okay, but you may not set the lib file added in the program settings, you can add the corresponding lib in the project properties-> linker-> input-> Add dependencies, or specify the method at the beginning of the program code (# include) once and for all:

# Pragma comment (lib, "CEGUIBase_d.lib ")

This is the same as setting project properties, but it will be more eye-catching. Third-party users cannot find dll errors when they commit your code.

3. When the program fails to read the disk, an error occurs because the image cannot be found, the resource file is incorrect, or a complex SDK function fails to be called.

This type of error vs will not be prompted, and troubleshooting is very difficult. In programming, you should develop a good habit of Multi-MessageBox where errors may occur.

Bad statement if (! M_Device) return; // return if m_Device fails to be created, but the following code is not executed, which is not what we want

If (! M_Device)

{

Messagebox (0 ,"! M_Device ", 0, 0); return; // you can see that the program returned for no reason is caused by m_Device creation failure.

}

4. Exit with an error when the program is compiled and run. The warning box "an unprocessed exception occurred to an exe at XXXXXX"

This is the most difficult BUG to tune. Many errors are exposed only during runtime. In this case, you need to use stack monitoring in VS to check for errors.

For example, "couldn't add <id, 4002201> twice, Continue?" is displayed during running ?"
Click "no" to interrupt the program. VS pops up. "Client.exe encountered an unhandled exception at XXXXXX. Are you sure you want to interrupt the program? "Click interrupted.
In this case, open the "call stack" window and see where the Green Arrow stops and where an error occurs. However, generally, it stops at the lower layer of the win api, if you cannot see any problem, you can find it at the bottom of the stack, that is, find it under the list to see who has called the method function with an error and find it step by step. In this case, I found:
VarContain. Add (XX, XX) seems to have encountered an error during loading. A keyword segment has been loaded twice, but it still cannot be seen to reload it. Then, you can find it at the bottom of the stack.
SkillProtoData-> LoadFromFile (strFilename ,.......) it seems that there is a problem loading files here. Use debug monitoring to check the file name and path of strFileName, which is "skillProtoName. xml file, open it. Sure enough, there are two "id = 4002201" items in the file, and the problem of deleting a duplicate item is solved.

Conclusion: Stack help is very important when there is a horrible error like "An exception not handled by an exe at XXXXXX !!!

Iv. error LNK2019: external symbol that cannot be parsed _ WinMain @ 16, which is in function ___ tmainCR...

I. Problem Description
MSVCRTD. lib (crtexew. obj): error LNK2019: external symbol that cannot be parsed _ WinMain @ 16, which is referenced in function ___ tmainCRTStartup
Debug \ jk.exe: fatal error LNK1120: 1 external command that cannot be parsed

Error LNK2001: unresolved external symbol _ WinMain @ 16
Debug/main.exe: fatal error LNK 1120:1 unresolved externals
Error executing link.exe;

Ii. causes and solutions
The real cause of this problem is that the appropriate program entry function cannot be found during the C language runtime,

In general, if it is a windows program, WinMain is the entry function, and the new project in VS2008 is a "win32 Project"

If it is a dos console program, the main function is the portal function. In VS2008, create a project as "win32 console application"

If the entry function is improperly specified, it is obvious that the function cannot be found during the C language runtime, and an error will be reported.

Modify settings to meet your needs

For windows programs:

1. Select Project> Properties from the menu to bring up the Property Pages window.

2. select Configuration Properties-> C/C ++-> Preprocessor from the left-side Navigation Pane, delete _ CONSOLE from the Preprocessor Definitions item on the right-side bar, and add _ WINDOWS.

3. Select Configuration Properties> Linker> System from the left-side Navigation Pane, and change the item corresponding to SubSystem in the right-side Navigation Pane to Windows (/SUBSYSTEM: WINDOWS)

For console programs:

1. Select Project> Properties from the menu to bring up the Property Pages window.

2. select Configuration Properties> C/C ++> Preprocessor from the left-side Navigation Pane, delete _ WINDOWS from the Preprocessor Definitions option on the right-side bar, and add _ CONSOLE.

3. Select Configuration Properties> Linker> System from the left-side Navigation Pane, and change the item corresponding to SubSystem in the right-side Navigation Pane to CONSOLE (/SUBSYSTEM: CONSOLE)

5. unhandled exception in **. exe 0x111552a1: 0xC0000005: Access conflict occurs when the read location is 0x00000018

Program interruption at Ogre: ResourceGroupManager: getSingleton (). addResourceLocation ("resource \ gui.zip", "Zip", "GUI ");

The prompt is as obscure as an alien text book. It seems that the hope of success is slim. Internet query people said, 0xC0000005 generally indicates a null pointer, and here it involves reading resources can guess the path is wrong. The relative path "resource \ gui.zip" is changed to absolute "d: \ gui.zip", which is normal, but there is still a problem in the project directory, what is the relative path? Is there a problem with the workspace settings?

Check "Project Properties"-> "debugging"-> "workspace" and set it to ".. \ bin \ Debug ", return to the parent directory to go to this directory and check that there is no" resource "folder. Create a new resourcecefolder, and put a new gui.zip file in it. Then compile the file, and report an error. Check the file carefully and change it to "resource \ gui.zip". The result is finally compiled successfully!

Experience: the so-called workspace is equivalent to a traditional project folder, so the relative paths are relative to this directory, rather than the program directory you take for granted. When writing program code paths, you must double dashes. Remember!

6. error C2360: initialization of 'C' is skipped by 'case' label

Void func (void)
{
Int x;
Switch (x)
{
Case 0:
Int I = 1; // error, skipped by case 1
{Int j = 1;} // OK, initialized in enclosing block
Case 1:
Int k = 1; // OK, initialization not skipped
}
}
When a switch statement defines a variable, if it is not in a statement block, it will not end until the switch "}" is encountered.

Therefore, if a new variable is defined in the case, it is best to add {} to the statement block in the case to avoid errors.
Either the variable is not defined in the case. To define the entire case, add {}

VII. "UINT WM_MY_REGISTERED_MSG" has been defined in "XXX. obj"
# Pragma once cannot solve the problem of repeatedly defining variables in the header file

At first glance, it is clear that the UINT WM_MY_REGISTERED_MSG variable is in XXX. cpp is defined repeatedly, but the whole project is searched and only WM_MY_REGISTERED_MSG is found in userMsg. the h header file defines one field: UINT WM_MY_REGISTERED_MSG; it is not defined elsewhere. What is the problem?
I think that a mentor once and again told me that the header file is only suitable for # define constants and declared classes and struct structures. It is not suitable for defining variables. To define variables, you must all be in. in cpp. Then I try to put UINT WM_MY_REGISTERED_MSG in the main file userMsg. cpp. That's right.

It seems that the header file contains multiple files, although the header file has already been written # pragma once can solve the problem of Repeated inclusion of the header file, but it cannot solve the problem of repeated definition variables. A variable is defined in a header file, even the inconspicuous int n; as long as the header file is multiple cpp # include, the int n is repeatedly defined, XXX already exists in XXX. the strange phenomenon defined in obj.

Conclusion:
Only the structure can be declared in the header file. No variable can be defined !!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.