GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Jan-2005, 22:56
crq crq is offline
New Member
 
Join Date: Jan 2005
Posts: 15
crq is on a distinguished road

pointer/reference issues? ... newbie confused


i have been working on this all DAY LONG. i am new to C++ and Unix and need to write this little tiny code in both. i am tripping all over myself with references and dereferences and operator overloads .... if anyone could help me decipher what my problem(s) are, that would be a lifesaver right now.
this is the error that i keep getting ... and below is the code.

Code:
classroom(52)% g++ lab2aa.C lab2aa.C: In function `int size()': lab2aa.C:55: `d_num_elements' undeclared (first use this function) lab2aa.C:55: (Each undeclared identifier is reported only once for each function it appears in.) lab2aa.C: At global scope: lab2aa.C:59: syntax error before `&' token lab2aa.C: In member function `void IntArray::init(const int*, int)': lab2aa.C:82: no match for `IntArray& [int&]' operator lab2aa.C:85: no match for `IntArray& [int&]' operator

CPP / C++ / C Code:
#include <iostream>
using namespace std;

#include "/unc/hedlund/121/lib/cpluslib.h"

class IntArray{


public:
        IntArray ( int sz = 10 );      
 
        IntArray ( const IntArray &x);

        virtual ~IntArray();

        int size () const;
    
        int &operator = ( int i ) const;

protected:
        int d_num_elements;
        int *d_array;


private:
        void init (const int *x, int sz);
};

IntArray::
IntArray  ( int sz )
{
        cout << "!!! default constructor called " <<endl;
        init ( NULL, sz);
}

IntArray::
IntArray ( const IntArray &x )
{
        cout<< "!!!  constructor" <<endl;
        init ( x.d_array, x.size() );
}

IntArray::
~IntArray()
{
        cout << "!!! IntArray destructor " <<endl;
        if ( size() > 0){
                delete [] d_array;
        }
}

int
size ()
{
        return (d_num_elements);
}

int IntArray::
&operator [] ( int i ) const
{
        return d_array[i];
}

void IntArray::
init ( const int *data, int sz)
{
        d_num_elements = sz;
        if ( 0 == d_num_elements ){                  // if no elements in array
                d_array = NULL;
        }
        else{
                d_array = new int [d_num_elements];
                                                     // allocate memory
                                                     // if new returns NULL then no
                                                     // more available memory
        }
        for (int i=0; i < sz; i++ ){
                                            // Loop Invariant:
                                            // for all j:0 .. i-1, self[j] == 0 OR
                                            //    self[j] == data[j]
                if ( NULL == data ){
                        self [i] = 0;
                }
                else{
                        self[i] = data[i];
                }
        }
}


int main () {
        IntArray a(5);
        IntArray b(8);
  cout << a.size() <<endl;
  cout << b.size() <<endl;
return(0); 
}

thanks for looking at this!!

crq
Last edited by dsmith : 23-Jan-2005 at 07:10. Reason: Please use [c] & [/c] for posting C/C++ code
  #2  
Old 23-Jan-2005, 08:57
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by crq
CPP / C++ / C Code:
int IntArray::
&operator [] ( int i ) const
{
        return d_array[i];
}

This is wrong. You need to specify the return type before the resolution operator (:: ); the return type includes the reference operator (&):

CPP / C++ / C Code:
int & IntArray::
operator [] ( int i ) const
{
        return d_array[i];
}

I didn't follow all your code, so in case you have other errors, please let us know.

Kind regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Streamyx (DSL) and Windows ME issues. JdS Computer Software Forum - Windows 4 03-Apr-2004 05:15
Downloadable document compatibilty issues? rhino1616 Web Design Forum 3 07-May-2003 04:05

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:15.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.