problem operator

This is a discussion on problem operator within the C++ Programming forums, part of the General Programming Boards category; i have one problem, function sort and dynamic array , program dont run, error, can you help me, find error ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    41

    problem operator

    i have one problem, function sort and dynamic array , program dont run, error, can you help me, find error and fix it. thanks very much.
    Code:
    class MyIntArray{
    public:
    	MyIntArray(int N); 
    	~MyIntArray();
    	int operator [](int i);
    	MyIntArray &operator=(const MyIntArray &A);
    	void sort();
    private:
    	int *ptr;
    	int len;
    
    };
    Code:
    #include"MyIntArray.h"
    #include"iostream"
    #include "string"
    int N =10;
    //using namespace std;
    MyIntArray::MyIntArray(int N){ // allocate array of N integers
    	ptr=new int[N];
    }
    MyIntArray::~MyIntArray(){
    	delete ptr;
    }
    int MyIntArray::operator [](int i){ 
    	return ptr[i];
    }
    
    MyIntArray &MyIntArray::operator =(const MyIntArray &A){
    //	len=(int)strlen(ptr.);
    	//A=new int[N];
    	for(int i=0;i<N;i++){
    		A[i]=ptr[i];
    	}
    	return (*this);
    }
    
    void MyIntArray:Sort(){ 
    	int tmp;
    	for(i=0;i<len;i++){
    		for(int j=i;j<N;j++){
    			if(ptr[j]>ptr[j+1]){
    				tmp=ptr[j];
    				ptr[j]=ptr[j+1];
    				ptr[j+1]=tmp;
    			}
    		}
    	
    	}
    }
    Code:
    MyIntArray IArr(5); // allocate array of 5 integers
    for (int i=0; i<5 i++)
    	cin >> IArr[i]; // input value from user
    cout << “before sorting:” << endl;
    cout << IArr << endl;
    MyIntArray IAS(1);
    IAS = IArr;
    IAS.Sort();
    cout << “after sorting:” << endl;
    cout << IAS << endl;
    Last edited by llynx; 10-19-2009 at 07:29 AM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> ptr=new int[N];

    You need to set 'len' to 'N', and stop using the global 'N' inside the class.

    >> delete ptr;

    For arrays, you need delete [].

    >> len=(int)strlen(ptr.);

    Come on, now, strlen? That's for null-terminated char arrays. Just use the 'len' member of 'A' to get the new size (don't forget to delete the old data, either).

    >> void MyIntArray:Sort()

    Bubble sort.

    >> ~MyIntArray();

    Just for good measure, make this virtual, in case someone tries to derive from MyIntArray (to ensure that the destructor gets called).

    Also, you need to either define the copy constructor (internally, it can simply call operator =, but just be sure to initialize your pointer to 0 first), otherwise, make it private.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by Sebastiani
    Just for good measure, make this virtual, in case someone tries to derive from MyIntArray (to ensure that the destructor gets called).
    I do not agree: the class has no virtual functions otherwise, and there is nothing that suggests that it was intended to be a base class.

    Speaking of implementing the copy constructor: you may find that it is easier to implement the copy constructor, and then implement the copy assignment operator using the copy constructor, with the help of a swap function that just swaps the length and pointer to int (you also get hidden help from your destructor).
    Last edited by laserlight; 10-19-2009 at 08:15 AM.
    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

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    thaks Sebastiani
    Code:
    Just use the 'len' member of 'A' to get the new size (don't forget to delete the old data, either).?
    i still vague.
    Sebastiani , can talk detail it ? or give example. thanks

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Sebastiani's point is that the object that you are copying from already knows the size of its array segment that is in use. If you were storing a null terminated string instead, then you could indeed use strlen(), but it would still be unnecessary since the size is already known.
    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

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    you can fix it help u? thanks
    Code:
    Sebastiani's point is that the object that you are copying from already knows the size of its array segment that is in use. If you were storing a null terminated string instead, then you could indeed use strlen(), but it would still be unnecessary since the size is already known.
    i simple want understand it if it run true.
    thanks laserlight

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by llynx
    i simple want understand it if it run true.
    It is just a matter of:
    Code:
    len = A.len;
    Notice that I am assigning A.len to len, not len to A.len. You have a similiar problem when you try to copy the elements of the array over. But you completely forgot to destroy the old array's elements. To do this correctly, you need a temporary pointer to hold the new array... but frankly, I suggest that you just implement the copy constructor and a member swap function first, then we'll show you how to implement the copy assignment operator.
    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

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    thanks laserlight , but i think, i have one swap is true.?

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    i have constructor, how do you understand about it?
    Code:
    MyIntArray::MyIntArray(int N){ // allocate array of N integers
    	len=N;
    	ptr=new int[len];
    }
    but when i run it, the output address, after sort();

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by llynx View Post
    thanks laserlight , but i think, i have one swap is true.?
    Your statement doesn't make much sense. If English is not your native language, that's fine, but just be sure to use as many words as necessary to communicate the general idea clearly.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by llynx View Post
    i have constructor, how do you understand about it?
    Code:
    MyIntArray::MyIntArray(int N){ // allocate array of N integers
        len=N;
        ptr=new int[len];
    }
    but when i run it, the output address, after sort();
    Your constructor looks fine. I'm not sure what you mean in the following sentence, though.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    thanks Sebastiani, after i fix it, but the output is address after sort();
    you can help me ? thanks very much.
    Code:
    #include"MyIntArray.h"
    #include"iostream"
    #include "string"
    int N=10;
    //using namespace std;
    MyIntArray::MyIntArray(int N){ // allocate array of N integers
    	len=N;
    	ptr=new int[len];
    }
    MyIntArray::~MyIntArray(){
    	delete []ptr;
    }
    int &MyIntArray::operator [](int i){ 
    	return ptr[i];
    }
    
    MyIntArray &MyIntArray::operator =(const MyIntArray &A){
    	//A.len=;
    	len=A.len;
    	ptr=new int[A.len];
    	for(int i=0;i<len;i++){
    		A.ptr[i]=ptr[i];
    	}
    	return (*this);
    }
    
    void swap(int *x, int *y)
    {
         int temp;
         temp = *x;
         *x = *y;
         *y = temp;
    }
    
    int compare(int x, int y)
    {
         return(x > y);
    }
    
    void MyIntArray::Sort(){ 
    for(int i = 0; i <=len; i++)
         {
              for(int j = 0;j< len; j++)
              {
                   if(compare(ptr[j], ptr[j+1]))
                      swap(&ptr[j], &ptr[j+1]);
              }
       }
    }

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    this is void main()
    Code:
    #include"MyIntArray.h"
    #include"iostream"
    using namespace std;
    void main(){
    	
    MyIntArray IArr(5); // allocate array of 5 integers
    for (int i=0; i<5 ;i++)
    	cin >> IArr[i]; // input value from user
    	cout << "before sorting:" << endl;
    	//cout << IArr << endl;
    	for(int i=0;i<5;i++){
    		cout<<IArr[i]<<" ";
    	}
    	MyIntArray IAS(5);
    	IAS = IArr;
    	IAS.Sort();
    	cout << "after sorting:" << endl;
    	for(int i=0;i<5;i++){
    		cout<<IAS[i]<<" ";
    	}
    	//for(int i=0)
    }
    http://upanh.com/uploads/19-Sep-2009...ayq8y6css8.png

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    why dont you help me?

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    41
    thanks Sebastiani &laserlight very much

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two dot operator problem HELP!
    By Amyaayaa in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2008, 10:45 AM
  2. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  3. Serializing problem.. (can't use >> operator)
    By RancidWannaRiot in forum Windows Programming
    Replies: 2
    Last Post: 10-29-2005, 11:10 AM
  4. Weird Problem with return with operator +
    By KonArtis in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2004, 03:46 PM
  5. problem with new operator
    By codefx in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2002, 05:04 PM

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