Iam running Dev-Cpp 4.9.9.2
The following generates an error at the function call:
invalid conversion from 'double' to 'const double'
The program runs if the 'const double' is changed to 'double' in the function definition. Conversions such as 'double' to 'const double' in the function pose no problems. This limitation on double pointers surprises me. What should I be doing differently?
Thanks
void Multiply(const double const M, const double const V,
const int n, const int m, double const MV)
{
code
}
int main()
{
code
double** Matrix = new double* [n];
for (int i=0; i<n; i++) Matrix[i] = new double [m];
code
Multiply(Matrix, Vector, n, m, MVproduct);
code
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It is always a good idea to post complete code that reproduces your issue without introducing others; yours had undefined references and comments without comment delimiters that made it not possible. Doing so may have encouraged more people to look at it sooner. I had to do some degree of massaging your code to get something that I could compile and show the error. All it needed was:
void Multiply(const double** const M)
{
}
int main()
{
double* Matrix = new double[10];
Multiply(Matrix);
}
which would not have been too hard for you to produce.
I compiled it in VC++, (after a system rebuild I have not bothered to put Dev-C++ back on). Are you sure that that is the complete error message, the types do not seem to match your code? It is always a good idea (in fact requested in the "PLEASE READ BEFORE POSTING A QUESTION" thread) to post the complete compile log verbatim rather than extracting, editing, describing, or paraphrasing error messages.
VC++ reported:
main.cpp(13) : error C2664: 'Multiply' : cannot convert parameter 1 from 'double ' to 'const double const '
note the "const double const", which is the full type of the parameter, your report has just "const double "
The rules and syntax of const-correctness are somewhat arcane and I have not taken the trouble to make myself clear why this works, or whether it is sensible, but the following rather ugly declaration works.
void Multiply(const double const const M)
{
}
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am sorry, I hope this message is clearer and complies better with the guidelines.
I am using 4.9.9.2 with XP.
The program runs well if I remove the first const in the function's variable list, appearing in "const double ** const M". Note the other pointers can be converted to pointers to constant objects -- just the double pointer is a problem.
Here is a complete simplified program (error log follows):
include <cstdlib>
include <iostream>
using namespace std;
void Multiply(const double* const M, const double * const V,
const int n, const int m, double * const MV)
{
for (int i=0; i<n; i++)
{
MV[i] = 0;
for (int j=0; j<m; j++)
{
MV[i] += M[i][j]V[j];
}
}
}
int main()
{
int n=2, m=3;
double** mat = new double* [n];
for (int i=0; i<n; i++) mat[i] = new double [m];
double * vec = new double [m];
double * matvec = new double [n];
for (int j=0; j<m; j++)
{
vec[j] = 1;
for (int i=0; i<=n-1; i++)
{
mat[i][j] = i;
}
}
Multiply(mat, vec, n, m, matvec);
delete[] matvec;
delete[] vec;
for (int i=0; i<n; i++) delete[] mat[i];
delete[] mat;
system("PAUSE");
return EXIT_SUCCESS;
}
Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\Richard\Desktop\CPP\Makefile.win"
Executing make...
make.exe -f "C:\Documents and Settings\Richard\Desktop\CPP\Makefile.win" all
g++.exe -c test.cpp -o test.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
test.cpp: In function int main()':
test.cpp:37: error: invalid conversion fromdouble' to const double**'
test.cpp:37: error: initializing argument 1 ofvoid Multiply(const double, const double, int, int, double)'
make.exe: *** [test.o] Error 1
Execution terminated
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Your new post does not include the fix I suggested. Did you try that?
On a different issue, don't put your project in C:\Documents and Settings\Richard\Desktop, the toolchain and the IDE both have bugs with handling paths containing spaces. It seems to work, until one day it does not.
void Multiply(const double const const M, const double * const V,
const int n, const int m, double * const MV)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Iam running Dev-Cpp 4.9.9.2
The following generates an error at the function call:
invalid conversion from 'double' to 'const double'
The program runs if the 'const double' is changed to 'double' in the function definition. Conversions such as 'double' to 'const double' in the function pose no problems. This limitation on double pointers surprises me. What should I be doing differently?
Thanks
void Multiply(const double const M, const double const V,
const int n, const int m, double const MV)
{
code
}
int main()
{
code
code
}
It is always a good idea to post complete code that reproduces your issue without introducing others; yours had undefined references and comments without comment delimiters that made it not possible. Doing so may have encouraged more people to look at it sooner. I had to do some degree of massaging your code to get something that I could compile and show the error. All it needed was:
void Multiply(const double** const M)
{
}
int main()
{
double* Matrix = new double [10];
Multiply(Matrix);
}
which would not have been too hard for you to produce.
I compiled it in VC++, (after a system rebuild I have not bothered to put Dev-C++ back on). Are you sure that that is the complete error message, the types do not seem to match your code? It is always a good idea (in fact requested in the "PLEASE READ BEFORE POSTING A QUESTION" thread) to post the complete compile log verbatim rather than extracting, editing, describing, or paraphrasing error messages.
VC++ reported:
main.cpp(13) : error C2664: 'Multiply' : cannot convert parameter 1 from 'double ' to 'const double const '
note the "const double const", which is the full type of the parameter, your report has just "const double "
The rules and syntax of const-correctness are somewhat arcane and I have not taken the trouble to make myself clear why this works, or whether it is sensible, but the following rather ugly declaration works.
void Multiply(const double const const M)
{
}
Clifford
I am sorry, I hope this message is clearer and complies better with the guidelines.
I am using 4.9.9.2 with XP.
The program runs well if I remove the first const in the function's variable list, appearing in "const double ** const M". Note the other pointers can be converted to pointers to constant objects -- just the double pointer is a problem.
Here is a complete simplified program (error log follows):
include <cstdlib>
include <iostream>
using namespace std;
void Multiply(const double* const M, const double * const V,
const int n, const int m, double * const MV)
{
for (int i=0; i<n; i++)
{
MV[i] = 0;
for (int j=0; j<m; j++)
{
MV[i] += M[i][j]V[j];
}
}
}
int main()
{
int n=2, m=3;
double** mat = new double* [n]; for (int i=0; i<n; i++) mat[i] = new double [m]; double * vec = new double [m]; double * matvec = new double [n]; for (int j=0; j<m; j++) { vec[j] = 1; for (int i=0; i<=n-1; i++) { mat[i][j] = i; } } Multiply(mat, vec, n, m, matvec); delete[] matvec; delete[] vec; for (int i=0; i<n; i++) delete[] mat[i]; delete[] mat; system("PAUSE"); return EXIT_SUCCESS;}
Compiler: Default compiler
Building Makefile: "C:\Documents and Settings\Richard\Desktop\CPP\Makefile.win"
Executing make...
make.exe -f "C:\Documents and Settings\Richard\Desktop\CPP\Makefile.win" all
g++.exe -c test.cpp -o test.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
test.cpp: In function
int main()': test.cpp:37: error: invalid conversion fromdouble' toconst double**' test.cpp:37: error: initializing argument 1 ofvoid Multiply(const double, const double, int, int, double)'make.exe: *** [test.o] Error 1
Execution terminated
Your new post does not include the fix I suggested. Did you try that?
On a different issue, don't put your project in C:\Documents and Settings\Richard\Desktop, the toolchain and the IDE both have bugs with handling paths containing spaces. It seems to work, until one day it does not.
void Multiply(const double const const M, const double * const V,
const int n, const int m, double * const MV)
Thank you very much! Your suggestion (syntax that I had not seen before)
void Multiply(const double const const M, const double * const V,
const int n, const int m, double * const MV)
worked very well.
This may help: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.5
Remember that you have three const here because you have two levels of indirection; a const pointer to a const pointer to a const value.
Clifford