error: passing 'const value_type {aka const Triangle}' as 'this' argument discards qualifiers...

Okay, I should note that this is homework that is due in ~1-2 days, unlike my previous posts that have been questions for homework already past their due dates (or not school related). However, I'm not asking for code or anything. I just can't figure out what is causing the errors I'm seeing. I know the main lines causing the issue but not what is actually wrong. I've spent quite a while on this and checked this post:
https://stackoverflow.com/questions/26963510/error-passing-const-as-this-argument-of-discards-qualifiers/26963552
But still can't figure out what I'm doing wrong as I don't see any constants.

The homework assignment was to fix specific parts so the program can complete it's intended purpose.

The files I'm working with are:
main.cpp
Triangle.cpp (Contains functions for the class in Triangle.h)
Triangle.h (Contains class and function referencing (I think it's called?))

I run it using:
g++ -std=c++14 main.cpp Triangle.cpp

The errors I'm getting are:
1
2
3
4
5
6
7
main.cpp: In function ‘void writeReport(const std::vector<Triangle>&)’:
main.cpp:139:37: error: passing ‘const value_type {aka const Triangle}’ as ‘this’ argument discards qualifiers [-fpermissive]
   ts[i].getSides(side1, side2, side3);
                                     ^
In file included from main.cpp:22:0:
Triangle.h:20:7: note:   in call to ‘void Triangle::getSides(double&, double&, double&)’
  void getSides(double &side1, double &side2, double &side3);


The main code that's getting the issue is (lines 135-149 in main.cpp):

I'm trying to write to a file with a format and with data from the class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
		// FIXME7
		// Output each triangle's information as shown in the sample output.
		// Use proper formatting
		//fout << "FIXME" << endl; // comment this out when fixed
		ts[i].getSides(side1, side2, side3);
		fout << setw(5) << i << setw(8) << side1 << setw(8) << 
			side2 << setw(8) << side3 << setw(8) << "area" << 
			setw(10) << "perimeter" << setw(17) << "type" << endl;
		
		/*
		ts[i].getSides(side1, side2, side3);
		fout << setw(5) << i << setw(8) << side1 << setw(8) << 
			side2 << setw(8) << side3 << setw(8) << ts[i].getArea() << 
			setw(10) << ts[i].getPerimeter() << setw(17) << ts[i].getType() << endl;
		*/



Full Code / Files:
main.cpp: http://cpp.sh/8j7vu
Triangle.h: http://cpp.sh/6dl2z
Triangle.cpp: http://cpp.sh/7geyd



I know 3 files isn't the best to be asking questions about, I really tried to figure out the issue myself but I've spent hours on this and have had no luck. I'd really appreciate some help!

Thanks!


Note: If you need an example input file then you can use a text file with:
1
2
3
2.5 2.0 2.0
5.0 5.0 5.0
2.0 3.0 4.0


Edit: Updated Error. Posted wrong errors before I think.
Last edited on
I didn't actually download and compile your code, but just looking at:

main.cpp:139:37: error: passing ‘const value_type {aka const Triangle}’ as ‘this’ argument discards qualifiers [-fpermissive]
ts[i].getSides(side1, side2, side3);

"discards qualifiers" is the compiler's way of saying that you're trying to modify (or potentially could modify) an object that was declared as const.

In your writeReport function, void writeReport(const vector<Triangle>& ts)
you are passing the vector ts as const reference. That means you can't use a function that could potentially modify the data of one of the elements of the vector (or the vector itself)*.

ts[i].getSides(side1, side2, side3); getSides is not declared with const correctness in your class definition, so to the compiler, getSides is a function that could potentially modify the object ts[i].

In Triangle.h, you need to make your getSides declaration look like this instead:
void getSides(double &side1, double &side2, double &side3) const;
Emphasis on the const at the end.
And remove the getSides declaration that doesn't use references, I think that will just cause more errors.

Let us know if adding const there makes the error disappear.


https://isocpp.org/wiki/faq/const-correctness

*note: The constness of data pointed to by pointers doesn't propagate, this can sometimes cause confusion. But you don't need to worry about that here. https://stackoverflow.com/questions/4729820/propagate-constness-to-data-pointed-by-member-variables
Last edited on
Ohh, thank you very much for your response (and explaining why)!

After checking the stackoverflow page I had tried something similar, but I made the mistake of putting the "const" after the statement "getSides(side1, side2, side3)" instead of after the function statement(s).

After adding the const where you said and in Triangle.cpp, it worked perfectly.

"And remove the getSides declaration that doesn't use references, I think that will just cause more errors."
Should I still remove the getSides declaration that doesn't use references if the code compiles without errors? (May be a dumb question, but sometimes even though it doesn't cause errors it could still be doing something wrong). Also, where were you referring to?
Nevermind about that last part... that was me just misreading your source code!

Glad it works.
Ah okay, thanks!
Edit: Closing this, I wasn't able to figure it out so I decided I'd just leave it as is. I completed the bonus questions so I should still get 100% on the assignment. As for the learning aspect of it, oh well. I need to focus on other assignments too.

Thank you for the original help fixing the error! =)
=========================================

Okay, was going to mark as solved but I'm having trouble with 2 more parts that I've been trying to figure out for the last 3 hours.
1. The findArea() function in Triangle.cpp. I think I set it up properly to actually find the area. But I think I need to call the function and no matter what I try it doesn't work.
2. Sorting triangle ( in main() from main.cpp ). Not sure how to use the sort() for this, all of my attempts have failed. Also, I don't fully understand what I'm supposed to sort. I guessed the sides of the triangles which is what I was trying to sort, but one of the comments says I should use this for it:
friend bool operator<(const Triangle &, const Triangle &);
Which compares the areas of t1 and t2 (triangle objects).

Notes:
- void Triangle::findArea() {} can be found in Triangle.cpp at line 43.
- The parts where I'm trying to use findArea() & getArea() are in main.cpp at the lines: 143-148.
- I'm guessing I'll need to get the area fixed before I can fix sort because in Triangle.h there's a note that says:
1
2
// use this friend function to compare and sort triangles
friend bool operator<(const Triangle &, const Triangle &);

And that ^ function is found in Triangle.cpp:
1
2
3
4
5
6
7
8
9
10
// friend function to Triangle class
bool operator<(const Triangle& t1, const Triangle& t2) {
    // FIXME12: - fixed
	// return true if t1's area is smaller than t2's area 
    // false otherwise
	if (t1.getArea() < t2.getArea())
		return true;
	else
		return false;
}


Latest Code:
main.cpp: http://cpp.sh/4bsfe
Triangle.h: http://cpp.sh/9hrsh
Triangle.cpp: http://cpp.sh/2o4pd

Input File:
1
2
3
2.5 2.0 2.0
5.0 5.0 5.0
2.0 3.0 4.0


Output File:
(Everything works except the area)
1
2
3
4
5
6
7
8
9
*****************************************************************
                    Triangle Information
*****************************************************************
    #  side 1  side 2  side 3    area perimeter             type
=================================================================
    1    2.50    2.00    2.00    0.00      6.50        Isosceles
    2    5.00    5.00    5.00    0.00     15.00      Equilateral
    3    2.00    3.00    4.00    0.00      9.00          Scalene
=================================================================



Edit: Closing this, I wasn't able to figure it out so I decided I'd just leave it as is. I completed the bonus questions so I should still get 100% on the assignment. As for the learning aspect of it, oh well. I need to focus on other assignments too.

Thank you for the original help fixing the error! =)
Last edited on
Topic archived. No new replies allowed.