Thread: reference doesn't work?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    Unhappy reference doesn't work?

    Hi folks,

    I have the following code of which I don't understand why the reference 'q' doesn't work:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int n;
            
        cout<<"enter number of items: ";
        cin>>n;
        
        double *p = new double[n];
        double &q = *p;
            
        for (int i=0;i<n;i++) {
        q[i] = i;
        }
        
        cout<<"The first item is: "<<q[0]<<endl;
        cout<<"The last item is: "<<q[n-1]<<endl;
        
        
        delete [] p;
        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    The Bloodshed Dev C++ linker said for the line:
    Code:
     q[i] = i;
    Code:
    19 invalid types `double[int]' for array subscript
    However when I dropped the use of 'q', the program works fine. Is there anything wrong when I use a reference in such a context?

    Thanks a lot in advance...

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Don't do that.
    Last edited by siavoshkc; 08-09-2006 at 11:03 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    I tried that, but...

    it only added the following error message:
    Code:
    14 invalid initialization of reference of type 'double&' from expression of type 'double*'

    Quote Originally Posted by siavoshkc
    double &q = *p;
    should be

    double &q = p;

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    q is not array you can't use it as an array like q[index]. Use a pointer instead of a reference.

    double &q = *p; is ok
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    Therefore...

    I have no means to reference a vector except using the pointer?

    Quote Originally Posted by siavoshkc
    q is not array you can't use it as an array like q[index]. Use a pointer instead of a reference.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You could use a std::vector instead.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Why not using pointer?
    Just change
    double &q = *p;
    to
    double *q=p;

    and everything will work fine.
    Last edited by siavoshkc; 08-09-2006 at 11:22 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    actually

    it should be:
    Code:
     double *q=p;
    and everything works fine. Thanks!
    However I still don't get it: is reference operator not applicable in this context? (therefore it can only be used for referencing a single variable? That's uncomfortable...)


    Quote Originally Posted by siavoshkc
    Why not using pointer?
    Just change
    double &q = *p;
    to
    double *q=*p;

    and everything will work fine.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> That's uncomfortable.
    Why? What do you expect to be able to do with a reference that you cannot do with a pointer?

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    it should be:

    Code:
    double *q=p;
    Yes, sorry.
    However I still don't get it: is reference operator not applicable in this context? (therefore it can only be used for referencing a single variable? That's uncomfortable...)
    When you write
    double &q= *p;

    q will be exactly the same as *p. So q[n] (==*p[n]) has no meaning.

    [edit]
    People will correct me, but I think reference is used when you have a pointer but you want to see it as the type that the pointer is pointing to. It is usually used in function arguments when you want to pass by reference.
    Last edited by siavoshkc; 08-09-2006 at 11:21 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    If you really want a reference you should declare it like so:

    Code:
    double *&q = p;
    And it will work fine. q is a reference to a pointer to double.

    If you prefer a pointer you should declare it like so:

    Code:
    double *q = p
    And it will also work fine. q is a pointer to double.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    Hi

    I CAN do with a pointer; that's where the uneasiness comes from: why can't I do with a reference? What's wrong?

    Quote Originally Posted by Daved
    >> That's uncomfortable.
    Why? What do you expect to be able to do with a reference that you cannot do with a pointer?

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You can and should do it with a reference. The simple rule of thumb is "use references when you can, only use pointers when you must."
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    This

    ...looks opaque at a first glance though...

    [QUOTE=Mario F.]If you really want a reference you should declare it like so:

    Code:
    double *&q = p;
    And it will work fine. q is a reference to a pointer to double.

  15. #15
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Why you are insisting in using a reference?

    [edit]
    I am not sure about this but when we have a pointer named p;
    *p or p[0] is the value pointer is pointing to.
    p is the memory address that points to that value.
    &p is the address of pointer itself.
    But when you use a reference named q=*p.
    q is the value that p was pointing to.
    &q is the address of that value and is equal to p.
    Now we can't get the address of q itself. But there is an address.

    I mean
    Code:
    int n=2;
    int &r=n;
    Now r acts as same as n right? But it doesn't have the same type and it will take one pointer size location in memory more than n.
    Last edited by siavoshkc; 08-09-2006 at 11:43 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  4. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  5. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 12:00 AM