Hi all,

I am new to Linux and c++ programming.

What I am trying to do is to declare an object in a .cpp and be able to access it from main.At the moment my code creates a new object in the ena.cpp and cannot use the one from the alpha.cpp. How can i access the object from alpha.cpp (bita.dio=6) from the main (ena.cpp).

Many thanks in advance.

ena.cpp
Qt Code:
  1. #include <iostream>
  2. #include "alpha.h"
  3. #include "bita.h"
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. A ob(4);
  9. B bita;//<this creates a new project which is irrelevant of the one i need to pass from alpha.cpp
  10. // i need to remove that
  11. cout << ob.get_a();
  12. cout << bita.dio;//<-- This currently outputs 2 (and not 6 as i want)
  13. //cout << par;
  14. return 0;
  15. }
To copy to clipboard, switch view to plain text mode 


alpha.cpp
Qt Code:
  1. #include "alpha.h"
  2. #include "bita.h"
  3. A::A(int x)
  4. {
  5. B bita;
  6. a = bita.ena;
  7. bita.dio = 6;//<---------- i want this to be accesible by ena.cpp directly
  8. }
  9.  
  10. int A::get_a()
  11. {
  12. return a;
  13. }
To copy to clipboard, switch view to plain text mode 


alpha.h
Qt Code:
  1. #include "bita.h"
  2. #ifndef ALPHA_H
  3. #define ALPHA_H
  4.  
  5. class A {
  6. int a;
  7. public:
  8. A(int x);
  9. int get_a();
  10. };
  11.  
  12. #endif /*ALPHA_H_*/
To copy to clipboard, switch view to plain text mode 


bita.h
Qt Code:
  1. #include "bita.h"
  2. #ifndef ALPHA_H
  3. #define ALPHA_H
  4.  
  5. class A {
  6. int a;
  7. public:
  8. A(int x);
  9. int get_a();
  10. };
  11.  
  12. #endif /*ALPHA_H_*/
To copy to clipboard, switch view to plain text mode 


bita.cpp
Qt Code:
  1. #include "bita.h"
  2.  
  3. B::B(){
  4. ena = 1;
  5. dio = 2;
  6. tria = 3;
  7. }
To copy to clipboard, switch view to plain text mode