0

I've created a struct and stored some values in it from a file. I've also created a Vector from custom class template Vector of the Struct type, and store the initial struct in this vector. Now I have 2 error messages:

|52|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'WindLogType')|

|52|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'WindLogType')|

I understand that what this means is I don't have an overloaded operator for << in my vector class template, but I don't really want one. I want to know whether it's possible to create a method that prints out the contents of the array, or whether the only option is to overload the << operator.

Main.cpp:

#include <iostream>
#include <fstream>
#include "Date.h"
#include "Time.h"
#include "Vector.h"

using namespace std;

typedef struct {

        Date d;
        Time t;
        float speed;

}WindLogType;

int main()
{

Date dTest;
Time tTest;
float speedtest = 52.5;

Vector<WindLogType> windlog;


ifstream infile("testinput.csv");

if(!infile){

    cout << "File not found.";

    return -1;

};

WindLogType windlog2;

//int i = 0;

while(!infile.eof()){

    infile >> windlog2.d >> windlog2.t >> windlog2.speed;


    windlog.add(windlog2);

}

for(int i = 0; i < windlog.size(); i++){

    cout << windlog[i] << " " << endl;

}


infile.close();

return 0;

}

Vector.h:

template <class T>
Vector<T>::Vector(int size){

    this->capacity = size;
    this->nrofel = 0;
    this->data = new T*[this->capacity];

    this->initialize();

}

template <class T>
T& Vector<T>::operator[](int index){

    if(index < 0 || index > this->nrofel){

        throw("Out of bounds");

    }

    return *this->data[index];

}

template <class T>
const T& Vector<T>::operator[](int index) const{

    if(index < 0 || index > this->nrofel){

        throw("Out of bounds");

    }

    return *this->data[index];

}

template <class T>
void Vector<T>::initialize(unsigned from){

    for(size_t i = from; i < this->capacity; i++){

        this->data[i] = nullptr;

    }

}

template <class T>
Vector<T>::~Vector(){

    for(size_t i = 0; i < capacity; i++){

        delete this->data[i];

    }
    delete[]this->data;
}


template <class T>
void Vector<T>::expand(){

    this->capacity *= 2;

    T** tempData = new T*[this->capacity];

    for(size_t i = 0; i < this->nrofel; i++){

        tempData[i] = new T(*this->data[i]);

    }

    for(size_t i = 0; i < this->nrofel; i++){

       delete this->data[i];

    }

    delete[] data;

    this->data = tempData;

    this->initialize(this->nrofel);

}

template <class T>
void Vector<T>::add(const T &obj){

    if(this->nrofel >= this->capacity){

        this->expand();

    }

    this->data[this->nrofel++] = new T(obj);

}

Any suggestions would be helpul!

thedafferg
  • 99
  • 6
  • 1
    Of course it's possible to create a function that prints the contents without an `operator< – Ted Lyngmo May 02 '20 at 11:08
  • 1
    Or it would be possible to write out the data inline, something like `cout << windlog[i].d << " " windlog[i].t << " " windlog[i].speed << " " << endl` – john May 02 '20 at 11:09
  • 2
    Sidenote: Using `while(!infile.eof())` will give you problems. [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Ted Lyngmo May 02 '20 at 11:14
  • 1
    Thank you both for the help. I tried using your approach John where I print out the variables individually, and it works just fine to my relief. Was spending ages on trying to understand this. Thank you! – thedafferg May 02 '20 at 11:20

0 Answers0