Overloading = gives me an error

This is a discussion on Overloading = gives me an error within the C++ Programming forums, part of the General Programming Boards category; Code: void cSTRING::operator=(const cSTRING& str) { if (pszString) delete[] pszString; nLen=str.Length(); pszString=new char [nLen+1]; myStringCopy(pszString, str.getString()); } void main() { ...

  1. #1
    Registered User KonArtis's Avatar
    Join Date
    Mar 2003
    Posts
    34

    Overloading = gives me an error

    Code:
    void cSTRING::operator=(const cSTRING& str)
    {	
    	if (pszString)
    		delete[] pszString;
    	
    	nLen=str.Length();
    	pszString=new char [nLen+1];
    	myStringCopy(pszString, str.getString());
    }
    
    void main()
    {
    	cSTRING* pString;
    	cSTRING* pPal;
    
    	pString = new cSTRING("This is my string");
    	pPal = new cSTRING("bacon, no Cab");
    
    	pString=pPal;
    	pString->Display();
    
    	delete pString;
    	delete pPal;
    }
    *I only included relvant code*
    When I try to compile this I get these errors:
    c:\documents and settings\joe\my documents\college.lvl.2\cst8132\lab5\cstring.cpp(1 59) : error C2662: 'Length' : cannot convert 'this' pointer from 'const class cSTRING' to 'class cSTRING &'
    Conversion loses qualifiers
    c:\documents and settings\joe\my documents\college.lvl.2\cst8132\lab5\cstring.cpp(1 61) : error C2662: 'getString' : cannot convert 'this' pointer from 'const class cSTRING' to 'class cSTRING &'
    Conversion loses qualifiers

    Whats the problem with this?

    Thanks

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    The assignment operator should return a const reference to an object, not void. And main() should return an int.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by joshdick
    The assignment operator should return a const reference to an object, not void. And main() should return an int.
    While the latter is correct, and the former is considered proper (though it should return a non-const reference), they're not actually the problem here.

    The problem is that the Length() member function isn't defined to be a const member-function, yet you are using it on the const reference to a cSTRING in the operator= function.

    Length, which most-likely just returns the length of the string, is a function which won't change the state of the object, and so it has to be noted as such in order for it to work with a const object. In C++, you can call no member functions of an object when the object is const unless you explicitly lable the member functions as being const. This makes it so that when the this pointer is passed to the function, it is passed as a pointer to a const rather than a pointer to a non-const value. If you have a const object and you call a non-const function, that would mean that you'd have to convert from a pointer-to-const type to just a pointer-to-type, which is not safe (and illegal without casting in C++, which is why you get the error, and even if you cast it the behavior is usually undefined).

    To label your Length member function as being const, put const after the function declaration and function header but before the function body.

    So, change length to

    Code:
    std::size_t cSTRING::Length() const
    {
      return nLen;
    }
    (and change it appropriately in the declaration in the class definition as well).

    That should fix your problem. Try to be consistent in your use of const. If you never use it, you won't come into compile problems, but your code becomes less clear and potentially less safe. If you use it all the time, then your code is more safe and more clear. Either way you can get your code to run and compile. However, if you use const in some places (IE in your operator=) but not in other places (like your Length function), then you start running into problems like the one you just came across. Try to get into the habit of thinking about const-correctness every single time you make a function or variable. It makes everything much better in the end and saves headaches in the future.
    Last edited by Polymorphic OOP; 02-28-2004 at 11:03 AM.

  4. #4
    Registered User KonArtis's Avatar
    Join Date
    Mar 2003
    Posts
    34
    Thanks for the help!!

    I have a new problem though...When I debug this it doesn't go into my overloaded operator function. It copys the address of pPal to pString.
    Code:
    void main()
    {
    	cSTRING* pString;
    	cSTRING* pPal;
    
    	pString = new cSTRING("This is my string");
    	pPal = new cSTRING("bacon, no Cab");
    
    	pString=pPal;
    	pString->Display();
    
    	delete pString;
    	delete pPal;
    }
    Thanks

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by KonArtis
    Thanks for the help!!

    I have a new problem though...When I debug this it doesn't go into my overloaded operator function. It copys the address of pPal to pString.

    Thanks
    That's because you're using pointers and copying the address. The assignment operator is called when you copy objects, not pointers. Don't dynamically allocate your objects here -- there is no reason to.

    Code:
    int main()
    {
      cSTRING String("This is my string"), Pal("bacon, no Cab");
    
      String = Pal;
      pString.Display();
    }
    If, for some reason you wanted to use dynamic memory allocation like you were doing in your original example (which I wouldn't recommend), you can copy the object by doing

    *pString = *pPal;

    Again, though, instead of that, just work with the objects non-dynamically.


    EDIT:

    Also noting that in your example you have a memory leak and you deleted the same object twice. Both of which are very bad (latter generally worse than the former).
    Last edited by Polymorphic OOP; 02-28-2004 at 10:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM

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