Thread: cannot convert 'int' to 'int &'

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    72

    Post cannot convert 'int' to 'int &'

    I got a small problem. I have an array class and it returns an index as a reference so it can be accessed or modified.

    Code:
    // Allows accessing and modifying the array's contents. 
    Datatype& operator[] (const int p_index) 
    {   
    	return m_array[p_index];    
    }
    This works fine. But now I want to add bound checking.

    I do this...

    Code:
    Datatype& operator[] (const int p_index) 
    {   
    	if ((p_index >= 0) && (p_index <= m_size))
    		return m_array[p_index];    
    }
    But the compiler complains about not all control paths return a value. So I do this...

    Code:
    Datatype& operator[] (const int p_index) 
    {    
    	if ((p_index >= 0) && (p_index <= m_size))
    	     return m_array[p_index];    
    	else
    	    return -1;  
    }
    I get an error saying I cannot convert an 'int' to 'int &'. I want it to return something to indicate that it's an invalid cell like a boolean somewhat. That way we can know if it was successful or not when trying to modify/access the array element. Got any idea how this can be done?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can't return -1 or any other literal value. You could have a static member and return that, but it doesn't really make much sense, especially since you are returning a non-const reference.

    I would do what vector does for operator[] and just define it as undefined behavior. Then you can return m_array[0] or m_array[m_size] just to suppress the compiler's warning and actually do something.

    You could also do what vector does for at() and throw an exception. If you program or library uses exceptions then this might be the better and safer way to go. Simply throwing std::out_of_range should be fine.

    >> if ((p_index >= 0) && (p_index <= m_size))
    BTW, that should probably be p_index < m_size, not <=. And don't forget to create a const version of the operator.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    72
    I would do what vector does for operator[] and just define it as undefined behavior. Then you can return m_array[0] or m_array[m_size] just to suppress the compiler's warning and actually do something.
    I'm not sure how vector does it. What do you mean as an undefined behavior? I don't know if this would make much sense...

    Code:
    Datatype& operator[] (const int p_index) 
    {    
    	if ((p_index >= 0) && (p_index < m_size))
    		return m_array[p_index];  
    	else 
    		return m_array[m_size-1];
    }
    I guess a Get() function would work, but it would be convienent to have it in a [] operator too so I'm not getting two differnet behaviors.
    Last edited by philvaira; 10-26-2007 at 06:59 PM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by philvaira View Post
    I'm not sure how vector does it. What do you mean as an undefined behavior? I don't know if this would make much sense...

    Code:
    Datatype& operator[] (const int p_index) 
    {    
    	if ((p_index >= 0) && (p_index < m_size))
    		return m_array[p_index];  
    	else 
    		return m_array[m_size-1];
    }
    I guess a Get() function would work, but it would be convienent to have it in a [] operator too so I'm not getting two differnet behaviors.
    That's no good either. If the size is zero then you're still accessing out of bounds.
    Best option is to change the if statement into an assert, and just always return m_array[p_index].
    Or you can use exceptions.
    Or if you really want to you can return a reference to a static Datatype.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm not sure how vector does it. What do you mean as an undefined behavior? I don't know if this would make much sense...
    In other words, do not do any bounds checking. It is up to the user of the class to ensure that only valid indices are used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM