function outside of base .h file -BUG!

Is there any way you can include a function inside your .h file that is not int the base .h file ? In other words, the function is not in base .h file and it is not virtual, so you only create it inside .h file.


I get an error:




In file included from a4.cpp:7:0:
True_or_false.h: In member function ‘virtual void kk::True_or_false::print_all() const’:
True_or_false.h:79:29: error: passing ‘const kk::True_or_false’ as ‘this’ argument discards qualifiers [-fpermissive]
calculate_score();
^
compilation terminated due to -Wfatal-errors.
<builtin>: recipe for target 'a4' failed
make: *** [a4] Error 1]



Note: calculate_score() is a void function that wants to just cout in another constant function from base .h file.
Last edited on
Has calculate_score() been marked const?
No I have not marked it as const.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
namespace ok{
	class True_or_false : public a4::Question {
		private:
                     //....

		public:

			//default constructor: 
                             // ...
			void calculate_score(){
                             //...

				cout << score;
			}


      		// Returns how many marks this question is out of.
			int out_of() const{
                              //....
			}

			// returns the questions "ask text".
			string ask_test() const{
				//....
			}


			void print_all() const{
				    //....
				

			}

			~True_or_false(){}
	};
}

Well, then that's the problem. A const function can only call other const functions (on the same object).
So you mean changing the function to const will solve the proble or maybe I can include that void function in my main using an object that is the same for every other functions ?
Yes, if the problem is that you're calling calculate_score() from print_all() that will solve the problem. calculate_score() doesn't modify the object so there is no reason why it shouldn't be const.
Topic archived. No new replies allowed.