So i'm trying to create a vec class that I can do vec math on at a later point.
Was working great until I began Implementing actual procedures on it.
Class vecn:
#include <vector>
template <typename T>
class vecn
{
public:
vecn() { }
template <typename... Args>
vecn(Args&&... args)
{
addtovector(args...);
}
friend std::ostream& operator<<(std::ostream& os, const vecn<T>& obj)
{
os << "{";
for (auto it = obj.contents.begin(); it < obj.contents.end(); it++) {
os << *it;
if (it != obj.contents.end() -1)
{
os << ",";
}
}
os << "}";
return os;
}
template<typename T>
vecn<T>& operator=(const vecn<T>& v) {
contents = v.contents;
return *this;
}
unsigned int size() const
{
return contents.size();
}
vecn<T> operator+(const vecn<T>& v1) {
vecn<T> v2();
for (unsigned int i = 0; i < size();i++)
{
v2[i] = v1[i] + this->contents[i];
}
return v2;
}
T& operator[](size_t Index)
{
if (Index > contents.size() -1)
{
contents.resize(Index + 1);
}
return contents.at(Index);
}
const T& operator[](size_t Index) const
{
return contents.at(Index);
}
private:
template <typename... Args>
void addtovector(T& first, Args&&... args)
{
addtovector(first);
addtovector(args...);
}
void addtovector(T& item)
{
contents.push_back(item);
}
std::vector<T> contents;
};
Now i'm having a problem with acceing the underling vector using the subscript operator, no matter how I design it, it never quite works. Usually resulting in a
Error C2109 subscript requires array or pointer type
From Googling the error, I'm supposed to return a pointer to the array with the subscript. which I do.
Is there something i'm missing?
main:
vecn<int> v(1,2);
vecn<int> b(3, 4, 5);
std::cout << v + b;
Expected output:
{4,6,5}