classes & member function question...

This is a discussion on classes & member function question... within the C++ Programming forums, part of the General Programming Boards category; wrong code posted. see down! the error i get is: Code: bbb.cpp: In member function `int bbb::getSomething(int) const': bbb.cpp:261: passing ...

  1. #1
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34

    classes & member function question...

    wrong code posted. see down!

    the error i get is:

    Code:
    bbb.cpp: In member function `int bbb::getSomething(int) const':
    bbb.cpp:261: passing `const aaa' as `this' argument of `int
       aaa::getSomething()' discards qualifiers
    what exactly am i doing wrong?

    thank!
    Last edited by rox; 03-09-2004 at 05:41 PM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Just change the base-class's function signature to const (a const function isn't supposed to call a non-const function).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    sorry, i think i pasted the wrong segment of code...

    in aaa i got:
    Code:
       const char* getSomething();
    and in bbb:

    Code:
    aaa somearray[10];
    Code:
    const char* bbb::getSomething(int i) const{
    	if(i <= index)
    		
    		return somearray[i].getSomething();
    	else
    		return '\0';
    }
    Last edited by rox; 03-09-2004 at 05:42 PM.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You cannot call aaa's getsomething from an object of type bbb unless:

    • bbb is derived publicly from aaa and getsomething() is declared as public or protected.
      I.E.
      Code:
      class aaa 
      {
        public:
         aaa(void) {};
         int getsomething(void);
      };
      
      class bbb:public aaa
      {
        public:
           bbb(void) {};
      }
      
      int main(void)
      {
        bbb *mybbb=new bbb;
        int val=mybbb->getsomething();
        return(0);
      }
    • a pointer to an object of type aaa is used to call getsomething() from an object of type bbb and getsomething() is declared as a public function in aaa.
      I.E.
      Code:
      class aaa
      {
        public:
          aaa(void) {};
          int getsomething(void);
      };
      
      class bbb 
      {
        aaa *ptrtoaaa;
        public:
            bbb(aaa *myaaaa) {ptrtoaaa=myaaa;};
            aaa *GetMyAAA(void) {return ptrtoaaa;};
      };
    • bbb is actually an instantiated object of type aaa.
      I.E.
      Code:
      aaa *bbb=new aaa;
      bbb->getsomething();


    And your use of the array in bbb along with a call to aaa's getsomething() looks very suspect.

  5. #5
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    i think i understand the idea, and i think it's case 3...

    one of the data members in class bbb is an array of objects type aaa.

    i'm trying to return from a member function in bbb the return of a member function from aaa for a certain element of the array of objects type aaa.

    right?

    damn i got my brain all twisted now.

    but i'm not using pointer. in aaa the return is an string (constant), and same in bbb.
    or is it a pointer...

    ok, i've been on this for too long, i need to take a step back and think.
    Last edited by rox; 03-09-2004 at 06:10 PM.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Did you try what I suggested?

    Changing:

    >> const char* getSomething();

    to:

    >> const char* getSomething() const;

    ...should stop the compiler warning.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    yes i did, i got it to work.
    THank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. More explicit template member function hell
    By SevenThunders in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2009, 10:36 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21