I'm having troubles with a code for a program (involving classes) where I am supposed to input the three sides of a triangle and the program determines whether the triangle is a right triangle, equilateral, isosceles, and scalene, etc.

I have only one error preventing me from compiling my program, and it is a c2062 "Type 'int' unexpected" error. It is related to a constructor I've written. If somebody can at least point out to me what's wrong and how to fix it, I would appreciate it.

At any rate, here's the header file:

#include<string>

using namespace std;

class Triangle  
{
private:
	int side1;
	int side2;
	int side3;
	int largest();
	int smallest();

public: 
	Triangle() ;     //Default Constructor
	Triangle(int side1, int side2, int side3);

	bool isRight();

	bool isEquilateral();

	bool isIsosceles();

	bool isScalene();

	void print();
};

And the Triangle.cpp file (different from the main function).

#include <iostream>
#include <string>
#include <iomanip>
#include "Triangle.h"
#include <cmath>
Triangle::Triangle()
{
	side1=0;
	side2=0;
	side3=0;
}
Triangle(int side1,int side2,int side3);
int Triangle::largest()
{
	// For the largest side of the triangle:
	int largest=side1;
	
	if (side2 > largest)
	{
		largest=side2;
	}

	if (side3 > largest)
	{
		largest=side3;
	}
	;
}

int Triangle::smallest()
{
	int smallest=side1;
	
	if (side2 < smallest)
		{
			smallest=side2;
		}

	if (side3 < smallest)
		{
			smallest=side3;
		}
}

bool Triangle::isEquilateral()
{	
	if (largest() == smallest())
	{
		return true;
	}

	else 
	{
		return false;
	}
}

bool Triangle::isIsosceles()
	// If the triangle is isosceles or not.
{
	if (side1 == side2)
	{
		return true;
	}

	else if (side1 == side3)
	{
		return true;
	}

	else if (side2 == side3)
	{
		return true;
	}

	else if (side1 != side2 && side1 != side3 && side2 != side3)
	{
		return false;
	}
}	
bool Triangle::isScalene()
{
	if (isIsosceles()== true)
	{
		return false;
	}

	else 
	{
		return true;
	}
}

bool Triangle::isRight() 
{
	if ( pow((double) side1,2) == (pow((double) side2,2) + pow((double) side3,2)))
	{
		return true;
	}

	else if (pow((double) side2,2) == (pow((double) side1,2) + pow((double) side3,2)))
	{
		return true;
	}

	else if (pow((double) side3,2) == (pow((double) side1,2) + pow((double) side2,2)))
	{
		return true;
	}

	else
	{
		return false;
	}
}

void Triangle::print()
{
	cout << side1 << " " << side2 << " " << side3 << " " << "triangle:" << endl;
}

The error is specifically related to "Triangle(int side1,int side2,int side3);" Also, it does that no matter the variables within the three arguments after "Triangle"- it's just whenever I place "int" in there.

Thanks!

Recommended Answers

That's supposed to be the class constructor, so code it like this

Triangle::Triangle(int side1,int side2,int side3)
{

}
Jump to Post

In addition, consider the overall design. Do you want to use integers as the values of the sides? I would use doubles. Be wary of comparing doubles using the == operator as you do in isRight () . There's a potential for round-off error. Since you are typecasting from integers …

Jump to Post

>>I've tried doing that, but to no avail.
Then you did it wrong. Post what you tried.

Put them exactly as I posted them. Some people like to do it like below. Do it which ever way you want, or however your instructor told you to.


       
Jump to Post

All 10 Replies

That's supposed to be the class constructor, so code it like this

Triangle::Triangle(int side1,int side2,int side3)
{

}

In addition, consider the overall design. Do you want to use integers as the values of the sides? I would use doubles. Be wary of comparing doubles using the == operator as you do in isRight () . There's a potential for round-off error. Since you are typecasting from integers and squaring, there may not be round-off error in your particular case. But you need to be very careful and aware.

