Thread: How can I pass a reference from a array os a class

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    11

    Question How can I pass a reference from a array os a class

    My quastion is very simple. Imagine that you have this:

    class car
    {
    ......
    };

    void main()
    {
    car * mycars[100]

    insert_cars(mycars)
    }

    insert_cars(car & mycars)
    {
    ......
    }

    The error that my compiler gives is:
    cannot convert parameter 1 from 'class quarto *(*)[200]' to 'class quarto &' A reference that is not to 'const' cannot be bound to a non-lvalue

    What can i do forcorrect this????
    Is that C++???

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Code:
    // try changing
    insert_cars(mycars)
    // to..
    insert_cars(&mycars)
    
    // im too lazy to open my compiler so I'll just hope that that works ;)

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    You could start here

    If you have an array

    int x[10];

    to create a reference to this, you need to do this

    int (&ref)[10] = x;

    now ref is the reference to the array x
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    11

    Nop that doesn't woks

    I put that, but i get more errors, like "A reference that is not to 'const' cannot be bound to a non-lvalue".
    Should i put insert_cars(const car& maycar) ?????

    thank's anyway
    Is that C++???

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    11

    Unhappy But i don't have that

    Ok ok!! if i hav int x[10], but i don't have that, in my case i have
    int * x[10], for exemple. then whit a for, i put all the elements pointing to NULL, an i want pass the refence os the array for a function, because i want have acess to the original elements of the array!!

    More one thing, i have a pointer, because i'm using derivated classes, so i need to have a array of pointer to the base.
    Is that C++???

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    This works

    Code:
    #include <iostream>
    typedef int* INTP;
    
    INTP y[10];
    
    void fun(INTP (&x)[10]);
    
    void fun(INTP (&x )[10])
    {
    	std::cout  << "Hi " << std::endl;
    
    }
    
    int main()
    {
    	fun(y);
    	return 0;
    }
    I believe you were refering to the post by "face_master" when you said it ain't working.

    ----------------------------------------------------------------
    PM: Polymorphic OOP...... lessons well taken eh!!
    ----------------------------------------------------------------
    Last edited by shiv_tech_quest; 01-20-2003 at 06:09 AM.
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    11

    It works

    Thaks, whit that form it work's thank you!!!
    But there ren't other way, more easy?? anytime a have to creat a new fnction i have to put int_ptr (&x) [10],char_ptr (&y) [10])??
    Is that C++???

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Re: It works

    Originally posted by Nautilus
    But there ren't other way, more easy?? anytime a have to creat a new fnction i have to put int_ptr (&x) [10],char_ptr (&y) [10])??
    Nope!! Not one that I know off.....to by-pass this route.. well don't feel disheartened.... lot of C++ freaks on this board..... someone out here... might have a better way
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in pass a variable to a class
    By nima_pw in forum C# Programming
    Replies: 3
    Last Post: 06-09-2009, 07:30 AM
  2. 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
  3. Pass an array to a function by reference?
    By Loic in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2007, 11:44 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. syntax to pass a member function pointer to another class?
    By reanimated in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2003, 05:24 PM