Open main menu

CDOT Wiki β

Changes

C/C++ FAQ

568 bytes added, 21:16, 8 November 2012
no edit summary
function will it call when the pointer is dereferenced and why?
Q: How to redirect cerr to a file (instead of a console window)?
Q: How to visualize a multi-dimensional array? <br>Q: What is the correct way to pass a 2D array (called array) into a function? Which of the following is right: array[][], array[][COLS] or **array? Q: When creating the header file for a template class and putting the implementation in the header as well there are no errors. However when splitting the deceleration (header) and implementation (making a cpp file) for that class template there are numerous compile errors. What do all these errors mean?
</pre>
'''Q:''' Why is the postfix increment/decrement operator (e.g. a++ and a--) evaluated differently on different compilers?<br>
'''A:''' Any of the stated syntaxes are acceptable for passing a 2D array into a function. Generally the 2nd syntax (array[][COLS]) is used if the array is static and the number of cols is known. This is in order to reveal the preferred structure to the compiler, since the compiler stores a 2D array as one really long array with arrays as its elements. For example an array[3][3] would looks something like this in memory '''|''' |||| '''|''' |||| '''|''' |||| '''|''' where as we perceive the structure as a table. The other 2 syntaxes are equivalent and are generally used to pass a 2D array into a function if the structure is not know, hence for dynamic created arrays.
*Comment (pliu): Excellent question and explanation. We need to make a distinction between a STATIC 2-d array and a 2-d array that is created by DYNAMAC MEMORY ALLOCATION.<br>
<br>'''Submitted by:''' Team 6 <br><br>
----
<br><br>
'''Q:''' When creating the header file for a template class and putting the implementation in the header as well there are no errors. However when splitting the deceleration (header) and implementation (making a cpp file) for that class template there are numerous compile errors. What do all these errors mean?
<br>
*comment (pliu): Could you please post up the source code and the listing of compiler error messages please? <br>
'''A:''' When splitting the deceleration of a template class additional code must be added onto every member of that class. If one has a class called Stack and a function called add(int x) for example, then in the implementation file the display function would be defined in the scope of the Stack class by writing int Stack::add(int x). Since templates are being used the compiler must also be told that this implementation is being defined with a template. This is done by writing something like template<class T> before every function. For the example stated it would look like this:
1
edit