Thread: function prototype

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    function prototype

    Hi

    The following function declaration doesn't work. It takes object flowfield and returns object matrix_square.

    Code:
    matrix_square eigenvalue(flow_field, flow_field);
    
    
    matrix_square eigenvalue_plus(flow_field &ob, flow_field &ob1)
    {
    
      int i;
      matrix_square temp;
    
        for (i = 0; i < Grids; i++)
          temp.ptr[i] = ob.ptr[i];							//Element 11
        for (i = 4*Grids; i < 5*Grids; i++)
          temp.ptr[i] = ob.ptr[i - 4*Grids] + ob1.ptr[i - 4*Grids];		//Element 22
        for (i = 8*Grids; i < 9*Grids; i++)
          temp.ptr[i] = 0;								//Element 33
    
    return temp;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It's usually easiest to copy and paste in order to avoid issues like this.
    Code:
    matrix_square eigenvalue(flow_field &, flow_field &);
    ?

    Leaving the parameter name in there costs you nothing.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    I get the following error when I declare:

    Code:
    matrix_square eigenvalue(flow_field &, flow_field &);
    
    
    matrix_square eigenvalue(flow_field &ob, flow_field &ob1)
    {
    
      int i;
      matrix_square temp;
    
        for (i = 0; i < Grids; i++)
          temp.ptr[i] = ob.ptr[i];							//Element 11
        for (i = 4*Grids; i < 5*Grids; i++)
          temp.ptr[i] = ob.ptr[i - 4*Grids] + ob1.ptr[i - 4*Grids];		//Element 22
        for (i = 8*Grids; i < 9*Grids; i++)
          temp.ptr[i] = ob.ptr[i - 8*Grids] - ob1.ptr[i - 8*Grids];		//Element 33
    
    return temp;
    }
    matrix_functions.C:110: error: ‘matrix_square’ does not name a type

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    Note the declaration is from a header file while the code itself is stored in a .C file

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Make sure that matrix_square is declared before your function prototype, usually by including it's headerfile.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    Unhappy

    When I compile I type:

    g++ -o supersonic_ob supersonic_ob.C matrix_functions.C -lm

    supersonic_ob.C

    Code:
    //Declare header files
    
    #include "matrix_square.H"  //Class definition
    #include "matrix_functions.H"  //Function prototype
    
    int main(){
    
    flow_field ob, ob1;
    matrix_square b;
    
    b = eigenvalue_minus(ob, ob1);
    
    }
    matrix_function.C

    Code:
    matrix_square eigenvalue_minus(flow_field &ob, flow_field &ob1)
    {
    
      int i;
      matrix_square temp;
    
        for (i = 0; i < Grids; i++)
          temp.ptr[i] = 0;								//Element 11
        for (i = 4*Grids; i < 5*Grids; i++)
          temp.ptr[i] = 0;								//Element 22
        for (i = 8*Grids; i < 9*Grids; i++)
          temp.ptr[i] = ob.ptr[i - 8*Grids] - ob1.ptr[i - 8*Grids];		//Element 33
    
    return temp;
    }
    matrix_function.H
    Code:
    matrix_square eigenvalue_minus(flow_field &, flow_field &);
    Hope I am clear enough.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    All headers that are part of the translation unit are relevant and should be posted for any real analysis: can you also post "matrix_square.H"?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    65
    Here's matrix_square.H

    Code:
    class matrix_square { 
    
    public:
    
      int i;
      double *ptr;
    
      matrix_square(){
    
        ptr = new double[square_points];
        assert( ptr!= 0);
    
     for(i = 0; i < square_points; i++)
    
    	ptr[i] = 0;}
    
    //copy constructor
    matrix_square(const matrix_square &a){
    
      int i;
      ptr = new double[square_points];
    
      for(i = 0; i < square_points; i++)
    
        ptr[i] = a.ptr[i];
    
    }
    
      ~matrix_square(){
    
        delete [] ptr;
    }
    };

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Does matrix_function.C #include "matrix_square.H"? It should, because it uses a matrix_square. (It should also #include "matrix_functions.H".)

    Each .C file should #include any .h files that contain declarations it needs.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    65

    Thumbs up

    Thanks you guys, got it working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. call to function without prototype
    By lgbeeb in forum C Programming
    Replies: 2
    Last Post: 03-25-2003, 12:20 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM