Confused with function header

This is a discussion on Confused with function header within the C++ Programming forums, part of the General Programming Boards category; For an assignment, I've been given function headers and I have to write the actual functions to go along with ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    44

    Confused with function header

    For an assignment, I've been given function headers and I have to write the actual functions to go along with the headers. About half of all of the functions in the program are supposed to be written by me and the rest are provided. The problem that arises is the provided functions are dependent on the functions you have to write and so while my functions work independently, the functions that depend on my functions don't work at all. I'm guessing this has something to do with me modifying the function headers... Let me provide an example.

    For the header
    Code:
    void iej(const int&,const int&)const;
    I have to write a function that exchanges to rows (i and j) of a 2D vector (that's the implicit object in a class called Matrix). So my implicit object is called a Matrix, and I have two ints that tell me which two rows to exchange. What I don't get is how can I modify the "Matrix" if the function has a const; at the end. The void implies I cannot return anything and the const does not let me change the object.

    So basically my question is, is there any way to modify my object without changing the function header? If so could I get any starting points on what to do? Thanks.

    Here's my code (with modified header)
    Code:
    //EXCHANGES ROW 'I' AND ROW 'J'
    void Matrix :: iej(const int&i,const int&j)
    {
         assert((i <= (nRow()-1)) && (j <= (nRow()-1)));
         vector<double> TempRow(nCol());
         
         //copy Row I to a temporary variable
         //copy Row J to Row I
         //copy temporary variable to Row J
         for (int z=0; z < nCol(); z++)
             {
                  TempRow[z]=data[i][z];
                  &data[i][j]=&data[j][i];
             }
         
         
    }
    The reason I don't want to change the function headers is because the other given functions don't work if I do (and I'm not allowed to modify those)...

    Thanks
    d02

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Well you must have the i and j variables in the Matrix class as members ? By the way, your message is hard to understand, especially because you talk of "function headers" but nothing such as that exists (to my knowledge at leasts). I know of const, parameters and return value but not of headers.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    > especially because you talk of "function headers" but nothing such as that exists

    The OP obviously is talking of function prototypes. The term 'function header' exists and is applied to the function declaration portion of its definition (or if you want, to the portion of the function definition that specifies how the function is called).

    As for the initial question... I have a doubt before I can answer it... Are you or not allowed to modify the function prototype?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    The only member in the Matrix class is a 2D vector.
    And yes, I meant function prototypes, not function headers.

    As for whether I am allowed to modify function headers: No. I tried to modify them and it prevents the other given functions from working right... so I cannot modify the given function prototypes.

    TiA
    d02

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    So, I'm assuming your teacher allows you to change the function prototype, but you are not being able to due to an error.

    You have two options. It depends on what you are allowed to do from the specification of your assignment and what you have learned so far.

    1. Remove const from the function prototype.
    The reason why you are getting an error when doing this is because another member function is calling this function from within and that member function is also const. Remove its constness too and all will be well.

    2. Use mutable
    The two data members used by this function can be made mutable. If you have learned about this already and your teacher doesn't allow you to change the function prototypes, then you should go this venue. Mutable data members can be changed from inside a const function.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

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. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  4. Header file for "fix" function ?
    By Rex in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 04:42 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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