CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 25

Threaded View

  1. #12
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Try to put own Class to creating by me <Template> like vector. unknow compile err

    Code:
    Please I really just want to see correction of code even without comments... Thanks in advance
    Unfortunately, you should consider that what you'll find here is exactly the opposite. Nobody will straight up correct your code. On the contrary, we'll take the time to try to teach you, and hope you'll find the problem yourself. We're not here to do/correct your homework, but we can still help.

    ----
    First: Set:

    Code:
    template <class Type> 
    void Array<Type>::SetElement(Type& type_object, int index)
    {
    	try
    	{
    		if (index >= m_size){throw 11;} 
    		m_data[index] = type_object;
    		cout << "Set Element " << type_object  << endl;
    	}
    	 catch (int x )
    	 {
    		cout << "ERROR" << x << " ignore the set, because index of element bigger then massive size"<< endl;
    	 }
    }
    What the **** is this ****? You are throwing around ints, catching them in the same method, and then squelching the error.

    If the caller made a mistake, there is nothing wrong with throwing an exception. That's the callers problem, and your responsibility is to throw an something at him:

    BTW: const correctness when passing arguments please.

    Code:
    template <class Type>
    void Array<Type>::SetElement(const Type& type_object, int index)
    {
    	if (index >= m_size || index < 0)
    		{throw std::out_of_range("Out of range error in void Array<Type>::SetElement");}
    
    	m_data[index] = type_object;
    }
    There, it's as simple as that.

    Now, the GET:
    Code:
    template <class Type> 
    const Type& Array<Type>::GetElement(int index) const
    {
    	try 
    	{
    		if (index > m_size || index < 0){ throw 1;} 
    	} 
    	catch (double x)
    	{
    	cout << "index incorrect, index too big or too small " << x << endl; 
    	}  
    	return m_data[index];
    }
    This is even more horrible.

    1) what is funny is that this does not do what you think at all. You are throwing an "int", but catching a "double". although an int is convertible to double, catches require exact matches, and will not cast for you. So basically, you won't catch your exception.

    2) Even if it did, did you notice that you are still calling "return m_data[index];". After all that, you are still doing the out of range read. Talk about useless.

    3) Why did you change the range condition? if index == m_size, it is illegal.

    Code:
    template <class Type>
    const Type& Array<Type>::GetElement(int index) const
    {
    	if (index >= m_size || index < 0)
    		{throw std::out_of_range("Out of range error in void Array<Type>::GetElement");}
        
    	return m_data[index];
    }
    now, you can try writing your main like this:

    Code:
    		int main()
    		try
    		{
    			Point *p1 = new Point (1,12);
    				cout << endl;
    			Array<Point> arr1(5);
    			arr1.SetElement(*p1,0);
    				cout << endl;
    			arr1.GetElement(16) ;
    			delete p1;
    			return 0;
    		}
    		catch(const std::exception& e)
    		{
    			std::cout << e.what() << std::endl;
    			throw;
    		}
    ----
    If you read through all of this, and are still wondering what the error is, it's not very complicated. Your compiler even tells you about it if you read his warnings.
    Last edited by monarch_dodra; June 18th, 2012 at 03:46 AM.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured