Calling an object from another class
-
Hi there !
Here we go ! I have a problem trying to call an object "Qcamera *m_camera " from another class, but i'm lost.
I explain the problem : I got 4 files called Camera.cpp, Camera.h, Parameters.cpp and Parameters.h.
Inside of Camera.h (simplified a lot for the problem), i have :
Camera.h
class Camera : public QMainWindow { public: Camera(); ~Camera(); public slots: private: QCamera *m_camera; }
I use it properly in my Camera.cpp (viewfinder etc.), but i also want to use this m_camera in my Parameters.cpp, in order to do something like that :
Parameters.cpp
Parameters::Parameters() { imageProcessing = m_camera->imageProcessing(); }
Parameters.h looks like :
#ifndef PARAMETERS_H #define PARAMETERS_H #include <QMainWindow> #include "camera.h" class Parameters : public QMainWindow { public: Parameters(); ~Parameters(); private: QCameraImageProcessing *imageProcessing; };
But when i build the project, i have this message : In constructor Parameters::Parameters() 'm_camera' was not declared in this scope.
I don't understand why it don't find my QCamera object "m_camera"...Am I clear ? Can someone help the newbie I'm please ?
Have a nice day !
-
Hi,
m_camera is declared in the Camera class. So if you want to access a method or variable from that class you need to create an instance of Camera Class.
Also you have to declare it public. Private variables and methods can only be accessed by methods inside the class itself.
-
@Suji said: Your problem is that you don't have such m_camera pointer as a member of your Parameters class.
What you would need is :- add a pointer to the QCamera as a private member of Parameters
- add a getter in Camera to return the QCamera pointer
- add a setter in Parameters (a method for initialising you QCamera pointer using your Camera getter
Keep in mind, that this solution is the following up of your current design (where you have a QCamera pointer as member of Camera) but using pointers will force you to take care of the QCamera ownership.
-
Hi guys,
Thank you for your so fast answer !
I tried to understand your solutions but, I'm sorry I'm novice and I really dont understand what I have to do and moreover where I have to put it : .cpp ? .h ?
Can you explain me more precisely please... my level is really low, I'm learning basis, vocabulary etc.Thanks for your help and your patience
-
@Suji
To explain what @Charby mentioned:- provide a getter method in the Camera class:
//in camera.h class Camera { //... public: QCamera* get(); //... } //in camera.cpp QCamera *Camera::get() { return m_camera; }
- in parameters.cpp
#include "camera.h" //... Camera *cam = new Camera; QCamera *m_camera = cam->get();
You still have to initialize the pointer in camera.cpp.
(@Charby correct me if I misinterpreded your post :))
-
It is hard to answer without knowing how your Camera and Parameters are created and where the QCamera member of Camera is initialized. This is one way :
Modify you Camera class adding a getter
class Camera : public QMainWindow { public: Camera(); ~Camera(); QCamera* getCamera(){ return m_camera;} public slots: private: QCamera *m_camera; }
Modify your Parameters class so that it can be constructed using a QCamera*
class Parameters : public QMainWindow { public: Parameters(QCamera* cam){ imageProcessing = cam->m_camera->imageProcessing(); } ~Parameters(); private: QCameraImageProcessing *imageProcessing; };
and here I consider that your Camera and Parameters instances can be created on the stack and that the QCamera is initialized in the Camera constructor :
Camera* cam = new Camera(); Parameters* param = new Parameters( cam->getCamera() ); ... delete param, delete cam;
-
Thanks again @Charby and @the_ .
Why it is so complicated ? :D
I had the "camera.h" in my Parameters.cpp, so why he just dont finds "m_camera" in Parameters.cpp ?
//Parameters.cpp imageProcessing = m_camera->imageProcessing();
like he finds it in Camera.cpp when I initialize a new QCamera ?
QCamera *m_camera
Moreover, compiler dont finds "m_camera" here :
class Parameters : public QMainWindow { public: Parameters(QCamera* cam){ imageProcessing = cam->m_camera->imageProcessing(); } ~Parameters(); private: QCameraImageProcessing *imageProcessing; };
even if i followed all your steps guys...
I know it is hard for you to help me, but my code is not in English, then if i post it all, it will not help you, in my opinion.
Maybe it can ?I don't like to annoy you with my so bad level, so i think I will try to find alone... Your answer are clear, but I don't understand why it is so complicated to do something simple ! :D
-
I dont know your programming skills, but maybe you should start with a quick reading about Classes in C++ :) Try to understand how to access public, private and protected methods and variables.
I hope this link will help a little bit
C++ Classes -
Hi @the_
Yes, you're right, my problem is I learnt alone, so I have a big vocabulary problem, I know how to do things, but not how they're called, I understand the logic, but not the syntax I have to use. :) So thank you for this link, I like this summary !
Actually, my code is able to launch a windows, to obtain the availables cameras in a checklist, when you ckeck one, it is selected and you can open its stream. But now, I created a new windows called "parameters", and i want to pilot my camera's settings from this new window "parameters".
I will work with your solutions guys, thank you.
Have a nice day !
-
@Suji
You don't have a vocabulary problem, you have a problem with understanding the very basics of object-oriented programming and C++ classes.
C++ is a very complex programming language, you can't learn it just by examples. Go to the source: http://mazonka.com/shared/Straustrup4th.pdf
If you come from the world of C (i.e. if you already know C and have programmed in C) then you can go directly to "Part III " in the book, "Abstraction Mechanisms" -
Hi @gyll,
You're right, the problem is I started programming for 2 months. I knew the if, for loop etc from Matlab, but didn't know the "real" programming 2 months ago. I did C++ open classroom courses and bought a book about C.
And as you said :
"you can't learn it just by examples."I'm actually learning from examples from here and there but it's a bad way for understanding the basis.
Thanks for this source, I will study this.
Have a nice day !
-
@Suji
You're welcome.
So i understand that you also have no experience with C either. In this case, just read the whole book except "exceptions", "pointers to methods", and "templates" for now; that will give you a good understanding to start programming.