C++ const "and the object has type qualifiers that are not compatible with the member











up vote
0
down vote

favorite












I'm new to C++ programming and in my OPP class we were requested to create a phone book.



Now, in the lecture the Professor said something about that if you want to make sure that your variable that is being injected to a method doesn't get changed you must put const on it.



here is my code so far.



private:
static int phoneCount;
char* name;
char* family;
int phone;
Phone* nextPhone;

public:
int compare(const Phone&other) const;
const char* getFamily();
const char* getName();


and in Phone.cpp



int Phone::compare(const Phone & other) const
{
int result = 0;
result = strcmp(this->family, other.getFamily());
if (result == 0) {
result = strcmp(this->name, other.getName);
}
return 0;
}


I keep getting "the object has type qualifiers that are not compatible with the member"
when I try to call to strcmp inside my compare function.
I know that I can just remove the const in the function declaration and it will go away, but I still doesn't understand why it's showing in the first place.



Help would be greatly appreciated.










share|improve this question


















  • 1




    As an aside, use std::string, and don't mix defining a collection of Phones with defining Phone
    – Caleth
    Nov 8 at 11:28










  • @Caleth Intrusive lists are not necessarily an antipattern (in fact they used to be extraordinarily common) though I would tend to agree that for modern production C++ it's a bit ew
    – Lightness Races in Orbit
    Nov 8 at 11:32















up vote
0
down vote

favorite












I'm new to C++ programming and in my OPP class we were requested to create a phone book.



Now, in the lecture the Professor said something about that if you want to make sure that your variable that is being injected to a method doesn't get changed you must put const on it.



here is my code so far.



private:
static int phoneCount;
char* name;
char* family;
int phone;
Phone* nextPhone;

public:
int compare(const Phone&other) const;
const char* getFamily();
const char* getName();


and in Phone.cpp



int Phone::compare(const Phone & other) const
{
int result = 0;
result = strcmp(this->family, other.getFamily());
if (result == 0) {
result = strcmp(this->name, other.getName);
}
return 0;
}


I keep getting "the object has type qualifiers that are not compatible with the member"
when I try to call to strcmp inside my compare function.
I know that I can just remove the const in the function declaration and it will go away, but I still doesn't understand why it's showing in the first place.



Help would be greatly appreciated.










share|improve this question


















  • 1




    As an aside, use std::string, and don't mix defining a collection of Phones with defining Phone
    – Caleth
    Nov 8 at 11:28










  • @Caleth Intrusive lists are not necessarily an antipattern (in fact they used to be extraordinarily common) though I would tend to agree that for modern production C++ it's a bit ew
    – Lightness Races in Orbit
    Nov 8 at 11:32













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm new to C++ programming and in my OPP class we were requested to create a phone book.



Now, in the lecture the Professor said something about that if you want to make sure that your variable that is being injected to a method doesn't get changed you must put const on it.



here is my code so far.



private:
static int phoneCount;
char* name;
char* family;
int phone;
Phone* nextPhone;

public:
int compare(const Phone&other) const;
const char* getFamily();
const char* getName();


and in Phone.cpp



int Phone::compare(const Phone & other) const
{
int result = 0;
result = strcmp(this->family, other.getFamily());
if (result == 0) {
result = strcmp(this->name, other.getName);
}
return 0;
}


I keep getting "the object has type qualifiers that are not compatible with the member"
when I try to call to strcmp inside my compare function.
I know that I can just remove the const in the function declaration and it will go away, but I still doesn't understand why it's showing in the first place.



Help would be greatly appreciated.










share|improve this question













I'm new to C++ programming and in my OPP class we were requested to create a phone book.



Now, in the lecture the Professor said something about that if you want to make sure that your variable that is being injected to a method doesn't get changed you must put const on it.



here is my code so far.



private:
static int phoneCount;
char* name;
char* family;
int phone;
Phone* nextPhone;

public:
int compare(const Phone&other) const;
const char* getFamily();
const char* getName();


