Non-aggregate type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    Non-aggregate type

    Hi,

    I have two problems.

    I have a class a called Edit that contains the following:

    Header
    Code:
    void setCharAt(int index,char ch);
    void rubout(int index) const;
    char text[];
    Problem 1:
    Now, the rubout function takes an index and it's supposed to do something with this index in the char text[]. To be more specific, it's supposed to remove the char at the index position and pack the array.

    The thing is when I try to get the length of the char tex[] I get this:

    Code:
    request for member `length' in `this->Edit::text', which is of on-aggregate type `char[0]'
    Problem 2:
    When I call the setCharAt(int index, char ch) it looks like this:
    Code:
    Edit::setCharAt(1,'v');
    The error I get for this is:

    Code:
    passing `const Edit' as `this' argument of `void Edit::setCharAt(int, char)' discards qualifiers
    What am I doing wrong?

    Cheers.
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    Its hard to tell what is going on without the actual code that is causing the problem. Please, we don't want to see all of your code, but a bit more is needed.

    #1 looks like you are treating char [] like a string. I.e. ...myEdit.text. length(). That won't work. My sugestion is to make text a std::string.

    #2 Somehow your edit object is a const and since since setCharAt is not a const method, the compiler will complain.

    Finally, rubout is const, but it sounds like it will modify the text. That won't work either.

    Comment

    • MarkoKlacar
      Recognized Expert Contributor
      • Aug 2007
      • 296

      #3
      Hi,

      Thanks for your response. As you mention, the methods rubout should not be const.

      This is probably a noob issue. Could the fact that I'm calling a non-const method from a const method have anything to do with it?

      I con provide you with more code, but I think you've gotten the problem.

      Thanks in advance....

      /M

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        text is a char array. It is not an object. There is no length member. Hence the error.

        Maybe you meant you use a string object? If so, use the size() method for the length rather than the length() method. The length() method is deprecated.

        Comment

        • MarkoKlacar
          Recognized Expert Contributor
          • Aug 2007
          • 296

          #5
          Hey guys,

          Thanks for the replies. But how do I tackle the other problem?

          From a const method call a non-const method?
          That's causing me problems...

          Cheers
          /M

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by MarkoKlacar
            From a const method call a non-const method?
            A non-const method (or any function for that matter) is one that can change variables outside the function. Like functions with pointer arguments.

            You make a function const by making the arguments const:
            [code=cpp]
            void fx(const int* arg)
            {
            *arg = 15; //ERROR. the int is const.
            }
            [/code]

            That is, the const arguments reassures the compiler that the function cannot change anything outside the scope of the function:
            [code=cpp]
            void fx(const int* arg)
            {
            cout << *arg << end;
            }
            [/code]


            This sort of const function can be called by a const member function.

            Now for a const member funciton:
            [code=cpp]
            void MyClass::AMetho d() const
            {
            fx(this->APointerMember ); //OK. fx cannot change what APoionterMember points at
            }
            [/code]


            Remember, that member functions have an invisible first argument that is the this pointer. When you call a member function that has no arguments, there is actually one supplied by the compiler: The this pointer.

            A member function can use the this pointer to chnage other class members unless the member function is const. A const member function is not allowed to change a class member.

            Therefore, from within a const member function, you can call any function that has const pointer arguments, or other const member functions. And that's all.

            Comment

            Working...