cannot convert 'int' to 'int &'

This is a discussion on cannot convert 'int' to 'int &' within the C++ Programming forums, part of the General Programming Boards category; I got a small problem. I have an array class and it returns an index as a reference so it ...

  1. #1
    Registered User philvaira's Avatar
    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,317
    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 philvaira's Avatar
    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,295
    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
    21,430
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04:26 PM

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