GMP Problem

This is a discussion on GMP Problem within the C++ Programming forums, part of the General Programming Boards category; I have made a simple program using the GMP library that is the start of a factoring program I'm making. ...

  1. #1
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735

    GMP Problem

    I have made a simple program using the GMP library that is the start of a factoring program I'm making. Basically, it just factors a number (n) up to a bound, which is either 4000000000 or the sqrt of the number, whichever is smaller. In this example the number being factored is 1000.
    I get three errors, all of which are the same: error C2107: illegal index, indirection not allowed.
    I'm pretty sure it has to do with the array but I don't know how to fix it.

    Here's the source code, I put a comment before the error lines
    Code:
    #include <iostream>
    #include <gmp.h>
    
    using namespace std;
    
    int main()
    {
    	mpz_t n, k, var, N, N2, array, hold;
    	mpz_init (n);
    	mpz_init (k);
    	mpz_init (hold);
    	mpz_init (var);
    	mpz_init (N);
    	mpz_init (N2);
    	mpz_set_ui (n, 1000);
    	mpz_set_ui (k, 2);
    	mpz_array_init (array, 4000000000, 1);
    	mpz_sqrt (N, n);
    	if (mpz_cmp_ui (N, 4000000000) == -1)
    		mpz_set_ui (N2, 4000000000);
    	else
    		mpz_set (N2, N);
    	while (mpz_cmp (k, N2) == -1)
    	{
    //the following line has the first two errors
    		if (mpz_sgn (array[k]) == 0)
    		{
    			mpz_cdiv_q (hold, n, k);
    			if (mpz_sgn (hold) == 0)
    			{
    			cout << k << " * " << hold << "; ";
    			mpz_set (var, k);
    			while (var <= N2)
    			{
    				mpz_add (var, var, k);
    //the following line has the next error
    				mpz_set (array[var], 0);
    			}
    			}
    		}
    		mpz_add_ui (k, k, 1);
    	}
    	mpz_clear (n);
    	mpz_clear (k);
    	mpz_clear (var);
    	mpz_clear (N);
    	mpz_clear (N2);
    	return 0;
    }
    Thanks in advance!
    MadCow
    Last edited by MadCow257; 01-01-2005 at 04:02 PM.

  2. #2
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    For code tags, you need to use [ and ] instead of < and >

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,670
    >> mpz_t n, k, var, N, N2, array, hold;

    you declared a single variable, not an array. do you see the problem?



    ITSA
    Socket Library!

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I realize in normal c++ you should do int array[4000000000], but in gmp doesn't the mpz_t_array_init make it an array for you? I fiddled around a bit and fixed the code so that it has no compile errors, but it has link errors now
    Link errors are so annoying...unlike compiler errors you don't get specific lines, and the LNK2001 entry in MSDN encomposes a whole lot of possible problems, compile errors are much more specific about what's going wrong.
    Anyways, the error is
    error LNK2001: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct __mpz_struct const *)" (??6@YAAAV?$b
    asic_ostream@DU?$char_traits@D@std@@@std@@AAV01@PB U__mpz_struct@@@Z)
    I have no idea what that's talking about.
    The error is for this code:

    Code:
    #include <iostream>
    #include <gmp.h>
    
    using namespace std;
    
    int main()
    {
    	mpz_t n, N, N2, hold;
    	bool array[100000000];
    	int k=2, var;
    	mpz_init (n);
    	mpz_init (hold);
    	mpz_init (N);
    	mpz_init (N2);
    	mpz_set_ui (n, 1000);
    	mpz_sqrt (N, n);
    	if (mpz_cmp_ui (N, 100000000) == -1)
    		mpz_set_ui (N2, 100000000);
    	else
    		mpz_set (N2, N);
    	while (mpz_cmp_ui (N2, k) == 1)
    	{
    		if (array[k] == 1)
    		{
    			mpz_cdiv_q_ui (hold, n, k);
    			if (mpz_sgn (hold) == 0)
    			{
    			cout << k << " * " << hold << "; ";
    			var = k;
    			while (mpz_cmp_ui(N2, k) == 1)
    			{
    				var = var + k;
    				array[var] = 0;
    			}
    			}
    		}
    		k++;
    	}
    	mpz_clear (n);
    	mpz_clear (N);
    	mpz_clear (N2);
    	return 0;
    }
    Is anyone willing to help?
    Thanks either way,
    MadCow

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I think it means that the << operator hasn't been overloaded for the mpz_t class. Therefore, when you try to call << to ouptput hold in the following line, there is no function/operator for the linker to find eventhough it is correct syntax so it compiles.


    cout << k << " * " << hold << "; ";

    To test if that's the problem comment out the line, and if the error goes away put a ; after the "*" and comment out the rest of the line. If you can narrow it down that way you will have confirmed what and where the problem is. To fix it you'll need to know what's in mpz_t so you can overload the << operator or so you can use some existing method of the mpz_t class to output information.

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