overloading operator errors

This is a discussion on overloading operator errors within the C++ Programming forums, part of the General Programming Boards category; I am using GMP to process huge numbers. I need to overload +,-,/ and * operators to handle operations between ...

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    overloading operator errors

    I am using GMP to process huge numbers. I need to overload +,-,/ and * operators to handle operations between double, mpz_t (gmp type) and char*. When I try to declare the operator, I have errors:

    declararion: mpf_t& operator+(mpf_t& v1, double v2);

    error:

    functions.cpp:10: error: `__mpf_struct (& operator+(__mpf_struct (&)[1], double)
    )[1]' must have an argument of class or enumerated type

    Thanks any help!

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    That's a weird looking error statement. Did you copy the declaration and error statement from your compiler before posting to the board, or did you try to reproduce them from your personal memory when posting?

    Is this:

    mpf_t& operator+(mpf_t& v1, double v2);

    declared as a member function within the mpf_t class or as a friend function within the class?

    If it's a friend function then try renaming the parameters. If that doesn't work then post the full definition please.
    You're only born perfect.

  3. #3
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    You can only overload operator for which at least one of the arguments is a CLASS or reference to it. You're trying to overload a operator that receives a double and a pointer..

    Code:
    class A{};
    A* operator+(const A* a,char){}
    int main(){
    	A a;
    	&a+'h';
    	return 0;
    }
    and the error
    Code:
    $ make
    g++ -Wall -c ./main.cpp -o obj/./main.o
    ./main.cpp:24: error: `A* operator+(const A*, char)' must have an argument of cl
    ass or enumerated type
    ./main.cpp: In function `int main()':
    ./main.cpp:27: warning: statement has no effect
    ./main.cpp:29:2: warning: no newline at end of file
    make: *** [obj/./main.o] Error 1

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    It's cool when explanations are more straightforward than the question. Since the OP was passing a reference to a GMP, whatever that is, type, it would therefore seem that the compiler doesn't recognize the GMP type, mpz_t or mpf_t or whatever, as a valid type/class.
    You're only born perfect.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,650
    In case you didn't know.....GMP has a C++ class interface.

    gg

  6. #6
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Yes, it seems that the compiler does not recognize mpf_t as a struct, very strange indeed. Codeplug, I know that GMP has a C++ interface. The problem is that I was trying to use GMP in Windows. I have not found a compiled version of GMP that supports the C++ interface, and I was unable to compile full GMP under Cygwin. I'll move to Linux, the Windows was a restriction from the project.

    Thanks for the replyies anyway. I am already compiling GMP under Linux to use its C++ interface.

  7. #7
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Uhm... I forgot my real problem, sorry! The most important overload would be:

    Code:
    mpf_t operator+(char*, double);
    What is impossible. The problem is that I have huge constants exported from a mathematical software into C code. Also, the code has operations with double, like pow. I can't represent the constants like any type, except char*.

    I have made a parser that translate these huge constants into strings (not the std::string). The only solution I can think of, it is to overload the plus operator to std::string and double. Then I change my parser to produce the following output:

    3987543756234

    to

    std::string("3987543756234 ")

    Is this a good solution? Thanks any reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 12:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  3. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 04:10 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 07:49 PM

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