Couldn't catch the error?

This is a discussion on Couldn't catch the error? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> const int MIN_ELEMENTS = 5; typedef int ArrayType[MIN_ELEMENTS]; typedef int ElementType; bool findSum( ArrayType myArray[], int n ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    Ankara, Turkey
    Posts
    18

    Unhappy Couldn't catch the error?

    Code:
    #include <iostream>
    
    const int MIN_ELEMENTS = 5;
    typedef int ArrayType[MIN_ELEMENTS];
    typedef int ElementType;
    
    bool findSum( ArrayType myArray[], int n )
    {
    
    	ElementType firstFiveSum = 0;
    	bool isSuccess = true;
    
    	if( n < MIN_ELEMENTS )
    	{
    		isSuccess = false;
    	}
    	else
    	{
    		for( int index = 0; MIN_ELEMENTS > index; ++index )
    		{
    			
    			if( !( myArray[index] < 0 ) )
    			{
    				firstFiveSum += (ElementType)myArray[index];
    			}
    			else
    			{
    				isSuccess = false;
    				firstFiveSum = 0;
    				break;
    			}
    		}
    	}
    
    	std::cout << firstFiveSum << "\n";
    
    	return isSuccess;
    }
    
    int main()
    {
    	const int FIRST_ARRAY_SIZE = 7;
    	const int SECOND_ARRAY_SIZE = 3;
    
    	ArrayType myFirstArray[FIRST_ARRAY_SIZE] = {1,1,1,1,1,1,1};
    	ArrayType mySecondArray[SECOND_ARRAY_SIZE] = {1,1,1};
    
    	findSum( myFirstArray, FIRST_ARRAY_SIZE );
    	
    	return 0;
    }
    I couldn't find the error, can you?

    Thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > bool findSum( ArrayType myArray[], int n )
    ArrayType is already typedef'ed to an array, so adding another [] in the prototype just makes it an array of arrays

    bool findSum( ArrayType myArray, int n )

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I think here is a possible error:

    I think you are typedef'ing 'int' twice..
    Code:
    typedef int ArrayType[MIN_ELEMENTS];
    typedef int ElementType;

    Try this and see if it works
    Code:
    typedef int* ArrayType[MIN_ELEMENTS];
    typedef int ElementType;
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Dec 2004
    Location
    Ankara, Turkey
    Posts
    18
    When I do like what Salem said, it gives an error like

    Error 1 error C2664: 'findSum' : cannot convert parameter 1 from 'ArrayType [7]' to 'int []' c:\Documents and Settings\Sarp\My Documents\Visual Studio\Projects\HomeworksDA\Exercises1_9\Exercise1 _9.cpp 66

    However, I could not get the idea of The Brain's answer, can you a bit explain it to me?

    Thanks.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Code:
    	ElementType myFirstArray[FIRST_ARRAY_SIZE] = {1,1,1,1,1,1,1};
    	ElementType mySecondArray[SECOND_ARRAY_SIZE] = {1,1,1};
    Note the type

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    You are attempting to alias the 'int' data type twice. The first time you try to define 'int' data types as an array. The second time.. you are re-aliasing 'int' to 'ElementType.'

    So by re-aliasing 'int' a second time.. you are basically undoing all of the work of your first attempt at typedef. But more importantly, array[ ] is nothing more than a pointer (which points to the first element of the array) and should be treated as such. For example, anytime you pass an array into a function.. you should have a pointer type argument in your parameter list.. this will allow you the ability to pass in your array. The same idea applies with your typedef. I think you could try typedefing an int* ArrayType pointer.. which will allow you to do stuff like this:

    Code:
    //Function Prototype
    bool findSum( ArrayType myArray, int n );

    which is basically the same thing as:
    Code:
    //Function Prototype
    bool findSum( int* myArray, int n );
    Last edited by The Brain; 12-11-2004 at 08:23 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    Registered User
    Join Date
    Dec 2004
    Location
    Ankara, Turkey
    Posts
    18
    Thank you Salem, I got it.

    It compiles successful but in run-time it gives an error like,

    Error 1 Error result 1 returned from 'C:\Program Files\Microsoft Visual Studio 8\VC\bin\mt.exe'. Project

    when I run it from Visual Studio.

    If I execute the .exe file from command prompt it works well.

    What can be the reason for this?

    Thanks.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > Error 1 Error result 1 returned from '
    Dunno - your code finishes with
    return 0;
    so I would normally expect the return result to be 0 (not 1)

    Unless that's something you've since edited out

  9. #9
    Registered User
    Join Date
    Dec 2004
    Location
    Ankara, Turkey
    Posts
    18
    This can be because of The Brain's explanation but when I do the typedef of ArrayType as a pointer to int, compiler gives me many errors and things get confusing.

    I think I need to study this typedef thing more, could you show me working example of your solution?

    Thanks.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Well this seemed OK to me
    Code:
    #include <iostream>
    
    const int MIN_ELEMENTS = 5;
    typedef int ArrayType[MIN_ELEMENTS];
    typedef int ElementType;
    
    bool findSum( ArrayType myArray, int n )
    {
    
    	ElementType firstFiveSum = 0;
    	bool isSuccess = true;
    
    	if( n < MIN_ELEMENTS )
    	{
    		isSuccess = false;
    	}
    	else
    	{
    		for( int index = 0; MIN_ELEMENTS > index; ++index )
    		{
    			
    			if( !( myArray[index] < 0 ) )
    			{
    				firstFiveSum += (ElementType)myArray[index];
    			}
    			else
    			{
    				isSuccess = false;
    				firstFiveSum = 0;
    				break;
    			}
    		}
    	}
    
    	std::cout << firstFiveSum << "\n";
    
    	return isSuccess;
    }
    
    int main()
    {
    	const int FIRST_ARRAY_SIZE = 7;
    	const int SECOND_ARRAY_SIZE = 3;
    
    	ElementType myFirstArray[FIRST_ARRAY_SIZE] = {1,1,1,1,1,1,1};
    	ElementType mySecondArray[SECOND_ARRAY_SIZE] = {1,1,1};
    
    	findSum( myFirstArray, MIN_ELEMENTS );
    	
    	return 0;
    }

  11. #11
    Registered User
    Join Date
    Dec 2004
    Location
    Ankara, Turkey
    Posts
    18
    Yes Salem, I did it like that(as you said) but compiler gave me an error as I mentioned above. Nonetheless, its executable works from command prompt without any error.

    Thank you...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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