and in Phone.cpp



int Phone::compare(const Phone & other) const
{
int result = 0;
result = strcmp(this->family, other.getFamily());
if (result == 0) {
result = strcmp(this->name, other.getName);
}
return 0;
}


I keep getting "the object has type qualifiers that are not compatible with the member"
when I try to call to strcmp inside my compare function.
I know that I can just remove the const in the function declaration and it will go away, but I still doesn't understand why it's showing in the first place.



Help would be greatly appreciated.







c++ const strcmp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 11:17









Yoni hodeffi

62




62








  • 1




    As an aside, use std::string, and don't mix defining a collection of Phones with defining Phone
    – Caleth
    Nov 8 at 11:28










  • @Caleth Intrusive lists are not necessarily an antipattern (in fact they used to be extraordinarily common) though I would tend to agree that for modern production C++ it's a bit ew
    – Lightness Races in Orbit
    Nov 8 at 11:32














  • 1




    As an aside, use std::string, and don't mix defining a collection of Phones with defining Phone
    – Caleth
    Nov 8 at 11:28










  • @Caleth Intrusive lists are not necessarily an antipattern (in fact they used to be extraordinarily common) though I would tend to agree that for modern production C++ it's a bit ew
    – Lightness Races in Orbit
    Nov 8 at 11:32








1




1




As an aside, use std::string, and don't mix defining a collection of Phones with defining Phone
– Caleth
Nov 8 at 11:28




As an aside, use std::string, and don't mix defining a collection of Phones with defining Phone
– Caleth
Nov 8 at 11:28












@Caleth Intrusive lists are not necessarily an antipattern (in fact they used to be extraordinarily common) though I would tend to agree that for modern production C++ it's a bit ew
– Lightness Races in Orbit
Nov 8 at 11:32




@Caleth Intrusive lists are not necessarily an antipattern (in fact they used to be extraordinarily common) though I would tend to agree that for modern production C++ it's a bit ew
– Lightness Races in Orbit
Nov 8 at 11:32












3 Answers
3






active

oldest

votes

















up vote
6
down vote













You need to add const qualifier for getters const char* getFamily() const;. This way these getters can be invoked on objects of type const Phone & that you pass into function.



Also other.getName should be other.getName().






share|improve this answer

















  • 2




    Thank you, that so obvious in retrospective
    – Yoni hodeffi
    Nov 8 at 11:23


















up vote
2
down vote













In addition to the other answers that correctly suggest const qualifying your getters, you can access the data members of other directly, avoiding those calls.



int Phone::compare(const Phone & other) const
{
int result = strcmp(family, other.family);
if (result == 0) {
result = strcmp(name, other.name);
}
return result;
}





