"Explicit specialization only allowed at file or namespace scope"

B

Bas

Explicit specialization only allowed at file or namespace scope" .. is the
error I got from a compiler. Another compiler doesn't
complain.
Who is right?

Thanks on forehand,

Bas

#include <iostream>

class Test
{
public:
int x,y;

Test(int a, int b) : x(a), y(b) {};

template<typename x>
x telop(x a, x b)
{
return a + b;
}

template<> // One doesn't like this one.
int telop<int>(int a, int b)
{
return (a + b) * 10;
}
};


int _tmain(int argc, _TCHAR* argv[])
{
Test t(3.14, 4.545);

std::cout << t.telop<int>(3.14, 4.545);

std::cin.get();
return 0;
}
 
A

Alf P. Steinbach

* Bas:
Explicit specialization only allowed at file or namespace scope" .. is
the error I got from a compiler. Another compiler doesn't complain.
Who is right?

Thanks on forehand,

Bas

#include <iostream>

class Test
{
public:
int x,y;

Test(int a, int b) : x(a), y(b) {};

template<typename x>
x telop(x a, x b)
{
return a + b;
}

template<> // One doesn't like this one.
int telop<int>(int a, int b)
{
return (a + b) * 10;
}
};

Yeah, you have to move that specialization out of the class definition, to the
namespace that the class is defined in, specified by §14.7.3/2.

int _tmain(int argc, _TCHAR* argv[])

This is not a valid 'main'.

{
Test t(3.14, 4.545);

std::cout << t.telop<int>(3.14, 4.545);

std::cin.get();
return 0;
}


Cheers & hth.,

- Alf
 
N

Neelesh

Explicit specialization only allowed at file or namespace scope" .. is the
error I got from a compiler. Another compiler doesn't
complain.
Who is right?

Thanks on forehand,

Bas

#include <iostream>

class Test
{
   public:
   int x,y;

   Test(int a, int b) : x(a), y(b) {};

   template<typename x>
   x telop(x a, x b)
   {
      return a + b;
   }

   template<>                                // One doesn't like this one.
   int telop<int>(int a, int b)
   {
      return (a + b) * 10;
   }
};

You will need to give the specialization outside the class in the
global / namespace scope.

template<> int Test::telop<int>(int a , int b)
{
return (a+b)*10;
}

int _tmain(int argc, _TCHAR* argv[])
{
That's not std C++, a portable code needs to use int main and char*
argv[])
 
B

Bas

Thanks both of you.

Bas

Explicit specialization only allowed at file or namespace scope" .. is the
error I got from a compiler. Another compiler doesn't
complain.
Who is right?

Thanks on forehand,

Bas

#include <iostream>

class Test
{
public:
int x,y;

Test(int a, int b) : x(a), y(b) {};

template<typename x>
x telop(x a, x b)
{
return a + b;
}

template<> // One doesn't like this one.
int telop<int>(int a, int b)
{
return (a + b) * 10;
}
};

You will need to give the specialization outside the class in the
global / namespace scope.

template<> int Test::telop<int>(int a , int b)
{
return (a+b)*10;
}

int _tmain(int argc, _TCHAR* argv[])
{
That's not std C++, a portable code needs to use int main and char*
argv[])
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top