First of all, thanks for looking at this code. I'm a novice coder trying to learn C++ on my own so any help is much appreciated.
Anyway, I really hope that the answers here are helpful to others, and I appreciate the time of anyone that posts!
#include <iostream> //Needed for std::cout and std::endl
#include <vector> //Needed for std::vector and others
//These values are arbitrary and are only used when computing the
//screen size for things like memory allocation for the Container
#define CONSOLEWIDTH 80;
#define CONSOLEHEIGHT 25;
//The purpose of this class is to have an easy way to input variables and objects into a container,
//sometimes a large number of them at a time, and then retrieve and print them easily as well.
//Templating the class allows the input and printing of ints and chars very easily
//You could input objects, but some of the functions may not work with the code as written
template <class myType>
class SimpleContainer
{
public:
//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;
}
}
//Simply looks at the classVector size and returns (used for iterating over it)
int getSize();
//The setValue function sets the value at specified container position
void setValue(int containerPosition, myType inputValue);
//I feel like I'm missing a reference here but <myType>& threw an error
void inputEntireVector(std::vector<myType> inputVector);
//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);
//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
void printContainer();
private:
std::vector<myType> classVector;
};
template<class myType>
int SimpleContainer<myType>::getSize()
{
return classVector.size();
}
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);
}
template<class myType>
void SimpleContainer<myType>::inputEntireVector(std::vector<myType> inputVector)
{
classVector.swap(inputVector);
}
template<class myType>
myType SimpleContainer<myType>::getValue(int containerPosition)
{
return classVector[containerPosition];
}
template<class myType>
void SimpleContainer<myType>::printContainer()
{
for(int i = 0; i < classVector.size(); i++)
{
std::cout << classVector[i];
}
}
//Runs some basic interactions with the Container
void sampleContainerInterfacing();
int main()
{
sampleContainerInterfacing();
return 0;
}
void sampleContainerInterfacing()
{
//Setting integers for the view width and height to input into Container
//Uses them immediately to do the math to figure out Container size
int width = CONSOLEWIDTH;
int height = CONSOLEHEIGHT;
int containerSize = width*height;
SimpleContainer<int> mySimpleContainer(containerSize, 0);
//Outputs one console screen worth of 0's
mySimpleContainer.printContainer();
std::cout << mySimpleContainer.getSize() << std::endl;
//Defining these variables to aid readability of the 2D matrix math
int position;
int posY = 5;
int posX = 7;
//The position in the container equals the width * the desired y position
//plus the desired x position, -1 because it starts counting with 0
position = width * posY + posX - 1;
mySimpleContainer.setValue(position, 5);
std::cout << mySimpleContainer.getValue(position) << std::endl;
//Now contains the input variable
mySimpleContainer.printContainer();
}
Based on recommendations below I modified the code but I'm getting the following errors.
|8|warning: friend declaration 'myType operator<<(std::ostream&, const SimpleContainer&)' declares a non-template function Note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
|88|undefined reference to `operator<<(std::ostream&, SimpleContainer const&)'
I know I'm probably making mistakes here. I spent a bunch of time trying to find out how to format this properly but nothing that I found worked. Here is the modified code:
#include <iostream>
#include <vector>
#include <stddef.h>
template<class myType>
class SimpleContainer
{
friend myType operator<<(std::ostream& stream, const SimpleContainer& object);
public:
SimpleContainer(std::size_t xDim, std::size_t yDim, myType const& defaultValue = myType())
: objectData(xDim * yDim, defaultValue)
, xSize(xDim)
, ySize(yDim)
{}
//These are overloaded operators for () for the class which allows for the syntax:
//Matrix m(2,3); // 2D array size 2,3 (default constructed).
//m(4,5) = 6; // Will modify the container version to be 6
myType& operator()(std::size_t x, std::size_t y) {return objectData[y*xSize + x];}
myType const& operator()(std::size_t x, std::size_t y) const {return objectData[y*xSize + x];}
//Simply looks at the classVector size and returns (used for iterating over it)
int getSize()
{
return objectData.size();
}
//Passing by value instead of reference since it uses swap
void inputEntireVector(std::vector<myType> inputVector)
{
objectData.swap(inputVector);
}
//Not currently functioning but does compile when not used with std::cout
void printContainer(std::ostream& stream);
private:
std::vector<myType> objectData;
std::size_t xSize;
std::size_t ySize;
};
template<class myType>
inline void SimpleContainer<myType>::printContainer(std::ostream& stream)
{
for(int i = 0; i < objectData.size(); i++)
{
stream << objectData[i];
}
// Or you can use std::copy()
// std::copy(classVector.begin(), classVector.end(),
// std::ostream_iterator<type>(stream, ""/*No Space*/));
}
template<class myType>
myType inline operator<<(std::ostream& stream, const SimpleContainer<myType>& object)
{
object.printContainer(stream);
return stream;
}
//Runs some basic interactions with the Container
void sampleContainerInterfacing();
int main()
{
sampleContainerInterfacing();
return 0;
}
void sampleContainerInterfacing()
{
//I couldn't figure out how to usefully put these inside the class
static int const ConsoleWidth = 80;
static int const ConsoleHeight = 25;
size_t width = ConsoleWidth;
size_t height = ConsoleHeight;
SimpleContainer<int> mySimpleContainer(width, height, 0);
std::cout << mySimpleContainer.getSize() << std::endl;
mySimpleContainer(0,0) = 5;
std::cout << mySimpleContainer(0,0) << std::endl;
//This is the main thing that's not working properly
std::cout << mySimpleContainer;
}
I had to use the friend function inside the class and use myType in front of the operator<< definition because otherwise the compiler gave the following errors:
|60|error: passing 'const SimpleContainer' as 'this' argument of 'void SimpleContainer::printContainer(std::ostream&) [with myType = int]' discards qualifiers
|58|error: ISO C++ forbids declaration of 'operator<<' with no type
Hopefully these aren't completely stupid questions! Thanks in advance for your time.