C++: Initializing class member variable with reference to another class member variable?











up vote
0
down vote

favorite












I am attempting to make a class in c++ called to store values for a number of parameters that are organized as member variables of class 'Planet' and class 'Satellite', which I want to initialize with a reference to an instance of 'Planet'. Here I provide an example where I have a 'PlanetCatalog' class with
member variables 'Planet neptune' and a 'Satellite triton'.



class Planet {
public:

double a;

Planet() {}

void setParams( const double a_) {
a = a_;
}
};


class Satellite {
public:

double b;

Planet & planet;
Satellite( Planet & planet_):planet(planet_) { }

void setParams(const double b_) {
b = b_;
}
};

class PlanetCatalog {

public:

Planet neptune;
Satellite triton(neptune);

void setAll() {
neptune.setParams(1.);
triton.setParams(2.);
}

};


However, upon compiling I encounter the error.



error: unknown type name 'neptune' 
Satellite triton(neptune);


Is it possible to have the Planet and Satellite stored as variables of the same class as I have done here. If not, could someone suggest a better way of organizing this functionality in c++?










share|improve this question






















  • Create a constructor of your PlanetCatalog class
    – drescherjm
    Nov 9 at 20:25















up vote
0
down vote

favorite












I am attempting to make a class in c++ called to store values for a number of parameters that are organized as member variables of class 'Planet' and class 'Satellite', which I want to initialize with a reference to an instance of 'Planet'. Here I provide an example where I have a 'PlanetCatalog' class with
member variables 'Planet neptune' and a 'Satellite triton'.



class Planet {
public:

double a;

Planet() {}

void setParams( const double a_) {
a = a_;
}
};


class Satellite {
public:

double b;

Planet & planet;
Satellite( Planet & planet_):planet(planet_) { }

void setParams(const double b_) {
b = b_;
}
};

class PlanetCatalog {

public:

Planet neptune;
Satellite triton(neptune);

void setAll() {
neptune.setParams(1.);
triton.setParams(2.);
}

};


However, upon compiling I encounter the error.



error: unknown type name 'neptune' 
Satellite triton(neptune);


Is it possible to have the Planet and Satellite stored as variables of the same class as I have done here. If not, could someone suggest a better way of organizing this functionality in c++?










share|improve this question






















  • Create a constructor of your PlanetCatalog class
    – drescherjm
    Nov 9 at 20:25













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am attempting to make a class in c++ called to store values for a number of parameters that are organized as member variables of class 'Planet' and class 'Satellite', which I want to initialize with a reference to an instance of 'Planet'. Here I provide an example where I have a 'PlanetCatalog' class with
member variables 'Planet neptune' and a 'Satellite triton'.



class Planet {
public:

double a;

Planet() {}

void setParams( const double a_) {
a = a_;
}
};


class Satellite {
public:

double b;

Planet & planet;
Satellite( Planet & planet_):planet(planet_) { }

void setParams(const double b_) {
b = b_;
}
};

class PlanetCatalog {

public:

Planet neptune;
Satellite triton(neptune);

void setAll() {
neptune.setParams(1.);
triton.setParams(2.);
}

};


However, upon compiling I encounter the error.



error: unknown type name 'neptune' 
Satellite triton(neptune);


Is it possible to have the Planet and Satellite stored as variables of the same class as I have done here. If not, could someone suggest a better way of organizing this functionality in c++?










share|improve this question













I am attempting to make a class in c++ called to store values for a number of parameters that are organized as member variables of class 'Planet' and class 'Satellite', which I want to initialize with a reference to an instance of 'Planet'. Here I provide an example where I have a 'PlanetCatalog' class with
member variables 'Planet neptune' and a 'Satellite triton'.



class Planet {
public:

double a;

Planet() {}

void setParams( const double a_) {
a = a_;
}
};


class Satellite {
public:

double b;

Planet & planet;
Satellite( Planet & planet_):planet(planet_) { }

void setParams(const double b_) {
b = b_;
}
};

class PlanetCatalog {

public:

Planet neptune;
Satellite triton(neptune);

void setAll() {
neptune.setParams(1.);
triton.setParams(2.);
}

};


However, upon compiling I encounter the error.



error: unknown type name 'neptune' 
Satellite triton(neptune);


