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

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US
Contact:

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

Post by jjtse » Sun Nov 20, 2005 6:41 pm

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

ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Post by ayon » Sun Nov 20, 2005 7:29 pm

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...
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program

jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US
Contact:

Post by jjtse » Sun Nov 20, 2005 11:38 pm

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?

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 » Mon Nov 21, 2005 9:09 am

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.

jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US
Contact:

Post by jjtse » Mon Nov 21, 2005 9:28 am

you are absolutely correct. you fixed the compile error.

jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US
Contact:

Post by jjtse » Mon Nov 21, 2005 9:28 am

thanks!

Post Reply

Return to “C++”