That's supposed to be the class constructor, so code it like this

Triangle::Triangle(int side1,int side2,int side3)
{

}

Understood, but could you please tell me where exactly am I supposed to put the brackets? Not to be rude. I've tried doing that, but to no avail.

>>I've tried doing that, but to no avail.
Then you did it wrong. Post what you tried.

Put them exactly as I posted them. Some people like to do it like below. Do it which ever way you want, or however your instructor told you to.

Triangle::Triangle(int side1,int side2,int side3) {

}

>>I've tried doing that, but to no avail.
Then you did it wrong. Post what you tried.

Put them exactly as I posted them. Some people like to do it like below. Do it which ever way you want, or however your instructor told you to.

Triangle::Triangle(int side1,int side2,int side3) {

}

Resulted in the same error, as well as "error C2447: '{' : missing function header (old-style formal list?)"

#include <iostream>
#include <string>
#include <iomanip>
#include "Triangle.h"
#include <cmath>
Triangle::Triangle(int side1,int side2,int side3)
{
	side1=0;
	side2=0;
	side3=0;
}

Triangle(int side1,int side2,int side3);
{

}
int Triangle::largest()
{
	// For the largest side of the triangle:
	int largest=side1;
	
	if (side2 > largest)
	{
		largest=side2;
	}

	if (side3 > largest)
	{
		largest=side3;
	}
	return largest;
}

int Triangle::smallest()
{
	int smallest=side1;
	
	if (side2 < smallest)
		{
			smallest=side2;
		}

	if (side3 < smallest)
		{
			smallest=side3;
		}
	return smallest;
}

bool Triangle::isEquilateral()
{	
	if (largest() == smallest())
	{
		return true;
	}

	else 
	{
		return false;
	}
}

bool Triangle::isIsosceles()
	// If the triangle is isosceles or not.
{
	if (side1 == side2)
	{
		return true;
	}

	else if (side1 == side3)
	{
		return true;
	}

	else if (side2 == side3)
	{
		return true;
	}

	else if (side1 != side2 && side1 != side3 && side2 != side3)
	{
		return false;
	}
}	
bool Triangle::isScalene()
{
	if (isIsosceles()== true)
	{
		return false;
	}

	else 
	{
		return true;
	}
}

bool Triangle::isRight() 
{
	if ( pow((double) side1,2) == (pow((double) side2,2) + pow((double) side3,2)))
	{
		return true;
	}

	else if (pow((double) side2,2) == (pow((double) side1,2) + pow((double) side3,2)))
	{
		return true;
	}

	else if (pow((double) side3,2) == (pow((double) side1,2) + pow((double) side2,2)))
	{
		return true;
	}

	else
	{
		return false;
	}
}

void Triangle::print()
{
	cout << side1 << " " << side2 << " " << side3 << " " << "triangle:" << endl;
}

Look at line 13. Don't you see the error in that line?? Hint: look at the semicolon at the end of the line.

And you failed to add the class declaration Triangle::Triangle(i// blabla)

Wasn't the offending line Triangle(int side1,int side2,int side3); ?
Why is it still in the code?

Look at line 13. Don't you see the error in that line?? Hint: look at the semicolon at the end of the line.

And you failed to add the class declaration Triangle::Triangle(i// blabla)

Unfortunately I did try adding the class declaration before and it resulted in a further error where Visual Express told me that there was an error in adding the class declaration again...

I'll try again.

Wasn't the offending line Triangle(int side1,int side2,int side3); ?
Why is it still in the code?

If I tried removing the code then it would result in errors where the main program (which isn't posted) wouldn't know what to do with the Triangle.cpp file.

A constructor is needed, and the main issue is why int is "unexpected."

Unfortunately I did try adding the class declaration before and it resulted in a further error where Visual Express told me that there was an error in adding the class declaration again...

I'll try again.

I posted it EXACTLY as you should have coded it. All you had to do is copy/paste into your program. You even quoted it just an hour ago (see your post #4 in this thread).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.