Метод BST inorder, ссылающийся на вектор, показывающий ошибку: объявление несовместимо - PullRequest
0 голосов
/ 10 июля 2020

Я новичок в C ++, и я кодирую программу, которая хранит данные в классе шаблона BST, поскольку это другой класс с именем log_t, который хранит переменные, я пытаюсь использовать метод inorder из класса BST для ссылки на вектор из основного, чтобы ввести значения и иметь возможность использовать его в основном классе

мой основной класс

int main(int argc, char* argv[])
{

   BST<log_t> l;

string infilename = "";

string filePath = "data\\";
string files[1] = {
    filePath + "MetData-31-3a.csv"
    //filePath + "Jan20071toDec31abcdefghijklmnopq",
    //filePath + "Jan20081toDec31abcdefghijklmnopq",
    //filePath + "MetData_Jan01-2010-Jan01-2011-ALL"

};

/// checks if file can open
for (int i = 0; i < 1; i++) {
    infilename = files[i];
    ifstream infile(infilename);

    if (!infile.is_open())
    {
        cout << "unable to read file" << files[i] << endl;
    }
    else
    {
        cout << "reading file" << files[i] << endl;
    }

    /* parse sensor data csv file */
    string tmp;
    getline(infile, tmp);  // skip the first line


    while (getline(infile, tmp))

    {
        // if successfully parsed then append into vector
        log_t logline;
        if (ParseLog(tmp, logline))
            l.insert(logline);

    }
}

cout << " end with reading file" << endl;


/* aggregate/filter logs */

Vector<log_t> vec;
l.inorder(vec);

 /* prompt menu */

// this array stores all the menu option callback functions
void(*funs[])(const Vector<log_t> & vec) = { NULL, &option1, &option2, &option3, &option4, &option5,&option6 };

// keep printing menu in loop
while (true)
{
    // prompt menu and ask user to select option
    int choice = PromptMenu();

    // check validity of choice
    if (choice < 1 || choice > 6)
    {
        cout << "invalid choice" << endl;


    }
    else
    {
        cout << endl;

        // call menu option handler
        (funs[choice])(vec);
    }

}
system("pause");
return -1;
}

мой класс BST

 #include <string>
 #include <iostream>
 #include <stream>
 #include <iomanip>
 #include <stream>
 #include "date.h"
 #include "times.h"
 #include "log_t.h"
 #include "Vector.h"

 using namespace std;

 template <class T>
 class BST {

 private:

    struct Node {
    T num;
    Node* left;
    Node* right;
    };

Node* root = NULL;
Node* insert(Node* node, T x);
Node* newnode(T num);
void removeprivate(T num, Node* parent);
T findsmallestprivate(Node* ptr);
void inorderprivate(Node* ptr, void (BST<T>::* FT)(T&), Vector<log_t>const& log);
void postorderprivate(Node* ptr, void (BST<T>::* FT)(T&));
void preorderprivate(Node* ptr, void (BST<T>::* FT)(T&));
//void inorderprivate(Node* ptr);
//void postorderprivate(Node* ptr);
//void preorderprivate(Node* ptr);
void removematch(Node* parent, Node* match, bool left);


public:
void insert(T num);
void remove(T num);
void removerootmatch();
T findsmallest();
void inorder(Vector<log_t>const& log);
void postorder();
void preorder();
void print(T& p) { cout << p << " "; };

};

template <class T>
void BST<T>::inorder(Vector<log_t>const& log) {
inorderprivate(root,print,log);

}

template <class T>
 void BST<T>::inorderprivate(Node* ptr, void (BST<T>::* FT)(T&), Vector<log_t>const& 
 log) {
 if (root != NULL)
 {

    if (ptr->left != NULL)
    {
        inorderprivate(ptr->left, FT);
    }
    (this->*FT)(log);
    log.Append( ptr->num);
    if (ptr->right != NULL)
    {
        inorderprivate(ptr->right, FT);
    
    }
   }

  else
  {
    cout << "tree is empty";
   }
  }



 

