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> ?
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> ?