Code Requirement |
A class must have one or more functions that use a reference parameter to return a value to the calling function. |
Correct Code |
Function in class SomeClass. int m_iIntVal is a member variable of SomeClass and holds 32
which was set in the class constructor.void SomeClass::getIntValRef(int& a) { a = m_iIntVal; } Code in main: SomeClass *sc = new SomeClass(); // Create an instance of a class int iVal = 0; sc->getIntVal(iVal);// Call the function passing the name of the variableThe variable iVal now holds the value 32. |
Incorrect Code |
Error: The address operator is not placed in front of the reference variable a in the code:void SomeClass::getIntValRef(int& a) { &a = m_iIntVal; } |
Code Requirement |
A class must have one or more functions that use a pointer parameter to return a value to the calling function. |
Correct Code |
Function in class SomeClass. int m_iIntVal is a member variable of SomeClass and holds 32 which was set
in the class constructor.void SomeClass::getIntValPtr(int *a) { *a = m_iIntVal; } Code in main: SomeClass *sc = new SomeClass(); // Create an instance of a class int iVal = 0; sc->getIntValPtr(&iVal);// Call the function passing in the address of the varaibleThe variable iVal now holds the value 32. |
Incorrect Code |
Error: The pointer operator is missing in front of the pointer variable a in the code:void SomeClass::getIntVal(int& a) { a = m_iIntVal; } |
Code Requirement |
A class must have a set function that takes a char array parameter and copies that string into a char array class member variable. |
Correct Code |
Function in class SomeClass. char m_sName[32] is a member variable of SomeClass.
The arguments to strcpy (string copy) are strcpy(destinationArray, sourceArray).void SomeClass::setName(char *name) { strcpy(m_sName, name); } Code in main: SomeClass *sc = new SomeClass(); // Create an instance of a class char nameArr[32] = "Bob"; // You can create and initialize a char array // You cannot set a char array after creating it with code like: nameArr="Bob" sc->setName(nameArr);// Call the function passing in the char array. // NOTE: You are really passing the starting address of the char array |
Incorrect Code |
Error: You cannot set a char array by using the equals operator.void SomeClass::setName(char *name) { m_sName = name; } |
Incorrect Code |
Error: The char arrays are in the incorrect order in the function code.void SomeClass::setName(char *name) { strcpy(name, m_sName); } |
Code Requirement |
A class must have a get function that takes a char array parameter and copies a char array class member variable into that string. |
Correct Code |
Function in class SomeClass. char m_sName[32] is a member variable of SomeClass.
The arguments to strcpy (string copy) are strcpy(destinationArray, sourceArray).
Assume m_sName holds the string "Bob" which was set in the class constructor.void SomeClass::getName(char *name) { strcpy(name, m_sName); } Code in main: SomeClass *sc = new SomeClass(); // Create an instance of a class char nameArr[32]; sc->getName(nameArr);// Call the function passing in the char array. // NOTE: You are really passing the starting address of the char arraynameArr now holds the string "Bob". |
Incorrect Code |
Error: You cannot set a char array by using the equals operator.void SomeClass::getName(char *name) { name = m_sName; } |
Incorrect Code |
Error: The char arrays are in the incorrect order in the function code.void SomeClass::getName(char *name) { strcpy(m_sName, name); } |
Code Requirement |
A class must have a function that returns a pointer to an instance of a struct or class. |
Correct Code |
Function prototype in the file SomeClass.h.MyClass *getMyClassInstance(); // Create and return an instance of MyClassFunction defined in the file SomeClass.cpp. MyClass *SomeClass::getMyClassInstance() { MyClass *mc = new MyClass(); // Code to finish initialization of the MyClass instance is here return mc; } |
Incorrect Code |
Error: The asterisk is part of the return type and must be on the other side of the class name.MyClass SomeClass::*getMyClassInstance() { MyClass *mc = new MyClass(); // Code to finish initialization of the MyClass instance is here return mc; } |
Code Requirement |
At some point in code you must compare two char array strings to determine if they hold the same string or if one alphabetically comes before the other. |
Correct Code |
Given the following code defining and setting several char array strings.char str1[32]; char str2[32]; char str3[32]; strcpy(str1, "Able"); // Copy string constant "Able" into str1 strcpy(str2, "Able"); // Copy string constant "Able" into str2 strcpy(str3, "Baker"); // Copy string constant "Baker" into str3 Compare str1 and str2 for holding the same string using the function strcmp. if(strcmp(str1, str2) == 0) { cout << "Strings str1 and str2 hold the same string.\n"; }strcmp returns zero if the two char arrays hold the same string. Compare str1 and str3 for str1 being alphabetically before str3. if(strcmp(str1, str3) < 0) { cout << "String str1 alphabetically comes before string str3.\n"; }strcmp returns a number less than zero if the first argument alphabetically comes before the second argument. Compare str1 and str3 for str1 being alphabetically before str3. if(strcmp(str3, str1) > 0) { cout << "String str1 alphabetically comes before string str3.\n"; }strcmp returns a number greater than zero if the first argument alphabetically comes after the second argument. |
Incorrect Code |
Error: The == operator has not been overloaded for use with char array strings. All this does is check to see if the starting address of char array str1 is equal to the starting address of char array str2, which of course it is not. if(str1 == str2) { cout << "Strings str1 and str2 hold the same string.\n"; } |
Incorrect Code |
Error: The < operator has not been overloaded for use with char array strings. All this does is check to see if the starting address of char array str1 is less than the starting address of char array str3, which of course it is not. if(str1 < str3) { cout << "String str1 alphabetically comes before string str3.\n"; } |
Incorrect Code |
Error: The > operator has not been overloaded for use with char array strings. All this does is check to see if the starting address of char array str1 is greater than the starting address of char array str3, which of course it is not. if(str3 > str1) { cout << "String str1 alphabetically comes before string str3.\n"; } |
Code Requirement |
The delete function in a binary tree must return a pointer to the item removed but it keeps returning NULL or the program crashes. |
Correct Code |
Given the following code defining the delete function in a binary tree.TreeNode *deleteNode(int key) { TreeNode *temp; TreeNode *back; // All the code is here to remove the node from the tree if key is found. // Temp is now pointing to NULL or the one to return. return temp; } |
Incorrect Code |
Error: Some examples in the Code Vault show the item removed from the tree as being deleted before returning a boolean true or false indicating success or failure to delete. In this error the code was copied but was not studied to see exactly what it was doing so an invalid pointer is returned to an object that has already been deleted. TreeNode *MyTreeClass::deleteNode(int key) { TreeNode *temp; TreeNode *back; // All the code is here to remove the node from the tree if key is found. // Temp is now pointing to NULL or the one to return. delete temp; return temp; } |
Code Requirement |
The delete function in a binary tree must return a pointer to the item removed but item to be removed has two children. |
Correct Code |
Given the following code defining the delete function in a binary tree.TreeNode *MyTreeClass::deleteNode(int key) { TreeNode *back; TreeNode *temp; TreeNode *delParent; // Parent of node to delete TreeNode *delNode; // Node to delete // All code to do the searching and removing of a node with no children or. // only a node with one child on the left or right is here. else // Case 2: Deleting node with two children { // Find the replacement value. Locate the node // containing the largest value smaller than the // key of the node being deleted. temp = delNode->left; back = delNode; while(temp->right != NULL) { back = temp; temp = temp->right; } // Create a copy of the node to be deleted TreeNode *retNode = new TreeNode(); // Create the new TreeNode TreeNode *rt, *lf; // Create temporary pointers rt = delNode->right; // Copy and save pointers from delNode lf = delNode->left; *retNode = *delNode; // Block copy delNode into retNode retNode->left = retNode->right = NULL; // Clear pointers in retNode // Copy the replacement values into the node to be deleted *delNode = *temp; delNode->left = lf; // Restore pointers in delNode delNode->right = rt; // Remove the replacement node from the tree if(back == delNode) back->left = temp->left; else back->right = temp->left; delete temp; // Delete the node actually removed from the tree. return retNode; // Return the copy of the node overwritten to remove it. }// end else 2 children }// end else at least 1 child }// end delete function |
Incorrect Code |
Error: The node that was used to overwrite the node being removed is returned instead of a copy of the node removed (overwritten). TreeNode *MyTreeClass::deleteNode(int key) { TreeNode *back; TreeNode *temp; TreeNode *delParent; // Parent of node to delete TreeNode *delNode; // Node to delete // All code to do the searching and removing of a node with no children or. // only a node with one child on the left or right is here. else // Case 2: Deleting node with two children { // Find the replacement value. Locate the node // containing the largest value smaller than the // key of the node being deleted. temp = delNode->left; back = delNode; while(temp->right != NULL) { back = temp; temp = temp->right; } TreeNode *rt, *lf; // Create temporary pointers rt = delNode->right; // Copy and save pointers from delNode lf = delNode->left; // Copy the replacement node into the node to be deleted *delNode = *temp; delNode->left = lf; // Restore pointers in delNode delNode->right = rt; FAILED TO MAKE A COPY OF THE NODE BEFORE OVERWRITING // Remove the replacement node from the tree if(back == delNode) back->left = temp->left; else back->right = temp->left; return temp; // Return the node removed from the tree. RETURNING THE WRONG NODE. THIS WAS USED TO OVERWRITE THE ONE REMOVED. }// end else 2 children }// end else at least 1 child }// end delete function |
Code Requirement |
A function must call a remove function in a linked list or a binary tree and return to its' caller what that function returned or return NULL if nothing was found to remove. |
Correct Code |
Given the following code defining the function which must call delete in a
linked list or binary tree. The pointer m_pCollection points to the
class defining a linked list or binary tree.Node *SomeClass::removeNode(int key) { return m_pCollection->delete(key);// Return whatever is returned by calling delete. } |
Incorrect Code |
Error: After the call to the delete function in the if statement the node is already removed from the collection. Node *SomeClass::removeNode(int key) { if(m_pCollection->delete(key) != NULL) return m_pCollection->delete(key);// Can't return anything already deleted. else return NULL; } |
|
Compiler Linker Problem |
The compiler reports that a function is an unresolved external symbol. The error may also report fatal error LNK1120: 1 unresolved externals. |
Correct Code |
Function prototype in SomeClass.h.int GetIntValue();Function definition in SomeClass.cpp int SomeClass::getIntValue() { return m_iVal; // m_iVal is an int member variable of SomeClass } |
Incorrect Code |
Error: The function has been prototyped in SomeClass.h but the code defining it is missing from
SomeClass.cpp. |
Incorrect Code |
Error: The function has been defined in SomeClass.cpp but the name of the class with two colons
is not in front of the function name, therefore the compiler does not recognize the function as part
of SomeClass even though it is defined in SomeClass.cpp. In the example below you will probably also
see m_iVal underlined in red with the error "m_iVal is undefined." It is undefined because the
function cannot see the class member variables.void getIntValue() { return m_iVal; // m_iVal is an int member variable of SomeClass } |
Compiler Linker Problem |
The compiler reports something like:
|
Correct Code |
Beginning of class definition in SomeClass.h. Note the #pragma once
#pragma once // Place #define statements here class SomeClass { // Place class member variables and function prototypes here. } |
Incorrect Code |
Error: #pragma once is missing from the top of the file.// Place #define statements here class SomeClass { // Place class member variables and function prototypes here. } |