no match for "operator+" problem!

This is a discussion on no match for "operator+" problem! within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Im writing a fraction program that will do some nifty stuff. To cut to the chase, here are ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    53

    no match for "operator+" problem!

    Hello everyone, Im writing a fraction program that will do some nifty stuff. To cut to the chase, here are my 3 files but Im having trouble compiling the test function, i keep getting a "no match for 'operator+'" error. Thanks for all and any help!

    fraction.h

    Code:
    #ifndef Fraction
    #define Fraction
    
    class mixedfraction
    {
    	public:
    		mixedfraction();
    		void read();
    		void display();
    		double evaluate();
    		mixedfraction add (mixedfraction, mixedfraction);
    		mixedfraction sub (mixedfraction);
    		mixedfraction mul (mixedfraction);
    		mixedfraction div (mixedfraction);
    		int whole;
    		int numerator;
    		int denominator;
    };
    #endif


    fraction.cpp

    Code:
    #include <iostream>
    #include <cmath>
    #include "mixedfraction.h"
    
    using namespace std;
    
    mixedfraction::mixedfraction()
    {
    	whole=0;
    	numerator=0;
    	denominator=1;
    }
    
    void mixedfraction::read()
    {
    	cout << "Please enter whole number: " <<endl;
    	cin >> whole;
    	cout << "Please enter numerator: " <<endl;
    	cin >> numerator;
    	cout << "Please enter denominator: " <<endl;
    	cin >> denominator;
    }
    
    void mixedfraction::display()
    {
    	cout << whole << numerator <<"/" << denominator << endl;
    }
    
    double mixedfraction::evaluate()
    {
    	double temp;
    	temp = (double) whole + (double) numerator / (double) denominator;
    	return temp;
    }
    
    mixedfraction add (mixedfraction f1, mixedfraction f2)
    {
    	mixedfraction sum;
    	sum.numerator = f1.numerator*f2.denominator + f2.numerator*f1.denominator;
    	sum.denominator = f1.denominator*f2.denominator;
    	return sum;
    }


    test.cpp

    Code:
    #include <iostream>
    #include <cmath>
    #include "mixedfraction.h"
    
    using namespace std;
    
    int main()
    {
    	mixedfraction x,y,z;
    	double i;
    	x.read();
    	x.display();
    	i = x.evaluate();
    	cout << i << endl;
    	y.read();
    	y.display();
    	z= x+y;
    	cout << "The sum of ";
    	x.display();
    	cout << "and";
    	y.display();
    	cout << "is";
    	z.display();
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    You are getting that error because you don't provide a + operator in your class. So you can provide one or change the
    Code:
    z = x + y
    To
    Code:
    z = x.add(x, y);
    Also I would like to note. That I would change that add definition to actually operate on the class itself and only take one parameter
    Woop?

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    add should be like all the other methods you declared. Taking one argument makes more sense. Also, since this is c++ you should take advantage of operator overloading.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    thanks for the help guys!

    changing it to "z = x.add(x, y);" worked. However, when I removed the second parameter, I get a compiling error:
    mixedfractiontest.cpp:17: error: no matching function for call to ‘mixedfraction::add(mixedfraction&, mixedfraction&)’

    Im sure I misunderstood what you guys were saying.

    EDIT: when i try to compile the files together, I get this error:

    mixedfractiontest.o: In function `main':
    /home/atayh/Programs/mixedfractiontest.cpp:17: undefined reference to `mixedfraction::add(mixedfraction, mixedfraction)'
    collect2: ld returned 1 exit status
    Last edited by wankel; 07-27-2009 at 02:23 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by wankel View Post
    thanks for the help guys!

    changing it to "z = x.add(x, y);" worked. However, when I removed the second parameter, I get a compiling error:
    mixedfractiontest.cpp:17: error: no matching function for call to ‘mixedfraction::add(mixedfraction&, mixedfraction&)’

    Im sure I misunderstood what you guys were saying.
    It should be x.add(y), if you want to add y to x.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by tabstop View Post
    It should be x.add(y), if you want to add y to x.
    thanks tabstop. I made your change and both the fraction.cpp and fractiontest.cpp compile fine individually. But when I try to combine them using the following line on linux:

    g++ -Wall -ggdb mixedfractiontest.o mixedfraction.o

    I get this error:

    mixedfractiontest.o: In function `main':
    /home/atayh/Programs/mixedfractiontest.cpp:17: undefined reference to `mixedfraction::add(mixedfraction)'
    collect2: ld returned 1 exit status

    Im not sure what the problem could be since they compile just fine seperately =/

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You have declared it as a member function but implemented it as a free function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    I would also like to give you a tip: SourceForge.net: Do not remove parameter names - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Thanks for the help and advice everyone! I finally got everything working nicely, but there is one last thing I would like some help on.

    At the end of "test.cpp", how do I make z.display() the new x.read()? I have my program set up in a loop so when I get the sum of the addition, id like to use that new number as my default. I tried "x.read()=z.display()", but I got an error saying thats an invalid use of void. Is there another way to do this? Any help/tips/advice would be very much appreciated!

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    >> x.read()=z.display()

    What did you expect? Both functions return void! Compilers don't read minds (yet, anyway)...you need to have your functions receive/return actual values so that they can be connected properly.



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

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