Basic Requirements for all Programming Assignments |
OverviewBelow is a list of basic requirements which every programming assignment must meet to be accepted. Some of these may seem obvious but they are here because some students in the past did not understand some basic principles of Object Oriented Program Design. A program not meeting all the following requirements will not be accepted. |
|
|
|
Common Programming Assignment Coding Problems |
OverviewMany students when faced with the sometimes daunting challenge of programming assignments in CS 307 find errors in their code reported by Visual Studio. Unfortunately Visual Studio is frequently somewhat cryptic about reporting just what the problem is. Below you will find some of the most common coding errors the instructor has seen over the years with the error which may be reported by Visual Studio and solutions to the problem.This page is still under construction. Check back later for updates. |
Common Coding ProblemsClick on any one of the items below to go instantly to the discussion of the problem.
|
Common Compiler/Linker ProblemsClick on any one of the items below to go instantly to the discussion of the problem.
|
Code Requirement |
Defining classes in .h and .cpp files.Every class defined in a project must have a .h file defining the class, its' member variables and its' member functions. There must also be a .cpp file in which the functions prototyped in the .h file are defined. |
Correct Code |
If two classes are needed in an application define them in separate .h and .cpp files.Object1.h file://----------------------------------------------- // File: Object1.h // Header file defining the class //----------------------------------------------- #pragma once; class Object1 { private: // Define all member variables here public: // Prototype all member functions here }; Object1.cpp file://----------------------------------------------- // File: Object 1.cpp // Implementation file defining the functions //----------------------------------------------- #include "Object1.h" //----------------------------------------------- // Default Constructor //----------------------------------------------- Object1::Object1() { // Initialize all member variagbles }; //----------------------------------------------- // Destructor //----------------------------------------------- Object1::~Object1() { // Clean up any external memory allocated by this instance }; Define other functions here including all get and set functions for the private member variablesAll other classes, like Object2 in this example, will be defined in a similar way in their own .h and .cpp files.Code in main: Objec1 *o1 = new Object1(); // Create an instance of a class Objec2 *o2 = new Object2(); // Create an instance of a class You now have an instance of each class which can be used in the program. |
Incorrect Code |
Both classes have been defined in the same file. The instructor has seen multiple
classes defined in a single header file and multiple classes defined in a .cpp file
without using a header file. This makes it very difficult to reuse classes in other
programs.//----------------------------------------------- // File: Objects.h // Header file defining the class //----------------------------------------------- #pragma once; class Object1 { private: // Define all member variables here public: // Prototype all member functions here }; class Object2 { private: // Define all member variables here public: // Prototype all member functions here }; |
Code Requirement |
In many of the programming assignments a collection of a certain class is needed, for example Jets, Vehicles, Sensors, Monitors, Bugs, etc. You must create a class to represent a single instance (Jet, Vehicle, Sensor, Monitor, Bug). Somewhere else, in another class, you can create a collection of instances of this class. |
Correct Code |
Here is a header file defining a Sensor.//----------------------------------------------- // File: Sensor.h // Header file defining a sensor object //----------------------------------------------- #pragma once; class Sensor { private: // Define all member variables here public: // Prototype all member functions here }; In the main simulation class you may find a vector of sensors like this: vector |
Incorrect Code |
Error: A vector of Sensor is placed inside the class defining a single sensor://----------------------------------------------- // File: Sensor.h // Header file defining a sensor object //----------------------------------------------- #pragma once; class Sensor { private: vectorIf this was a real sensor would you expect to be able to open it up and find inside a bunch of sensors exactly like the one you just opened? |
Code Requirement |
A data parser class is provided for each programming assignment. All the parameters to the "get data" functions in this class are pointers to variables and arrays. |
Correct Code |
Function getObjectData(char *objName, char *objType, int objID, double objDVal) is found
in the data parser. bool DataParser::getObjectData(char *objName, char *objType, int *objID, double *objDVal) { char Name[64]; char Type[64]; int ID; int DVal; // Input the object name into array Name from the data file strcpy(objName, Name); // Copy array Name into array pointed to by objName // Input the object type into array Type from the data file strcpy(objType, Type); // Copy array Type into array pointed to by objType // Input the object ID into int ID from the data file // Note: this will be input as a string from the data file then converted to an int with the // function atoi. *objID = ID; // Copy int ID into int variable pointed to by objID // Input the object double value into double DVal from the data file // Note: this will be input as a string from the data file then converted to a double with the // function atof. *objDVal = DVal; // Copy double DVal into double variable pointed to by objDVal } Code in main: char m_sName[64]; char m_sType[64]; int m_iID; int m_dDVal; DataParser *sc = new DataParser("SimData.txt"); // Create the data parser and give it the data file name. sc->getObjectData(m_sName, m_sType, &m_iID, &m_dDval);// Get all the data from the data parser // NOTE: You are really passing the starting address of the char arrays |
Incorrect Code |
Error: You have not created any place for the returned values to be stored.char *m_sName; char *m_sType; int *m_iID; int *m_dDVal; DataParser *sc = new DataParser("SimData.txt"); // Create the data parser and give it the data file name. sc->getObjectData(m_sName, m_sType, m_iID, m_dDval);// Crash and burn // NOTE: None of the pointers passed to getObjectData are really pointing to places in memory. |
Code Requirement |
Two classes must each have a pointer to the other. |
Correct Code |
Suppose the names of the classes are Class1 and Class2. In Class1.h use what is called
a Forward Definition to force the compiler to include the name of Class2 in its'
table of definitions: Code in Class1.h //--------------------------------------- // Class1.h // Definition of Class1 //--------------------------------------- #pragma once; // Any needed #include statements are here class Class2; // This is a forward definition class Class1 // This begins the definition of Class1 in Class1.h { private: Class2 *m_C2Ptr; // Define a pointer to Class2 instance // Other variable definitions are here public: Class1(); ~Class1(); // Other function prototypes are here } Code in Class2.h //--------------------------------------- // Class2.h // Definition of Class2 //--------------------------------------- #pragma once; #include "Class1.h" // Include header for the already defined Class1 // Any other needed #include statements are here class Class2 // This begins the definition of Class2 in Class2.h { private: Class1 *m_C1Ptr; // Define a pointer to Class1 instance // Other variable definitions are here public: Class2(); ~Class2(); // Other function prototypes are here } |
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"; } |
|
Common Compiler Problems |
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. } |