In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the programming mistakes that novices are easy to make in C++?" in the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Some keywords are overwritten in the cpp file
For C++ classes, as long as some keywords are written in .h, there is no need to add them in cpp, such as virtual, static and other keywords. If you write more keywords in cpp, the compiler will report an error. For example, the following definitions of virtual interfaces and static member variables can be declared in the header file.
Class shape {virtual Draw (); / /... Static int nLevel;} 2. The default values of the function parameters are written into the function implementation.
For functions with parameter default values, the default values are added to the function declaration, and the parameters at the function implementation do not need to be taken. To make it easier to view the code, comment the default values in the parameters at the function implementation. The correct thing to do is to have default values in the header file:
BOOL CreateConf (const CString& strConfName, const BOOL bAudio = FALSE)
There is no need to add default values to the parameters at the function implementation:
BOOL CreateConf (const CString& strConfName, const BOOL bAudio/* = FALSE*/); {/.} 3. When writing a class, I forgot to add ";" at the end of the class.
If you forget to add a semicolon at the end of the class, the compilation will report an error, and the newcomers may not find out the cause of the compilation error for a long time. It's really simple. I forgot to add a semicolon at the end of the class.
Class Shape {/ /...}; 4. Only function declaration has been added, no function implementation
When adding a function of a class, only the function declaration is added in the header file of the class, but there is no implementation of the function in cpp. If the function is called elsewhere, a unresolved external symbol error will be reported when compiling the link. Because there is no implementation, all obj files that are not available for links.
5. The cpp file was forgotten to be added to the project, resulting in no obj file generated for the link
When adding C++ classes, we usually add .h header files and a .cpp source file. As a result, I forgot to add the .cpp file to the project, that is, I did not participate in the compilation and did not generate an obj file for the link. If there is code that calls the interface of the C++ class, a unresolved external symbol error will be reported when compiling the link, that is, the corresponding interface of the C++ class cannot be linked.
6. The address or reference of a local variable is returned in the function
The address or reference of a local variable is returned in the function, and the life cycle of the local variable ends at the end of the function, and memory is freed. When the memory of the variable is accessed externally, a memory access violation is triggered because the memory of the variable has been freed. For example, the following error code:
Char* GetResult () {char chResult [100] = {0}; /. Return chResult;} 7. Forgot to declare the virtual function of the interface in the parent class, so the polymorphism did not take effect.
The code was supposed to call the interface implemented by the subclass with the help of the virtual function call of C++ polymorphism, but forgot to declare the corresponding interface as virtual in the parent class, resulting in not calling the function implemented by the subclass. It is important to remember that to implement polymorphic function calls, the relevant interface of the parent class must be declared as virtual.
Class Shape () {/ /... Virtual void Draw (); / /...} 8. Where a double pointer should be used, a single pointer is used
Sometimes we need to call an interface to get some data, and the data is copied to the memory corresponding to the incoming parameter, and a pointer or reference is passed in when the parameter is designed. We define the structure pointer p before calling GetData, and new the corresponding structure object memory. We should use double pointers (pointer pointers) when defining the GetData interface. As a result, it is miswritten as a single pointer.
The code in question is as follows:
Struct CodecInfo / / Encoding information {int nFrameRate; / /...} CodecInfo* pInfo = new CodecInfo; GetAudioCodecPtr ()-> GetCodecInfo (pInfo); / / call AudioCodec::GetCodecInfo to get encoding information AudioCodec::GetCodecInfo (CodecInfo* pInfo) / / the parameters here should not use a single pointer {memcpy (pInfo, m_codecInfo, sizeof (CodecInfo));}
The parameters of the AudioCodec::GetCodecInfo API in the figure above should not be single pointers, but should be double pointers. The modified code should be as follows:
AudioCodec::GetCodecInfo (CodecInfo** pInfo) / / the parameter types here use double pointers {memcpy (* pInfo, m_codecInfo, sizeof (CodecInfo));} 9. When you publish an exe program, you forget to bring the C runtime library and MFC library that exe depends on
For example, when a newcomer writes a testing tool software with the VS-MFC library, when releasing the release version of the program, he does not bring the C runtime library on which the program depends, causing the tool software to start reporting an error on some computers and indicating that the C runtime library cannot be found:
Because the program relies on dynamic versions of runtime libraries and MFC libraries, you should take these libraries with you when you release the program. Some systems do not have these libraries, so when the program starts, it will report that the library cannot be found and will fail to start.
10. A deep copy should be used, but a shallow copy should be used
When a deep copy is supposed to be made, a shallow copy (direct assignment) is used, causing another C++ object with a different life cycle to point to the same piece of memory. After one object frees up memory, another object uses this piece of memory, resulting in a memory access violation and an exception.
There is a classic C++ written test question, let's implement the relevant functions of the String class, its main purpose is to examine the understanding of deep copy and shallow copy. The declaration of the String class is given in the title:
Class String {public: String (); String (const String& str); String (const char* str); String& operator= (String str); char* c_str () const; ~ String (); int size () const;private: char* data;}
Let's write out the internal implementation of the above functions. The implementation code for these functions is as follows:
/ / ordinary constructor String::String (const char * str) {if (str = = NULL) {m_data = new char [1]; / / score points: automatically apply for the end flag'\ 0' for empty strings, plus NULL judgment: * m_data ='\ 0y 'for m_data;} else {int length = strlen (str); m_data = new char [length + 1] / / if you can add NULL to judge, it is better to strcpy (m_data, str);}} / / String destructor String::~String (void) {delete [] mdistinct data; / / or delete mdistinct data;} / / copy constructor String::String (const String & other) / / score point: input parameter is "int length = strlen (other.m_data)"; m_data = new char [length + 1] / / better strcpy (m_data, other.m_data) if you can add NULL judgment;} / / assignment function String & String::operator = (const String & other) / / score point: input parameter is if (this = = & other) / / score point: check the self-assigned return * this; if (m_data) delete [] m_data / / score points: release the original memory resources int length = strlen (other.m_data); m_data = new char [length + 1]; / / points: judge strcpy (m_data, other.m_data) for m_data plus NULL; return * this;// score points: return the reference to this object} "what are the programming mistakes easy to be made by novices in C++". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.