Is it possible to have the Planet and Satellite stored as variables of the same class as I have done here. If not, could someone suggest a better way of organizing this functionality in c++?







c++ constructor reference initialization






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 20:21









Sean Wahl

91




91












  • Create a constructor of your PlanetCatalog class
    – drescherjm
    Nov 9 at 20:25


















  • Create a constructor of your PlanetCatalog class
    – drescherjm
    Nov 9 at 20:25
















Create a constructor of your PlanetCatalog class
– drescherjm
Nov 9 at 20:25




Create a constructor of your PlanetCatalog class
– drescherjm
Nov 9 at 20:25












2 Answers
2






active

oldest

votes

















up vote
2
down vote













Use of parentheses for in-class initialization makes compiler treat triton as a non-static member function declaration with neptune being type of the first argument, you should use list-initialization syntax instead:



Satellite triton{neptune};


Note that there is actually no need to define PlanetCatalog constructor for this.






share|improve this answer






























    up vote
    1
    down vote













    What happened?




    class PlanetCatalog {
    public:
    ...
    Planet neptune;
    Satellite triton(neptune); //<-- Compiler sees this as a non-static member-function declaration
    ...



    Because of the context of that statement, the compiler sees it as a non-static member-function declaration and tries to find such type named neptune within the relevant namespace(s); It issues an error since it can't find it.





    Option 1: You can define a constructor that initializes triton for you in its member-initialization-list



    class PlanetCatalog {
    public:
    ...
    Planet neptune;
    Satellite triton;

    PlanetCatalog() : triton(neptune) {}
    ...


    Note: using this option, the order of your class data members matters, because the order initialization of data members is defined by their order of declaration in the class, not by the order of initialization in the member-initialization-list





    Option 2: Another straightforward solution will be to use copy-initialization



    Satellite triton = neptune;


    Option 3: or list-initialization



    Satellite triton{neptune};


    Options 2 and 3 are preferable because it forces the declaration order implicitly.






    share|improve this answer























    • I was not familiar with list-initialization, but it looks like what I want. However if I just replace the line I had with 'triton(neptune)' to 'triton{neptune}'. I encounter the following error: 'error: function definition does not declare parameters'. In the documentation it seems like the example cases are using to initialize it with the same class ( i.e. planetA{planetB} and not SatelliteA{planetB} ). Should this work for my case? I am not sure what I am missing.
      – Sean Wahl
      Nov 9 at 21:50










    • Are you using C++11? ... If you aren't using a veey recent compiler, you may need to add -std=c++11 flag in the command line invocation of your compiler; or IDE, build file, whatever... You can try more recent standards like -std=c++14 or -std=c++17 if your compiler supports it
      – WhiZTiM
      Nov 10 at 6:47











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232793%2fc-initializing-class-member-variable-with-reference-to-another-class-member-v%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    Use of parentheses for in-class initialization makes compiler treat triton as a non-static member function declaration with neptune being type of the first argument, you should use list-initialization syntax instead:



    Satellite triton{neptune};


    Note that there is actually no need to define PlanetCatalog constructor for this.






    share|improve this answer



























      up vote
      2
      down vote













      Use of parentheses for in-class initialization makes compiler treat triton as a non-static member function declaration with neptune being type of the first argument, you should use list-initialization syntax instead:



      Satellite triton{neptune};


      Note that there is actually no need to define PlanetCatalog constructor for this.






      share|improve this answer

























        up vote
        2
        down vote










        up vote
        2
        down vote









        Use of parentheses for in-class initialization makes compiler treat triton as a non-static member function declaration with neptune being type of the first argument, you should use list-initialization syntax instead:



        Satellite triton{neptune};


        Note that there is actually no need to define PlanetCatalog constructor for this.






        share|improve this answer














        Use of parentheses for in-class initialization makes compiler treat triton as a non-static member function declaration with neptune being type of the first argument, you should use list-initialization syntax instead:



        Satellite triton{neptune};


        Note that there is actually no need to define PlanetCatalog constructor for this.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 9 at 20:42

























        answered Nov 9 at 20:28









        VTT

        23.7k42345




        23.7k42345
























            up vote
            1
            down vote













            What happened?




            class PlanetCatalog {
            public:
            ...
            Planet neptune;
            Satellite triton(neptune); //<-- Compiler sees this as a non-static member-function declaration
            ...



            Because of the context of that statement, the compiler sees it as a non-static member-function declaration and tries to find such type named neptune within the relevant namespace(s); It issues an error since it can't find it.





            Option 1: You can define a constructor that initializes triton for you in its member-initialization-list



            class PlanetCatalog {
            public:
            ...
            Planet neptune;
            Satellite triton;

            PlanetCatalog() : triton(neptune) {}
            ...


            Note: using this option, the order of your class data members matters, because the order initialization of data members is defined by their order of declaration in the class, not by the order of initialization in the member-initialization-list





            Option 2: Another straightforward solution will be to use copy-initialization



            Satellite triton = neptune;


            Option 3: or list-initialization



            Satellite triton{neptune};


            Options 2 and 3 are preferable because it forces the declaration order implicitly.






            share|improve this answer























            • I was not familiar with list-initialization, but it looks like what I want. However if I just replace the line I had with 'triton(neptune)' to 'triton{neptune}'. I encounter the following error: 'error: function definition does not declare parameters'. In the documentation it seems like the example cases are using to initialize it with the same class ( i.e. planetA{planetB} and not SatelliteA{planetB} ). Should this work for my case? I am not sure what I am missing.
              – Sean Wahl
              Nov 9 at 21:50










            • Are you using C++11? ... If you aren't using a veey recent compiler, you may need to add -std=c++11 flag in the command line invocation of your compiler; or IDE, build file, whatever... You can try more recent standards like -std=c++14 or -std=c++17 if your compiler supports it
              – WhiZTiM
              Nov 10 at 6:47















            up vote
            1
            down vote













            What happened?




            class PlanetCatalog {
            public:
            ...
            Planet neptune;
            Satellite triton(neptune); //<-- Compiler sees this as a non-static member-function declaration
            ...



            Because of the context of that statement, the compiler sees it as a non-static member-function declaration and tries to find such type named neptune within the relevant namespace(s); It issues an error since it can't find it.





            Option 1: You can define a constructor that initializes triton for you in its member-initialization-list



            class PlanetCatalog {
            public:
            ...
            Planet neptune;
            Satellite triton;

            PlanetCatalog() : triton(neptune) {}
            ...


            Note: using this option, the order of your class data members matters, because the order initialization of data members is defined by their order of declaration in the class, not by the order of initialization in the member-initialization-list





            Option 2: Another straightforward solution will be to use copy-initialization



            Satellite triton = neptune;


            Option 3: or list-initialization



            Satellite triton{neptune};


            Options 2 and 3 are preferable because it forces the declaration order implicitly.






            share|improve this answer























            • I was not familiar with list-initialization, but it looks like what I want. However if I just replace the line I had with 'triton(neptune)' to 'triton{neptune}'. I encounter the following error: 'error: function definition does not declare parameters'. In the documentation it seems like the example cases are using to initialize it with the same class ( i.e. planetA{planetB} and not SatelliteA{planetB} ). Should this work for my case? I am not sure what I am missing.
              – Sean Wahl
              Nov 9 at 21:50










            • Are you using C++11? ... If you aren't using a veey recent compiler, you may need to add -std=c++11 flag in the command line invocation of your compiler; or IDE, build file, whatever... You can try more recent standards like -std=c++14 or -std=c++17 if your compiler supports it
              – WhiZTiM
              Nov 10 at 6:47













            up vote
            1
            down vote










            up vote
            1
            down vote









            What happened?




            class PlanetCatalog {
            public:
            ...
            Planet neptune;
            Satellite triton(neptune); //<-- Compiler sees this as a non-static member-function declaration
            ...



            Because of the context of that statement, the compiler sees it as a non-static member-function declaration and tries to find such type named neptune within the relevant namespace(s); It issues an error since it can't find it.





            Option 1: You can define a constructor that initializes triton for you in its member-initialization-list



            class PlanetCatalog {
            public:
            ...
            Planet neptune;
            Satellite triton;

            PlanetCatalog() : triton(neptune) {}
            ...


            Note: using this option, the order of your class data members matters, because the order initialization of data members is defined by their order of declaration in the class, not by the order of initialization in the member-initialization-list





            Option 2: Another straightforward solution will be to use copy-initialization



            Satellite triton = neptune;


            Option 3: or list-initialization



            Satellite triton{neptune};


            Options 2 and 3 are preferable because it forces the declaration order implicitly.






            share|improve this answer














            What happened?




            class PlanetCatalog {
            public:
            ...
            Planet neptune;
            Satellite triton(neptune); //<-- Compiler sees this as a non-static member-function declaration
            ...



            Because of the context of that statement, the compiler sees it as a non-static member-function declaration and tries to find such type named neptune within the relevant namespace(s); It issues an error since it can't find it.





            Option 1: You can define a constructor that initializes triton for you in its member-initialization-list



            class PlanetCatalog {
            public:
            ...
            Planet neptune;
            Satellite triton;

            PlanetCatalog() : triton(neptune) {}
            ...


            Note: using this option, the order of your class data members matters, because the order initialization of data members is defined by their order of declaration in the class, not by the order of initialization in the member-initialization-list





            Option 2: Another straightforward solution will be to use copy-initialization



            Satellite triton = neptune;


            Option 3: or list-initialization



            Satellite triton{neptune};


            Options 2 and 3 are preferable because it forces the declaration order implicitly.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 20:47

























            answered Nov 9 at 20:28









            WhiZTiM

            17.8k32850




            17.8k32850












            • I was not familiar with list-initialization, but it looks like what I want. However if I just replace the line I had with 'triton(neptune)' to 'triton{neptune}'. I encounter the following error: 'error: function definition does not declare parameters'. In the documentation it seems like the example cases are using to initialize it with the same class ( i.e. planetA{planetB} and not SatelliteA{planetB} ). Should this work for my case? I am not sure what I am missing.
              – Sean Wahl
              Nov 9 at 21:50










            • Are you using C++11? ... If you aren't using a veey recent compiler, you may need to add -std=c++11 flag in the command line invocation of your compiler; or IDE, build file, whatever... You can try more recent standards like -std=c++14 or -std=c++17 if your compiler supports it
              – WhiZTiM
              Nov 10 at 6:47


















            • I was not familiar with list-initialization, but it looks like what I want. However if I just replace the line I had with 'triton(neptune)' to 'triton{neptune}'. I encounter the following error: 'error: function definition does not declare parameters'. In the documentation it seems like the example cases are using to initialize it with the same class ( i.e. planetA{planetB} and not SatelliteA{planetB} ). Should this work for my case? I am not sure what I am missing.
              – Sean Wahl
              Nov 9 at 21:50










            • Are you using C++11? ... If you aren't using a veey recent compiler, you may need to add -std=c++11 flag in the command line invocation of your compiler; or IDE, build file, whatever... You can try more recent standards like -std=c++14 or -std=c++17 if your compiler supports it
              – WhiZTiM
              Nov 10 at 6:47
















            I was not familiar with list-initialization, but it looks like what I want. However if I just replace the line I had with 'triton(neptune)' to 'triton{neptune}'. I encounter the following error: 'error: function definition does not declare parameters'. In the documentation it seems like the example cases are using to initialize it with the same class ( i.e. planetA{planetB} and not SatelliteA{planetB} ). Should this work for my case? I am not sure what I am missing.
            – Sean Wahl
            Nov 9 at 21:50




            I was not familiar with list-initialization, but it looks like what I want. However if I just replace the line I had with 'triton(neptune)' to 'triton{neptune}'. I encounter the following error: 'error: function definition does not declare parameters'. In the documentation it seems like the example cases are using to initialize it with the same class ( i.e. planetA{planetB} and not SatelliteA{planetB} ). Should this work for my case? I am not sure what I am missing.
            – Sean Wahl
            Nov 9 at 21:50












            Are you using C++11? ... If you aren't using a veey recent compiler, you may need to add -std=c++11 flag in the command line invocation of your compiler; or IDE, build file, whatever... You can try more recent standards like -std=c++14 or -std=c++17 if your compiler supports it
            – WhiZTiM
            Nov 10 at 6:47




            Are you using C++11? ... If you aren't using a veey recent compiler, you may need to add -std=c++11 flag in the command line invocation of your compiler; or IDE, build file, whatever... You can try more recent standards like -std=c++14 or -std=c++17 if your compiler supports it
            – WhiZTiM
            Nov 10 at 6:47


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232793%2fc-initializing-class-member-variable-with-reference-to-another-class-member-v%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()