Problem with new object from another class



  • Hi
    I am sure it´s a minor problem, but i cant find a solution to my problem.
    I am trying to declare a new object from another class and then use a method.
    The other class has an overloaded constructor and i think thats my problem.

    #pragma once
    /*!  ----------------------------------------------------------------------------
    * Name:	  				AnalyseEKG.h
    * Autor:				Ritthaler
    * Version:				1.0
    * Copyright: 			Team 3KG
    * Description:			Softwarepraktikum SoSe 2017
    *
    * Abhängige Dateien: AnalyseEKG.cpp
    * Änderungen:
    *
    * Name             | Version        |   Datum/Zeit        |Grund
    *    Ritthaler     |    1.0         |    07.05.17         |Erstellung
    *    Ritthaler     |    1.1         |    07.06.17         |Bearbeitung
    *    Ritthaler     |    1.2         |    10.06.17         |Bearbeitung
    *                  |                |                     |
    * ----------------------------------------------------------------------------*/
    #ifndef _ANALYSE_EKG_
    #define _ANALYSE_EKG_
    #include <vector>
    #include <string>
    #include <AnalyseLagetyp.h>
    #include <AnalyseSTEMI.h>
    
    using namespace std;
    
    class AnalyseEKG
    {
    
    
    
    public:
    
    	/*!
    	* Aufgabe:  Konstruktor
    	*
    	* Input:	Zweidimensionaler double-Vektor
    	*
    	* Output:  -
    	*
    	*/
    
        AnalyseEKG(std::vector<std::vector<double>> v);
    	~AnalyseEKG();
    	/*!
    	* Aufgabe: Liefert die Anzahl der R-Zacken zurück
    	*
    	* Input:	 -
    	*
    	* Output:  int 
    	*
    	*/
    	int getAnzRZacken(); 
    
    	/*!
    	* Aufgabe: liefert die Herzfrequenz zurück
    	*
    	* Input:	-
    	*
    	* Output:  int HF
    	*
    	*/
    
    	int getHF();
    
    	/*!
    	* Aufgabe: liefert den Lagetyp zurück
    	*
    	* Input:	 -
    	*
    	* Output:  string Lagetyp
    	*
    	*/
    	
        string getLagetyp();
    
    	/*!
    	* Aufgabe: liefert STEMI ja/nein zurück
    	*
    	* Input:	 -
    	*
    	* Output:  bool 
    	*
    	*/
    
    	bool getSTEMI();
    
    
    private:
       // vector<double> vAuswertung; //!Vektor mit EKG-Ableitungen: Zeit I II III aVR aVF aVL
        std::vector<std::vector< double > > vAuswertung;
    
    
    	/*!
    	* Aufgabe: Markierung der Q- und R-Zacken im EKG
    	*
    	* Input:	-
    	*
    	* Output:  -
    	*
    	*/
    	void MarkiereEKG(); 
    	
    
    
        std::vector<int> PositionQZacke;	//! Speichert Positionen der Q-Zacken
        std::vector<int> PositionRZacke;	//! Speichert Positionen der R-Zacken
    
    
    
    	
    
    };
    
    
    
    #endif //_ANALYSE_EKG___
    
    
    /*! ----------------------------------------------------------------------------
    * Name: mainwindow.h
    * Autor:    Sk...., Marcel
    *           Stangl, Florian
    *
    * Version: 0.9
    * Erstellung: 02.05.2017
    * Copyright: Team 3KG
    * Description: Softwarepraktikum SoSe 2017
    *
    * Abhängige Dateien: Befunder.cpp, Person.h, Person.cpp, EKG2File.h, EKG2File.cpp
    *                   qcustomplot.h, qcustomplot.cpp
    *
    * Aufgabe: GUI Erstellen und alle Operationen ausführen
    *
    * Änderungen:
    *
    * Name					| Version	| Datum/Zeit	|Grund
    * | | |
    * | | |
    * ----------------------------------------------------------------------------*/
    
    #pragma once
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    //! @brief Include für das QT
    #include <QMainWindow>
    //! @brief Include der abhängigen Dateien (Klassen)
    #include "befunder.h"
    #include "patient.h"
    #include "person.h"
    #include "EKG2File.h"
    #include "AnalyseEKG.h"
    #include "AnalyseLagetyp.h"
    #include "AnalyseSTEMI.h"
    //! @brief Include für Graphenausgabe
    #include <qcustomplot.h>
    #include <QVector>
    #include <vector>
    
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
    //! @brief Konstruktor
        explicit MainWindow(QWidget *parent = 0);
    //! @brief Destruktor
        ~MainWindow();
    
    //! @brief  Slots zur Verarbeitung der Befehle auf der Oberfläche
    private slots:
    
    
    private:
    
    //! @brief Objekt Vektor für den Plot
        EKG2File werteVector;
    
        
    //! @brief Objekt für die Herzfrequenz
        AnalyseEKG herzfrequenz (std::vector<std::vector<double>> hfVector); //////////////  here is the problem. he doesnt recognize "herzfrequenz"(it´s not colored red). but theres no error message here.
    //! @brief Objekt für den Lagetyp
        AnalyseEKG lagetyp; /////// here i want to do the same. he recognizes "lagetyp" (colors it red). but thats wrong, because he needs a vector.
    //! @brief Objekt für den STEMI
        //AnalyseEKG stemi;
    
    
    #endif // MAINWINDOW_H
    
    
    void MainWindow::einthoven_plotten()
    {  
    
        std::vector<std::vector<int>> hfVectors;
        graph_erstellen();
    
        do
        {
           
            plotVector = werteVector.createEKGVector(i);
            x.append(plotVector[0]);
            abl1.append(plotVector[1]);
            abl2.append(plotVector[2]);
            abl3.append(plotVector[3]);
    
            ui->Plot1->graph(0)->setData(x,abl1);
            ui->Plot1->replot();
    
            ui->Plot2->graph(0)->setData(x,abl2);
            ui->Plot2->replot();
            ui->Plot3->graph(0)->setData(x,abl3);
            ui->Plot3->replot();
    
            hfVectors = herzfrequenz.getHF(); /////// i want to use it here, but the error message says "D:\Programme\QT\MeineProjekte\3KG_3.0\mainwindow.cpp:792: Fehler: '((MainWindow*)this)->MainWindow::herzfrequenz' does not have class type
             hfVectors = herzfrequenz.getHF();
                         ^
    ////////////////////////////////
            if(plotVector[0] == max)
            {
                QApplication::processEvents();
                max = max + 1;
                min = min + 1;
    
                ui->Plot1->xAxis->setRange(min,max);
                ui->Plot1->replot();
    
                ui->Plot2->xAxis->setRange(min,max);
                ui->Plot2->replot();
    
                ui->Plot3->xAxis->setRange(min,max);
                ui->Plot3->replot();
    
            }
    
            i++;
        }while(i != 10000 || stoppVariable != 0);
    }
    

    ive marked the problem lines with ///////////

    Thanks in advance.


  • Qt Champions 2016

    @Marcel92 said in Problem with new object from another class:

    Hi, easy way around it is just to use pointer
    in mainwindows.h
    AnalyseEKG *herzfrequenz=NULL;

    then before any use, you do
    std::vector<std::vector<double>> v; // note its local.
    herzfrequenz = new AnalyseEKG (v); // give it the vector

    Alternatively you can assign such members via constructor list
    http://en.cppreference.com/w/cpp/language/initializer_list

    You can also add a second constructor taking no arguments and add a
    setVector function so you can add vector later. (before use)

    Note that it seems to copy the vector to its private variable. ( i assume its by design)
    Note2: The einthoven_plotten contains a loop that if very long, will hang the application.



  • Hey @mrjj
    Thanks for your answer.
    I tried your pointer solution

    AnalyseEKG *herzfrequenz=NULL;
    
    
        std::vector<std::vector<double>> v; // note its local.
        herzfrequenz = new AnalyseEKG (v); // give it the vector
    

    But then it gives me the error : herzfrequenz does not name a type?? I dont get it.


  • Qt Champions 2016

    @Marcel92
    Did you add
    #include "AnalyseEKG.h"
    in mainwindow.h
    so it knows the type for
    AnalyseEKG *herzfrequenz=NULL;

    also the using code is inside a member function of mainwinow?

    #include "AnalyseEKG.h"
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    void Using();
    private:
        Ui::MainWindow *ui;
    AnalyseEKG *herzfrequenz=NULL;
    };
    
    then in mainwindow.cpp
    
    MainWindow::void Using() {
     std::vector<std::vector<double>> v; // note its local.
     herzfrequenz = new AnalyseEKG (v); // give it the vector. Note you should call delete on this when finished using it
    }
    


  • Oh i wasnt using it in a member function -.-

    Thank you so much!


  • Qt Champions 2016

    @Marcel92
    Np :)

    Note that each time you call Using() , a new class is created.
    If that not what you want then use this not so pretty construct

    if (! herzfrequenz)
    herzfrequenz = new AnalyseEKG (v); // will only run once.

    but make sure it says
    AnalyseEKG *herzfrequenz=NULL; in .h file ( or nullptr)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.