мой log_t класс тип T

 #pragma once 
  #ifndef LOG_T_H
  #define LOG_T_H

  #include <iostream>
  #include <stream>
  #include <string>
  #include <algorithm>
  #include <iomanip>
  #include <stream>
  #include "date.h"
  #include "times.h"
  #include "BST.h"

   class log_t
   {
   public:

   log_t();
   log_t(log_t& log);

   float gettemp();

   float getwind();

   float getsolar();

   void setwind(float wind);


   void setsolar(float rad);


   void settemp(float temp);

   Date date;
   Times time;


   private:


   float wind_speed;
  float solar_radiation;
  float air_temperature;

   };


   log_t::log_t()
   {
    wind_speed = 0;
    solar_radiation = 0;
    air_temperature = 0;
    }

    log_t::log_t(log_t& log) {
    wind_speed = log.wind_speed;
    solar_radiation = log.solar_radiation;
    air_temperature = log.air_temperature;
    date.SetDate(log.date.GetDay(), log.date.GetMonth(), log.date.GetYear());
    time.SetHour(log.time.GetHour());
    time.SetMinute(log.time.GetMinute());

    }

    float log_t:: gettemp()
    {
    return air_temperature;
     }

    float log_t::getwind() {
    return wind_speed;
      }

   float log_t::getsolar() {
    return solar_radiation;
    }

   void log_t::setwind(float wind)
   {
    wind_speed = wind;
    }

    void log_t::setsolar(float rad)
     {
    
    solar_radiation = rad;
    
     }

     void log_t::settemp(float temp)
     {
    
    air_temperature = temp;
     }




     #endif // LOG_T_H

мой векторный класс

      #pragma once
      #ifndef VECTOR_H
      #define VECTOR_H
      #include <iostream>
       #include <stream>
       #include <string>
    template <class T>
     class Vector
      {
         public:
          Vector();
          Vector(int capacity);
          Vector(const Vector& vec);
          Vector& operator=(const Vector& vec);
          ~Vector();

          int GetSize() const;
          void Expand();
          T& GetLast();
          void Append(const T& val);
          T& operator[](int idx);
          const T& operator[](int idx) const;

           private:
            T* elems;
            int capacity;/** < int capacity, stores the size of the array */
            int count;

            void CopyFrom(const Vector& vec);
      };


       template <class T>
       inline Vector<T>::Vector() : elems(nullptr), capacity(0), count(0)
        {
        }


        template <class T>
         inline Vector<T>::Vector(int capacity)
          : elems(new T[capacity]()), capacity(capacity), count(0)
          {
          }

          template <class T>
           inline Vector<T>::Vector(const Vector& vec)
            {
              CopyFrom(vec);
             }

         template <class T>
         inline Vector<T>& Vector<T>::operator=(const Vector& vec)
          {
           if (elems)
           delete[] elems;
           CopyFrom(vec);
           return *this;
           }


          template <class T>
          inline Vector<T>::~Vector()
          {
             if (elems)
             delete[] elems;
           }

           template <class T>
           inline int Vector<T>::GetSize() const
            {
                   return count;
             }

              template <class T>
              inline void Vector<T>::Expand()
               {
                 ++count;
                 if (count > capacity)
                  if (capacity)
                  capacity *= 2;
                   else
                  capacity = 4;
                  T* tmp = new T[capacity]();
                  for (int i = 0; i < count - 1; ++i)
                   tmp[i] = elems[I];
                   if (elems)
                   delete[] elems;
                   elems = tmp;
               }

          template <class T>
           inline T& Vector<T>::GetLast()
           {
                 return elems[count - 1];
            }

            template <class T>
             inline void Vector<T>::Append(const T& oval)
             {

                 Expand();
                 GetLast() = val;

              }

             template <class T>
             inline T& Vector<T>::operator[](int idx)
             {
               return elems[idx];
              }

           template <class T>
           inline const T& Vector<T>::operator[](int idx) const
           {
             return elems[idx];
           }

         template <class T>
         inline void Vector<T>::CopyFrom(const Vector& vec)
          {
           elems = new T[vec.capacity]();
            capacity = vec.capacity;
           count = vec.count;
           for (int i = 0; i < count; ++i)
            elems[i] = vec.elems[i];
           }

           #endif //VECTOR_H

ошибки, которые продолжают появляться:

Код серьезности Описание Ошибка состояния подавления строки файла проекта (активный) Объявление E0147 несовместимо с "void BST :: inorder (const Vector <> & log)" (объявленным в строке 241 из "C: \ Users \ Frances \ Documents \ A2 \ A2 \ BST.h") A2 C : \ Users \ Frances \ Documents \ A2 \ A2 \ BST.h 241

Код серьезности Описание Ошибка состояния подавления строки файла проекта C2065 'log_t': необъявленный идентификатор A2 C: \ Users \ Frances \ Documents \ A2 \ A2 \ BST.h 33

Описание кода серьезности Ошибка состояния подавления строки файла проекта iption C2923 'Вектор': 'log_t' не является допустимым аргументом типа шаблона для параметра 'T' A2 C: \ Users \ Frances \ Documents \ A2 \ A2 \ BST.h 33

может ли кто-нибудь помочь мне выяснить, что именно вызывает это, или что я делаю неправильно, я пытался найти ответ в течение нескольких часов и безуспешно, спасибо

...