cancel
Showing results for 
Search instead for 
Did you mean: 

Can we declare structure in model.hpp and how to use it in model.cpp ? any syntax , example or something

ABeck.1
Associate II
 
1 ACCEPTED SOLUTION

Accepted Solutions
Martin KJELDSEN
Chief III

Just to do something simple:

//Model.hpp
#ifndef MODEL_HPP
#define MODEL_HPP
 
class ModelListener;
 
struct MyStruct
{
    int a;
    int b;
};
 
class Model
{
public:
    Model();
    void bind(ModelListener* listener)
    {
        modelListener = listener;
    }
    void tick();
protected:
    ModelListener* modelListener;
    MyStruct s;
};
#endif // MODEL_HPP

//Model.cpp
#include <gui/model/Model.hpp>
#include <gui/model/ModelListener.hpp>
 
Model::Model() : modelListener(0)
{
    s.a = 1;
    s.b = 2;
}
 
void Model::tick()
{
    modelListener->setStructValue1(s);
}

View solution in original post

16 REPLIES 16
Martin KJELDSEN
Chief III

I have no idea what you're asking :) Can you be more specific? Are you asking a general C++ question here?

/Martin

ABeck.1
Associate II

ya its general C++ question, which I am using in TouchGFX. I want to create structure in model.hpp file

 typedef struct

             {

                int16_t Test_hour;

                int16_t Test_minute;

             } LANG;

I want to use this structure in model.cpp,

How to do that?

if I write LANG.Test_hour in model.cpp file, its is throughing error, it is not recognizing LANG structure.

Now am I bet clear?

Can you post your code?

Martin KJELDSEN
Chief III

Just to do something simple:

//Model.hpp
#ifndef MODEL_HPP
#define MODEL_HPP
 
class ModelListener;
 
struct MyStruct
{
    int a;
    int b;
};
 
class Model
{
public:
    Model();
    void bind(ModelListener* listener)
    {
        modelListener = listener;
    }
    void tick();
protected:
    ModelListener* modelListener;
    MyStruct s;
};
#endif // MODEL_HPP

//Model.cpp
#include <gui/model/Model.hpp>
#include <gui/model/ModelListener.hpp>
 
Model::Model() : modelListener(0)
{
    s.a = 1;
    s.b = 2;
}
 
void Model::tick()
{
    modelListener->setStructValue1(s);
}

  1. {
  2. s.a = 1;
  3. s.b = 2;
  4. }

Like this are you defining its initial values?

as we define for any variable like this Model::Model() : modelListener(0), hour(0)

?

I want to save structure value in model so that can we used in different screens

  1. {
  2. s.a = 1;
  3. s.b = 2;
  4. }

Like this are you defining its initial values?

as we define for any variable like this Model::Model() : modelListener(0), hour(0)

?

I want to save structure value in model so that can we used in different screens

ABeck.1
Associate II

class ModelListener

{

public:

  ModelListener() : model(0) { }

  virtual ~ModelListener() {}

virtual void setStructValue1( struct s){}  // HERE IN LISTENER FUNCTION CAN I PASS STRUCT ?

protected:

  Model* model;

};

Sure you can!

../TouchGFX/gui/include/gui/model/ModelListener.hpp: In member function 'virtual void ModelListener::setStructValue1(s)':

../TouchGFX/gui/include/gui/model/ModelListener.hpp:50:46: error: '<anonymous>' has incomplete type

     virtual void setStructValue1( struct s){} //ABC

This error I am getting

I think its not identifying struct s,

How to send struct as parameter? in modelListener and view