Function Template Problem With MSVS 2005 compiler

L

LCJ

I dont know whether this is the correct group for this post as it
seems to specific to the Microsoft Visual Studio 2005 C++ compiler. I
have begun to construct a function template for a Gauss Seidel
procedure to solve a system of linear equations. The code can be ssen
here:

------------------------------------------------------------------------------
// Main.cpp
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

template<typename T, int n> void GaussSeidel(T A[][n], T x[n], T b[n],
int iter = 20)
{

for(int i=0; i<iter; i++)
{
x[0] = b[0]/A[0][0] - (A[0][1]/A[0][0])*x[1];
x[1] = b[1]/A[1][1] - (A[1][0]/A[1][1])*x[0];
}

cout << setprecision(20) << A[0][0]*x[0] + A[0][1]*x[1] << endl
<< A[1][0]*x[0] + A[1][1]*x[1] << endl;

return;
}



int main()
{
cout << endl;
double A[][2] = {{10.4, 1.2},
{1.7, -9.2}};
double x[2] = {1, 1};
double b[2] = {-2, 4};

GaussSeidel(A,x,b);

cout << x[0] << endl
<< x[1] << endl;

return 0;
}

---------------------------------------------------------------------------------------------

The idea is that the function template should accept a sytem linear
equations of the form Ax=b where A is a n x n array, x is an initial
guess and b is the result obtained for the correct solution.

When the above code is compiled in MSVS 2005 the following error is
obtained:

error C2784: 'void GaussSeidel(T [][n],T [n],T [n],int)' : could not
deduce template argument for 'T [n]' from 'double [2]'

However when compiled with the MinGW g++ compiler there is no problem
whatsoever.

My question is whether anyone knows what the problem is in MSVS 2005
and how to solve it. Preferably without having to instantiate the
template manually ie GaussSeidel<double, 2> ?
 
V

Victor Bazarov

LCJ said:
I dont know whether this is the correct group for this post as it
seems to specific to the Microsoft Visual Studio 2005 C++ compiler. I

You could also try 'microsoft.public.vc.language'. And you're aware
that VS 2008 has been around for more than a year already, yes?
have begun to construct a function template for a Gauss Seidel
procedure to solve a system of linear equations. The code can be ssen
here:

------------------------------------------------------------------------------
// Main.cpp
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

template<typename T, int n> void GaussSeidel(T A[][n], T x[n], T b[n],

Those arrays aren't really arrays, you understand that, yes? They are
pointers, the size for the singly-dimensioned ones is ignored, and the
doubly-dimensioned one has an empty set of brackets.

You could *try* making those references to arrays:

... void GaussSeidel(T A[][n], T (&x)[n], T (&b)[n]),
int iter = 20)
{

for(int i=0; i<iter; i++)
{
x[0] = b[0]/A[0][0] - (A[0][1]/A[0][0])*x[1];
x[1] = b[1]/A[1][1] - (A[1][0]/A[1][1])*x[0];
}

cout << setprecision(20) << A[0][0]*x[0] + A[0][1]*x[1] << endl
<< A[1][0]*x[0] + A[1][1]*x[1] << endl;

return;
}



int main()
{
cout << endl;
double A[][2] = {{10.4, 1.2},
{1.7, -9.2}};
double x[2] = {1, 1};
double b[2] = {-2, 4};

GaussSeidel(A,x,b);

cout << x[0] << endl
<< x[1] << endl;

return 0;
}

---------------------------------------------------------------------------------------------

The idea is that the function template should accept a sytem linear
equations of the form Ax=b where A is a n x n array, x is an initial
guess and b is the result obtained for the correct solution.

When the above code is compiled in MSVS 2005 the following error is
obtained:

error C2784: 'void GaussSeidel(T [][n],T [n],T [n],int)' : could not
deduce template argument for 'T [n]' from 'double [2]'

However when compiled with the MinGW g++ compiler there is no problem
whatsoever.

My question is whether anyone knows what the problem is in MSVS 2005
and how to solve it. Preferably without having to instantiate the
template manually ie GaussSeidel<double, 2> ?

How is 'n' used in your function? There seems to be only one use for it
- to tell the compiler what the second dimension of 'A' is. Are you
hoping to make your function more sophisticated and use 'n' in some way?
Then show it. Otherwise, swap the template arguments:

template<int n, typename T>

and supply 'n' when calling your function:

GaussSeidel<2>(A,x,b);

V
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top