Tuesday 15 September 2015

c++ - Making a structure in class -


I am making my first step in learning OOP and this is the first problem I can not solve. The maximum number of maximum numbers in this class should be returned. I want to number the numbers in the private sector and in the public realm. But when I want to use variables from the struct data {} in the public area, the compiler says that the variables are not declared. Please tell me why I found these errors

  class myclass {private: structure data {int q; Int w; }; Public: Get zero (int a, int b) {struct data = {a, b}; // Here I want to pass the variable in the data stratum} int max () {// This function returns the largest number if (q> gt; w) return q; Second return; }}; C ++ is close to being synonymous in "class" and "structure" (the same thing)). The only difference is that there is a "structure" default to access "public", while a "class" misses privately.  

Once you understand it, it should be clear that what you are doing is defining all that inside your class.

  class myclass {private: // < - Not necessary, you have already said that by saying "class". Structure Data {// & lt; - This is the definition of "a class with the public:" Just here. ...}; };   

C ++ allows you to create nest class / structure definitions so you can create martial parameters or return value structures.

  class database {class result {...}; }; ... class results {class results {...}; };   

Avoid collision of namespace in these two result sections, database :: results and exam :: results instead of just "results".

However - these are just definitions. They do not show - it does not affect the external class, it means that they are not being used to add a member to the class.

Your code:

  class myclass {private: structure data {// & lt; - This is a TYPE declaration, struct myclass :: data int q; // int w; //}; // & lt; - There is no member's name here, so MacLeace does not affect itself Public: get zero (int a, int b) {struct data = {a, b}; // Here I want to pass the variable in the data stratum} int max () {// This function returns the largest number if (q> gt; w) return q; Second return; }};   

Somehow declares "myclass :: data", but member of the class does not add "type" myclass :: data. Line "structure data =" is invalid, you are trying to hand over one type of values.

This might be

  class MyClass {int m_q; Int m_w; Public: zero set (full q, int w) {m_q = q; M_w = w; } Int max () const {returns (m_q> m_w)? M_q: m_w; // or # include & lt; Algorithm & gt; And back to std :: max (m_q, m_w); }};   

You only need the Q & amp; If you are going to reuse the structural definition outside the boundaries of the classroom, then in a structure, for example in derivative or parallel classes where you want to add more than one type of object You can probably do the following, but if you do this, then this will eventually kick you to interrupt yourself:

  class MyClass {Public: Stret Data {int m_q; Int m_w; }; Private: data m_data; Zero set (int q, int w) {m_data.m_q = q; M_data.m_w = w; } Int max () const {returns (m_data.m_q> m_data.m_w)? M_data.m_q: m_data.m_w; }};   

There is a better way if this coupling of members will need to appear externally for some degree: Preclass {public: Class data {int m_q; Int m_w; Public: Data (): m_q (0), m_v (0) {} data (full q, int w): m_q (0), m_w (0) {} zero set (int q, int w) {m_q = w ; M_w = w; } Int q () const {return m_q; } Int w () const {returns m_w; } Int max () const {returns (m_q> m_w)? M_q: m_w; }; Private: data m_data; Public: MyClass (): m_data () {} // or = Default MyClass (int q, int w): m_data (q, w) {} MyClass (data and data processed): m_data (data) {} // read -The only access figure & amp; Data () cons (return m_data; } // to allow writing, e.g. For Set: Data & amp; Data () {return m_data; }};

This is a hyperactive for such a simple case, but C ++: Welcome to the boilerplate language.

No comments:

Post a Comment