eclipse - C++ Class Constructor Argument: How to access members and methods from another class instance without copying? -
i have 2 classes a , b. in a, have members , methods, e.g.,
header files:
a.hpp:
class { public: a(int i); virtual ~a(); int j; eigen::vector3d e; void printe(); } b.hpp:
class b { public: b(a* a_ptr); virtual ~b(); void dosomething(); private: * object; }; source files:
a.cpp:
#include <iostream> #include "a.hpp" using namespace eigen; using namespace std; a::a(int i) { j = i; } a::~a() { } a::printe() { cout<<"e = ("<<this->e(0)<<","<<this->e(1)<<","<<this->e(2)<<")"<<endl; } b.cpp:
#include <iostream> #include "a.hpp" #include "b.hpp" using namespace eigen; using namespace std; b::b(const * a_ptr) { object = a_ptr; } b::~b() { } b::dosomething() { int = 2*object->j+object->e(1); // stupid , simple example } }
in class b, access members methods of instance of a, without copying anything. thought passing pointer instance of a constructor of b , access desired members , methods.,
this code above abstraction of problem, hope point. code compiles fine (i use eclipse luna), except when try create instance of class b
#include "a.hpp" #include "b.hpp" using namespace eigen; using namespace std; int main(int argc, char **argv) { a_instance(n); a* a_ptr; a_ptr = &a_instance; b b_instance(a_ptr); // commenting line, code compiles fine return 0; } when compiling code, eclipse outputs these errors:
errors (3 items) make: *** [all] error 2 make[1]: *** [some_path.dir/all] error 2 make[2]: *** [some_other_path] error 1 which unfortunately not me lot.
my question is: best way this? maybe better use friend class or inheritance? (note a makes computations required b, otherwise not related.) or doing wrong when passing pointer argument in constructor of b?
void dosomethingb(a_ptr->a); this not valid member function declaration nor definition.
you may want write
void dosomethingb() { a_ptr->a; // want it? }
Comments
Post a Comment