share|improve this answer




























    up vote
    1
    down vote













    Your signature



    int Phone::compare(const Phone & other) const


    means inside that function you need to ensure you don't change the Phone instance.



    At the moment, your function calls const char* getFamily() (and getName, which you've missed the () call from). Neither of these functions are const, hence the error.



    If you mark these as const too, it will be ok.






    share|improve this answer





















      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%2f53206678%2fc-const-and-the-object-has-type-qualifiers-that-are-not-compatible-with-the-m%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      6
      down vote













      You need to add const qualifier for getters const char* getFamily() const;. This way these getters can be invoked on objects of type const Phone & that you pass into function.



      Also other.getName should be other.getName().






      share|improve this answer

















      • 2




        Thank you, that so obvious in retrospective
        – Yoni hodeffi
        Nov 8 at 11:23















      up vote
      6
      down vote













      You need to add const qualifier for getters const char* getFamily() const;. This way these getters can be invoked on objects of type const Phone & that you pass into function.



      Also other.getName should be other.getName().






      share|improve this answer

















      • 2




        Thank you, that so obvious in retrospective
        – Yoni hodeffi
        Nov 8 at 11:23













      up vote
      6
      down vote










      up vote
      6
      down vote









      You need to add const qualifier for getters const char* getFamily() const;. This way these getters can be invoked on objects of type const Phone & that you pass into function.



      Also other.getName should be other.getName().






      share|improve this answer












      You need to add const qualifier for getters const char* getFamily() const;. This way these getters can be invoked on objects of type const Phone & that you pass into function.



      Also other.getName should be other.getName().







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 8 at 11:19









      VTT

      23.4k42345




      23.4k42345








      • 2




        Thank you, that so obvious in retrospective
        – Yoni hodeffi
        Nov 8 at 11:23














      • 2




        Thank you, that so obvious in retrospective
        – Yoni hodeffi
        Nov 8 at 11:23








      2




      2




      Thank you, that so obvious in retrospective
      – Yoni hodeffi
      Nov 8 at 11:23




      Thank you, that so obvious in retrospective
      – Yoni hodeffi
      Nov 8 at 11:23












      up vote
      2
      down vote













      In addition to the other answers that correctly suggest const qualifying your getters, you can access the data members of other directly, avoiding those calls.



      int Phone::compare(const Phone & other) const
      {
      int result = strcmp(family, other.family);
      if (result == 0) {
      result = strcmp(name, other.name);
      }
      return result;
      }





      share|improve this answer

























        up vote
        2
        down vote













        In addition to the other answers that correctly suggest const qualifying your getters, you can access the data members of other directly, avoiding those calls.



        int Phone::compare(const Phone & other) const
        {
        int result = strcmp(family, other.family);
        if (result == 0) {
        result = strcmp(name, other.name);
        }
        return result;
        }





        share|improve this answer























          up vote
          2
          down vote










          up vote
          2
          down vote









          In addition to the other answers that correctly suggest const qualifying your getters, you can access the data members of other directly, avoiding those calls.



          int Phone::compare(const Phone & other) const
          {
          int result = strcmp(family, other.family);
          if (result == 0) {
          result = strcmp(name, other.name);
          }
          return result;
          }





          share|improve this answer












          In addition to the other answers that correctly suggest const qualifying your getters, you can access the data members of other directly, avoiding those calls.



          int Phone::compare(const Phone & other) const
          {
          int result = strcmp(family, other.family);
          if (result == 0) {
          result = strcmp(name, other.name);
          }
          return result;
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 11:26









          Caleth

          15.5k22037




          15.5k22037






















              up vote
              1
              down vote













              Your signature



              int Phone::compare(const Phone & other) const


              means inside that function you need to ensure you don't change the Phone instance.



              At the moment, your function calls const char* getFamily() (and getName, which you've missed the () call from). Neither of these functions are const, hence the error.



              If you mark these as const too, it will be ok.






              share|improve this answer

























                up vote
                1
                down vote













                Your signature



                int Phone::compare(const Phone & other) const


                means inside that function you need to ensure you don't change the Phone instance.



                At the moment, your function calls const char* getFamily() (and getName, which you've missed the () call from). Neither of these functions are const, hence the error.



                If you mark these as const too, it will be ok.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Your signature



                  int Phone::compare(const Phone & other) const


                  means inside that function you need to ensure you don't change the Phone instance.



                  At the moment, your function calls const char* getFamily() (and getName, which you've missed the () call from). Neither of these functions are const, hence the error.



                  If you mark these as const too, it will be ok.






                  share|improve this answer












                  Your signature



                  int Phone::compare(const Phone & other) const


                  means inside that function you need to ensure you don't change the Phone instance.



                  At the moment, your function calls const char* getFamily() (and getName, which you've missed the () call from). Neither of these functions are const, hence the error.



                  If you mark these as const too, it will be ok.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 11:21









                  doctorlove

                  14.4k22950




                  14.4k22950






























                      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%2f53206678%2fc-const-and-the-object-has-type-qualifiers-that-are-not-compatible-with-the-m%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







                      這個網誌中的熱門文章

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Xamarin.form Move up view when keyboard appear

                      JBPM : POST request for execute process go wrong