Why is this invalid as a function prototype? (c++)

cyclone3d

[H]F Junkie
Joined
Aug 16, 2004
Messages
13,800
template<class type, class type2, class type3>
void sieve(CPrime<type, type2, type3> *cPrime);

Any time I have this as a function prototype, VS2010 gives me an error that this is an "illegal use of type void" and stops my build?

When I just use it for the actual function definition, it compiles properly.
 

Cyrilix

2[H]4U
Joined
Jan 21, 2005
Messages
2,188
Are you aware that template classes and functions are not separated into header and implementation? The implementation must be with the header, because the information on how to generate instances of the function for types T must be known at compile time.
 

cyclone3d

[H]F Junkie
Joined
Aug 16, 2004
Messages
13,800
Are you aware that template classes and functions are not separated into header and implementation? The implementation must be with the header, because the information on how to generate instances of the function for types T must be known at compile time.

I'm not using a header file for this. The prototype is near the top of my main source file and the implementation is in my main source file.

All of the other templated function prototypes work fine. The only difference is that none of the other functions I have prototyped have anything passed into them.
 

Blazestorm

Supreme [H]ardness
Joined
Jan 17, 2007
Messages
6,940
You might need a "typename" before CPrime in the parameter list...

void sieve(typename CPrime<type, type2, type3> *cPrime);

I'm not 100% on templates but we just had an assignment and I remember that was required when we were returning templated classes, so it might be required for the parameters as well.
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,775
I don't think anyone can help you without seeing the declaration of CPrime.
 

cyclone3d

[H]F Junkie
Joined
Aug 16, 2004
Messages
13,800
I don't think anyone can help you without seeing the declaration of CPrime.

Guess I should have incuded it... In any case, here is the declaration of CPrime

template<class type, class type2, class type3>
class CPrime : public ting::Thread
 

cyclone3d

[H]F Junkie
Joined
Aug 16, 2004
Messages
13,800
We'd need to see more of the code to know for sure.

O.k., here is the complete CPrime class:

Code:
template<class type, class type2, class type3>
class CPrime : public ting::Thread
{
   public:
     int iNum;
     my_uint32 array_id;
   
    void Run() //override
    {
       if(choice == 1)
       {
          if (iNum == 0)
          { 
             first_prime_sieve<type>();
             array_id = cpu_cores;
          }
          else 
          {
             array_id = iNum;
          }
          while(array_id < total_work_units)
          {
             prime_sieve<type, type2>(array_id);
             array_id += cpu_cores;
          }
       }
       else
          if(choice == 2)
          {
             array_id = iNum;
             while(array_id < total_work_units)
         {
         prime_sieve_range<type, type3>(array_id);
         array_id += cpu_cores;
       }
    }
 };
};

And here is the sieve function:

Code:
template<class type, class type2, class type3>
void sieve(CPrime<type, type2, type3> *cPrime)
{
   my_uint32 i;

   for(i=0; i<cpu_cores; i++)
   {   
      cPrime[i].iNum = i;
		
      again:
      try
      {
         cPrime[i].Start();
      }
      catch (exception& e)
      {
         cout << e.what() << endl;
         cout << "Trying thread: " << i  << " again." << endl;
         goto again;
       }
   }

// Wait for all threads to end before continuing 
   for (i = 0; i<cpu_cores; i++)
   {
      cPrime[i].Join();
   }

   delete[] cPrime;

   return;
}
 
Last edited:

Arainach

2[H]4U
Joined
Feb 25, 2006
Messages
2,353
For reference, I just wrote this, that compiles and runs fine in VS2010:
Code:
#include <iostream>
using namespace std;

template<class A, class B, class C>
class foo
{
public:
    A item1;
    B item2;
    C item3;

    foo(A a, B b, C c)
        : item1(a), item2(b), item3(c) {};
};

template <class D, class E, class F>
void sieve(foo<D,E,F> *cPrime)
{
    if (!cPrime)
    {
        cout << "Null Pointers are bad, mmkay?" << endl;
    }
    else
    {
        cout    << "item1: " << cPrime->item1 << endl
                << "item2: " << cPrime->item2 << endl
                << "item3: " << cPrime->item3 << endl;
    }
}

int main()
{
    foo<int, float, bool> myFoo(42, 3.14159f, false);

    sieve<int, float, bool>(&myFoo);

    return 0;
}
If I move the implementation of sieve to the bottom of the file and leave a declaration at the top of the file, it still works fine. My guess is you have another error in your file somewhere (missed semicolon after a class or struct definition?) that is causing the error.
 

cyclone3d

[H]F Junkie
Joined
Aug 16, 2004
Messages
13,800
hrmmm...

Here it is surrounded by other function declarations.

Code:
template<class type, class type2>
void prime_sieve_range(my_uint32 work_unit_id);

template<class type, class type2, class type3>
void sieve(CPrime<type, type2, type3> *cPrime);

void prime_number_test(my_uint32 work_unit_id);

It only gives me that stupid error when the declaration for sieve is there.... :mad:
 
Last edited:

cyclone3d

[H]F Junkie
Joined
Aug 16, 2004
Messages
13,800
Yep, no matter where I put that function declaration in my function declarations it gives me that error.

As soon as I comment out that function declaration it compiles fine.

I even tried moving some variables and a few defines down to where the rest of them were and it made no difference.

I have looked through my code line by line and cannot find anything wrong,

It compiles and runs great without that function declaration there..... so weird.
 

cyclone3d

[H]F Junkie
Joined
Aug 16, 2004
Messages
13,800
Figured it out... was a lame mistake on my part. I was declaring the function before the class. As soon as I put the function declaration after the class the error went away.
 
Top