Hi,

I'd like to print a line of 'double' or of 'integer' type according to T typename.
I tried to compare T with typeof but to no success.

template <typename T>
void printArray(unsigned int x_sz, unsigned int y_sz, T * arr[], std::string title)
{
	double f;
	unsigned short int usi;
	for (unsigned int i = 0; i != x_sz; ++i) {
		for (unsigned int j = 0; j != y_sz; ++j) {
			if (T == typeof(f)) { 
				printf("   %2.2f", arr[i][j]);
			} else if (T == typeof(usi)) {
				printf("   %2d", arr[i][j]);
				
			}
		}
		cout << endl;
	}
}

How should I do this?

You should make some new functions that do the printing for the different types that you want to print. For example:

void PrintValue( int x )
{
   printf("   %2d", x);
}

void PrintValue( double x )
{
   printf("   %2.2f", x);
}

template <typename T>
void printArray(unsigned int x_sz, unsigned int y_sz, T * arr[], std::string title)
{
   double f;
   unsigned short int usi;
   for (unsigned int i = 0; i != x_sz; ++i) {
      for (unsigned int j = 0; j != y_sz; ++j) {
         PrintValue(arr[i][j]);
      }
      cout << endl;
   }
}

Thank you.
It's working now.

The PrintValue function had to go to the .cpp file.

>> The PrintValue function had to go to the .cpp file.

If you mark the functions with the keyword inline , then you won't have to put the definition in the cpp file (if you don't want to).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.