Thread: Non-lvalue in assigmnet Error?

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    92

    Non-lvalue in assigmnet Error?

    I have an object that creates a multi dimensional array.

    Object a(10) // this creates an array: temp [10][2]

    In the main function I need to access the object this way:

    a[6] = 6 * 10 // I want it to set temp[6][2] to 60

    I keep getting a "non-lvalue in assignment" error. I had to overload the [] operator and I'm returning a pointer to that position in the array.

    I've searched google for "non-lvalue in assignment" but I wasn't able to work out my problem from that. If you can help that would be great.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Also I would like to add that I have to use the syntax:

    a[6] = 6 * 10;

    So the problem is in my Class.

  3. #3
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    Why don't you post some of your code to make your question clearer. Its hard to understand what you want.

    And , as I know , lvalue == left value. Which is the left part of an assignment operation. So if we say x=y , x is an lvalue.
    Here is an lvalue error:
    Code:
    const int x=3, y=5;
    
    x = y;
    this is wrong because we can't assign to const. So a const variable can't be an lvalue...

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Colchester, Essex, United Kingdom.
    Posts
    31
    a[6] = 6 * 10 // I want it to set temp[6][2] to 60
    The element `temp[6][2]' won't exist if temp was declared as `temp[10][2]'.

    Looks to me like you need to overload the `[]' operator. You can do that like this:
    Code:
    class Object{
      int *arr;
      int size;
    
    public:
      Object():size(0),arr(0){}
      Object(const int n):size(n){
        arr = new int[n];
      }
    
      ~Object(){
        if(arr){
          delete [] arr;
          arr = 0;
        }
      }
    
      int& operator[](const int n){
        if(n < size)
          return arr[n];
      }
    };
    So now you can see how to overload the operator, try adapting my code to suit your needs.

    Note: I havn't tested this code. It was written straight into the forums text box.
    Last edited by tomcant; 03-25-2007 at 05:39 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I had to overload the [] operator and I'm returning a pointer to that position in the array.
    You should be returning a reference like tomcant's example does, not a pointer.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    I'll try returning a reference. I'm really confused about the whole reference vs pointer thing though. I'll try and figure it out. Thanks for the help.

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, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM