Simple Review of code
In this pass I will do a simple review of the code you have written.
I think there are some problems with the design of your interface but I will deal with those completely separately so this part of the review is solely on the code you have written and assumes that the interface is good.
You don't need to comment why you are including header files.
#include <iostream> //Needed for std::cout and std::endl
#include <vector> //Needed for std::vector and others
Don't use macros.
#define CONSOLEWIDTH 80;
#define CONSOLEHEIGHT 25;
These macros have no scope (are all scope depending on how you look at it). Thus you are polluting the namespace. It would be preferable to use int const value. Personally I would make them static members of the class. This allows you to define them inline.
class sampleContainerInterfacing
{
static int const ConsoleWidth = 80;
static int const ConsoleHeight = 25;
The vector already has a constructor that initializes all values:
http://www.sgi.com/tech/stl/Vector.html
Also note sizes are usually defined via std::size_t as this can never be negative.
//I think there are other ways to do the following but I don't know them
SimpleContainer(int containerSize, myType defaultValue)
{
classVector.resize(containerSize);
for(int i = 0; i < containerSize; i++)
{
classVector[i] = defaultValue;
}
}
Try this:
SimpleContainer(std::size_t size, myType defaultValue)
: classVector(size, defaultValue)
{}
This function is simple enough that I would inline it.
int getSize();
Same comment as constructor. Use std::size_t to indicate a position in the container.
void setValue(int containerPosition, myType inputValue);
I am not sure I agree with your implementation of setValue(). The call to erase() removes tha value from the array. This will cause the vector to copy all the elements (above containerPosition) down one position (that could be a huge number of copies if the type is not a POD. Then you insert a value back into the location which causes all the elements (from containerPosition) to copied back up 1 position.
template<class myType>
void SimpleContainer<myType>::setValue(int containerPosition, myType inputValue)
{
classVector.erase(classVector.begin() + containerPosition);
//vector.insert() takes for its third argument a value for the number of times to
//insert the input value
int numInputCopies = 1;
classVector.insert(classVector.begin() + containerPosition, numInputCopies, inputValue);
}
Personally I would just overwrite the value in the container:
void setValue(int containerPosition, myType inputValue) {classVector[containerPosition] = inputValue;}
Personally I would also pass by (const) reference. The error was probably caused because you did not match the declaration and definition (ie you needed to add the & to both locations).
//I feel like I'm missing a reference here but <myType>& threw an error
template<class myType>
void SimpleContainer<myType>::inputEntireVector(std::vector<myType> inputVector)
{
classVector.swap(inputVector);
}
But you are using swap() on the internal array (which is a valid technique) but requires you to make the copy first so that you don't destroy the original array. What it comes down to is that you need to copy the values from the input into the array. There are two ways to do this.
- Assignment: (Pass parameter by const reference then use assignment)
- Copy and Swap: (Pass parameter by value (thus doing an implicit copy) then call swap()
Same comment as above: Parameter should be std::size_t and its simple enough to put inline.
//You have to compute the X and Y positions outside of this function if you want
//to simulate a 2D matrix
myType getValue (int containerPosition);
Sure: This is fine. But I would pass the stream the the values are printed on as a parameter (thus you are not hard coding it to std::cout).
//Prints the entire contents of the container starting from vector.begin()
//I think this will only work with ints and chars, and with ints over 9 it will
//mess up the alignment of the console view
template<class myType>
void SimpleContainer<myType>::printContainer()
{
for(int i = 0; i < classVector.size(); i++)
{
std::cout << classVector[i]; // Note no space between elements.
}
}
But if you are going to do that then you should also provide an overload of operator<<(std::ostream* stream, SimpleContainer& data) that calls this method.
Traditionally it is more usual to provide iterators that allow the user to control how much of the array they want to print. Then you can copy the whole array with:
std::copy(con.begin(), con.end(), std::ostream_iterator<type>(std::cout, ", "));