Thread: redefinition

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    redefinition

    this is the error that i get by running the following code.
    how do i fix it?
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define SIZE 10
    
    main(){
    
    	int values[]={5,3,7,12,9,1,10,8,4,3};
    	int i;
    	int square;
    	for (i=0; i<SIZE; i++){
    		printf ("%i  ",values[i]);}
    		printf("\n");
    
    	square=squareElements(values);
    
    	for (i=0; i<SIZE; i++){
    		printf("%i",values[i]);}
    
    	system("Pause");
    }
    void squareElements(int values[],int size){
    	int i;
    	for (i=0; i<SIZE; i++){
    		values[i]=values[i]*values[i];
    	}
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    this is the error that i get by running the following code.
    running? or compiling?
    Why not to post the exact error message and mark in some way the line where it ocured?

    You should put the function prototype before main
    Code:
    void squareElements(int values[],int size);
    update the call to mtch the prototype

    and fix you main prototype to
    Code:
    int main(void)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i did some fixing and i still get the same error
    error C2371: 'cube' : redefinition; different basic types
    error C2063: 'cube' : not a function
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    cube(int*);
    
    
    main(){
    	
    	double variable=3.43;
    	int* pVariable;
    	int cube;
    	
    	printf("Value of variable is &#37;.2lf\n",variable);
    	printf("Address of variable is %p\n",&variable);
    	
    	pVariable=&variable;
    	cube(&variable);
    
    	printf ("New value for variable is %lf\n",variable);
    	
    
    	
    	system("Pause");
    }
    
    void cube(int* pVariable){
    	printf("The number %.2lf\n",*pVariable);
    	*pVariable=*pVariable * *pVariable * *pVariable;
    	printf("The value for pVariable is %p\n",pVariable);
    }

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by goran00 View Post
    i did some fixing and i still get the same error
    error C2371: 'cube' : redefinition; different basic types
    error C2063: 'cube' : not a function
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    cube(int*);
    
    
    main(){
    	
    	double variable=3.43;
    	int* pVariable;
    	int cube;
    	
    	printf("Value of variable is &#37;.2lf\n",variable);
    	printf("Address of variable is %p\n",&variable);
    	
    	pVariable=&variable;
    	cube(&variable);
    
    	printf ("New value for variable is %lf\n",variable);
    	
    
    	
    	system("Pause");
    }
    
    void cube(int* pVariable){
    	printf("The number %.2lf\n",*pVariable);
    	*pVariable=*pVariable * *pVariable * *pVariable;
    	printf("The value for pVariable is %p\n",pVariable);
    }
    your function prototype should be : void cube(int*);

    Another problem is that you define a variable : int cube;
    Thats the same name as your function.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    thank you
    now i still do not know how to fix the error
    error C2063: 'cube' : not a function
    i thought the compiler is supposed to know to look at the bottom to find what the cube function is
    am i writing it wrong?
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    void cube(int*);
    
    
    main(){
    	
    	double variable=3.43;
    	int* pVariable;
    	int cube;
    	
    	printf("Value of variable is &#37;.2lf\n",variable);
    	printf("Address of variable is %p\n",&variable);
    	
    	pVariable=&variable;
    	cube(&variable);
    
    	printf ("New value for variable is %lf\n",variable);
    	
    
    	
    	system("Pause");
    }
    
    void cube(int* pVariable){
    	printf("The number %.2lf\n",*pVariable);
    	*pVariable=*pVariable * *pVariable * *pVariable;
    	printf("The value for pVariable is %p\n",pVariable);
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's as DaveH already said.
    Another problem is that you define a variable : int cube;
    Thats the same name as your function.
    In other words, you have a variable called cube and a function called cube. The compiler thinks you're trying to call the variable, because it has more limited scope. (It's local to main and cube() is global.)

    Call the variable or the function something else.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    1. change to : int main() {

    2. variable is a double. pVariable is int*. You have : pVariable=&variable;

    3. function cube take an int* as a parameter. You are passing &variable. variable is a double.

    4. rename your int cube variable to something else and see what happens.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM