Dev-c++ No Match For Operator

Posted on by
  1. Dev C++ No Match For Operator
  2. C++ No Match For Operator

Operators are simply symbols that perform a certain function - some of these perform mathematical functions. In this tutorial, we're going to build on your knowledge of user input, outputting data, and variables, to learn about some of the basic mathematical operators.

I know how to do it through a friend function, i am now trying to learn how to do it through a member function. For a binary operator@ and objects A a and B b, the syntax a @ b will call either a non-member function of the form operator@(A,B) or a member function of the form A::operator.

C++, like most programming languages, works on the concept of 'expressions' - combinations of symbols which represent values. If a simple mathematical statement is written, for example 5+5, the calculation will be evaluated by the C++ compiler, and the result put in the place of the expression - in this case, 10. The + operator which is used to add two number values, be these constants (or literals) like '5', or variables of supported types (e.g. int, double, or float), can be shown to behave in this way with a simple cout. Take the following in which '14' is outputted instead of the actual text '8+6':

  1. Operator Keyword for!= The noteq operator is the text equivalent of!=. There are two ways to access the noteq operator in your programs: include the header file iso646.h, or compile with the /Za (Disable language extensions) compiler option.
  2. This project can now be found here. Summary Files Reviews Support Wiki Mailing Lists.

The use of operators is often dependent on the to the string. Take for example the following:

Note that in the above that the 'expression' type functionality is extremely important. The expression, one + ' Bob!' is being evaluated to 'Hello Bob!' and then outputted, however the variable 'one' is not being changed. If we wanted to actually change the value of the variable 'one' we would have to set it using something like the equals operator:

There is also another mathematical operator related to + which can do the 'one = one +' functionality for us - this is the += operator. So instead of typing something like the above, we could instead use something a little bit cleaner like:

The four basic mathematical operators, as you may expect, are those of addition, subtraction, multiplication, and division. These are represented by the +, -, * and / operators as appropriate. Each of these also has a += equivalent for setting variables to themselves, add, subtracted, multiplied by, or divided by, some other value - these are +=, -=, *=, and /= as appropriate. There is also another mathematical operator which we're going to cover in this tutorial, which is called the modulus operator. This does division of whole numbers and gives the remainder of that division - for example 5 % 3 will be '2' as 3 goes into 5 once, with 2 remaining. This task may seem quite niche at first, however can become quite useful in a number of tasks.

All of these operators can be used with either variables or literals of the correct type (in this case, you'll want to use those of a 'number' type), and perhaps the easiest way to show the functionality of these operators is to simply give an example of some expressions using them, couting the results:

So looking at the above, do they all output as expected? The majority do, however the 7 / 2 calculation outputs 'incorrectly' as '3'. This is because our application knows it's dealing with whole number division (as we've specified integer literals), and so it rounds the result to a whole number. To solve this problem if this functionality is not what we want, as is the case here, we can simply change the literals to those of the double type by changing the calculation to 7.0 / 2.0. This change means that the calculation outputs as we would expect.

So now that we know how to use basic mathematical operators in C++, let's put this to some use! Before we create a 'big' application demonstrating a number of the operators we've covered - I just want to give a moment for the modulus operator, who I feel may be under-appreciated at this point. A great use for it is checking if a whole number is even or not -- if the number divides by 2 with no remainders, i.e. number % 2 is equal to 0 the number must be even, otherwise it must be odd. We don't know how to check conditions like this quite yet, but come back to this after going through the next tutorial if you have some extra time, and try to create a basic program which takes input into an integer variable and then outputs if the number entered is even or not.

So back to creating an application with some practical use! I thought it might be a nice idea to create a program which uses the famous Pythagorean Theorem to calculate the longest side of a right-angled triangle given the other two sides. If you are unfamiliar with the equation, it states that the square of the longest side of a right-angled triangle is equal to the added squares of the other two sides. So if the longest side is 'c' and the other two are 'a' and 'b', the equation would be:

a2 + b2 = c2

So let's first get our application set up! Let's just think about includes for a second - we're definitely going to need iostream for outputting data and getting data from the user, but we're also going to need to somehow get the square root of a variable or expression to calculate the length of the longest side of the triangle. This functionality, along with some other advanced mathematical stuff, is stored inside the cmath header file, and so we should include this in our code too.

Note that with cmath included, we can square root things using the sqrt function. You can do what is called 'calling' a function by specifying the function name, followed by whatever you need to give the function to do it's job in brackets. If you don't need to give the function any values, you can just give empty brackets - (). In our case, we want to call the sqrt function with the value of the square of 'a' plus the square of 'b', and this expression will evaluate into the value we're looking for - the length of the longest side. Before this however, let's just use our knowledge of cin and cout to prompt the user for the first two sides (which, since they may be decimals, should probably be of type double):

As talked about previously, we can now simply make use of the sqrt function to square root the added squares of these values. Squaring something is just multiplying it by itself, and thus we want the sqrt of a*a + b*b - hence sqrt(a*a + b*b);. Once again the 'expression' type functionality comes in handy as the simpler mathematics is first evaluated to give the result, and then this is passed to the sqrt function and square rooted to give us the result. Some people may prefer using another variable to deal with the result and get rid of some of the nested calculations in brackets - but I don't think such clutter is necessary here. The full code for our application can be seen below:

When i tried to compile it, it gave me this error 'no match for operator'. Any idea how could this happen? This program will take user input and add it into dat file

Edited by Bigbrain99: n/a
  • 3 Contributors
  • forum 6 Replies
  • 166 Views
  • 8 Hours Discussion Span
  • commentLatest Postby Bigbrain99Latest Post

Fbody682

Dev C++ No Match For Operator

Cracked guitar vst. You haven't given a specific Line number for us to look at, but I'm guessing the error points to a line where you use an std::string.

You have not included the standard header <string> all of the functions and operators related to the std::string class are defined in that header. Without it, you can't do much with one.

C++ No Match For Operator

Edited by Fbody: n/a