Thread: Reading from file into a struct using a customer Vector class

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    6

    Reading from file into a struct using a customer Vector class

    Hi, so I'm fairly new to structs, and what I want to do is read data from a file into a struct. My struct uses 2 classes date and time to read in date and time values, and I've created a Custom Vector class that stores this information in a dynamic array and expands when it needs to.

    I'm strugging how to figure out how to read the data into my struct, and for now I am using a test file to test functionality. I'm getting some errors.

    Main.cpp:
    Code:
    #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;
    
    
    };
    
    
    //int i = 0;
    
    
    while(!infile.eof()){
    
    
        infile >> dTest >> tTest >> speedtest;
    
    
        windlog.add(dTest); //line 42 for error
        windlog.add(tTest); //line 43 for error
        windlog.add(speedtest); //line 44 for error
    
    
    }
    
    
    for(int i = 0; i < windlog.size(); i++){
    
    
        cout << windlog[i] << " "; //line 50 for error
    
    
    }
    
    
    
    
    infile.close();
    
    
    return 0;
    
    
    }



    Errors:
    Code:
    Line |42|error: no matching function for call to 'Vector<WindLogType>::add(Date&)'|
    
    Line |43|error: no matching function for call to 'Vector<WindLogType>::add(Time&)'|
    
    Line |44|error: no matching function for call to 'Vector<WindLogType>::add(float&)'|
    
    |50|error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'|
    Inputfile:
    31/2/2014 23:45,55.6
    My Vector add method handles any type of data because I've used it in previous exercises so I think the issue lies in how I'm using my struct. Somebody please help!
    Last edited by daffer; 05-02-2020 at 04:09 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You would need to show the interface of the Vector class template at the very least, but from the error message, the add member function probably only handles arguments that are comvertible to the template argument type of the Vector, which in this case is WindLogType
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2020
    Posts
    6
    Here is my Vector template class:
    Code:
    #ifndef VECTOR_H
    #define VECTOR_H
    #include <iostream>
    #include <string>
    #include <sstream>
    
    
    using namespace std;
    
    
    template <class T>
    class Vector
    {
        public:
            Vector(int size = 10);
            ~Vector();
    
    
            void initialize(unsigned from = 0);
            void expand();
            void add(const T &obj);
            int size() const{return this->nrofel;}
    
    
            T& operator[](const int index);
            const T& operator[](const int index) const;
    
    
    
    
        private:
            T **data;
            unsigned capacity;
            unsigned nrofel;
    
    
    };
    
    
    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);
    
    
    }
    
    
    
    
    
    
    #endif // VECTOR_H
    So what I can understand from what you're saying, should I be making another add method that takes in a Vector type as a parameter? I'm unsure

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, you should construct a WindLogType object to add.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a text file to a vector
    By satty in forum C++ Programming
    Replies: 8
    Last Post: 01-19-2011, 08:18 AM
  2. Reading from a vector to a file
    By veronicak5678 in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2008, 12:57 AM
  3. Class/Struct in a Vector...
    By yaya in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2008, 04:16 AM
  4. vector - left of .push_back must have class/struct/union type
    By patricio2626 in forum C++ Programming
    Replies: 5
    Last Post: 11-18-2006, 04:37 PM
  5. reading a customer record file
    By abbycat131 in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 09:15 PM

Tags for this Thread