Page 1 of 1

compile error. problem may be in using find() function.

Posted: Sun Nov 20, 2005 6:41 pm
by jjtse
Hey guys,

can someone help me compile this code under the right compiler and tell me what the error is? I'm suspecting it's the find() function that might have triggered it:

Code: Select all



#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> vec;
vector<int> prime;
vector<int> neighbor[17];
int used[17];

void printList();

void prime_generator(){

  int i,j;
  bool isPrime;
  int num;

  prime.clear();
  prime.push_back(2);
  for(j=3; j<100; j=j+2){
    isPrime = true;
    for (i=0; (prime.at(i)*prime.at(i)) <= j;  i++) {  //skips 0th prime            
      if ((j%prime.at(i)) == 0) {
        isPrime = false;
        break;
      }
    }
    if (isPrime){
      prime.push_back(j);
      //      primes[num_primes++] = j;
    }
  }   //end outer for                                                           
}


void erase (int cnt){
  int i;

  vector<int>::iterator result;
  result = find( vec.begin(), vec.end(), cnt );
  vec.erase(result);
}


bool isPrime(int num){
  vector<int>::iterator result;
  result = find(prime.begin(), prime.end(), num);
  if (result == prime.end())  //can't find prime number
    return false;
  else
    return true;
}


void checkPrime(){
  int i = 0;
  int num;
  bool p = true;

  i = vec.size()-1;
  num = vec.at(i) + vec.at(0);
  if (isPrime(num))
    printList();  
}


void printList(){
  int i;
  for (i=0; i<vec.size(); i++)
    cout<<vec.at(i)<<" ";
  cout<<endl;
}

void print(int cnt, int n){
  int i,j,k;
  int num;
  
  vec.push_back(cnt);
  used[cnt] = 1;
  if (vec.size() == n)
    checkPrime();
  
  //search all of the current node's possible neighbors from neighbor[17]
  
  for (i=0; i<neighbor[cnt].size(); i++){
    num = neighbor[cnt].at(i);
    if (num <= n){
      if (used[num] == 0)
	print(num, n);
    }
  }
  //not in its neighbors
  erase(cnt);
  used[cnt] = 0;
}


void calc_nbrs(){  //calculate neighbors that will get a prime sum
  int i, j;
  int sum;

  for (i=0; i<17; i++)
    neighbor[i].clear();
  
  for (i=1; i<=16; i++){
    if (i % 2 == 0){
      for (j=1; j<=16; j+=2){
	sum = i+j;
	if (isPrime(sum))
	  neighbor[i].push_back(j);
      }
    }
    else{
      for (j=2; j<=16; j+=2){
	sum = i+j;
	if (isPrime(sum))
	  neighbor[i].push_back(j);
      }
    }
  }//end for i
}//end calc_nbrs;

void init_array(){
  for (int i=0; i<17; i++){
    used[i] = 0;
  }
}


int main(){
  int n, i, j;
  int counter = 1;


  prime_generator();
  calc_nbrs();

  cin>>n;
  
  while (!cin.eof()){
    vec.clear();
    init_array();
    cout<<"Case "<<counter<<":"<<endl;
    counter++;
    print(1,n);
    cin>>n;
    if (!cin.eof())
      cout<<endl;
  }

  return 0;
}



Thanks

Posted: Sun Nov 20, 2005 7:29 pm
by ayon
i think your

Code: Select all

prime.clear();
makes the compile error. You used this function 3 times. Better change the function to

Code: Select all

prime.erase(prime.begin(), prime.end());
Hope it works...

Posted: Sun Nov 20, 2005 11:38 pm
by jjtse
nope. That's not the error. I did what you said, and it still gives me compile error when I submitted it.

When you compiled it, what kind of error did it give you?

Posted: Mon Nov 21, 2005 9:09 am
by chunyi81
I compiled your code using g++ 2.95 (the same compiler the OJ is using) and got the following errors:

testing.cpp: In function `void prime_generator()':
testing.cpp:24: no matching function for call to `vector<int,allocator<int> >::at (int &)'
testing.cpp:24: no matching function for call to `vector<int,allocator<int> >::at (int &)'
testing.cpp:25: no matching function for call to `vector<int,allocator<int> >::at (int &)'
testing.cpp: In function `void checkPrime()':
testing.cpp:63: no matching function for call to `vector<int,allocator<int> >::at (int &)'
testing.cpp:63: no matching function for call to `vector<int,allocator<int> >::at (int)'
testing.cpp: In function `void printList()':
testing.cpp:72: no matching function for call to `vector<int,allocator<int> >::at (int &)'
testing.cpp: In function `void print(int, int)':
testing.cpp:88: no matching function for call to `vector<int,allocator<int> >::at (int &)'

testing.cpp is the file I saved your code in.
Instead of using the at function, use the [] operator instead.

The clear function is fine. However, if you try to use the clear function in the C++ string library (supported by the compiler g++ 3.0 and later but not g++ 2.95 I think) you will get a compile error from the judge.

Posted: Mon Nov 21, 2005 9:28 am
by jjtse
you are absolutely correct. you fixed the compile error.

Posted: Mon Nov 21, 2005 9:28 am
by jjtse
thanks!