My environment is Ubuntu 12.04. I use g++ compiler
I need to try to compile and run a C++ program devoted to a special variant of Fourier Transform: Spherical Harmonics Expansion. It takes place on a unit sphere. The source code can be had here:
The code seems to be written with Microsoft Visual Studio in mind but I am trying to use g++. I get a number of serious errors which I do not understand how to correct. I mostly do gfortran code.
1 2 3 4 5
/// Multiplies a SHProjection by a constant factor.
/// This is equivalent to a scaling of the represented function.
116:=> template < typename TReal, class TFunction, typename TScalar >
friendconst SHProjection operator * <> ( TScalar lhs, const SHProjection& rhs );
Error:
1 2 3
SHProjection.h:116:16: error: declaration of ‘class TReal’
template < typename TReal, class TFunction, typename TScalar >
^
1 2 3 4
template< typename TReal > // <== line 44
class SHProjection
{
public:
/// Multiplies a SHProjection by a constant factor.
/// This is equivalent to a scaling of the represented function.
template < typename TReal, class TFunction, typename TScalar >
friendconst SHProjection operator * <> ( TScalar lhs, const SHProjection& rhs );
The last line in the code above is line 117
Error:
1 2 3
SHProjection.h:117:84: error: invalid use of template-id ‘operator*<>’ in declaration of primary templatefriendconst SHProjection operator * <> ( TScalar lhs, const SHProjection& rhs );
^
There are a number of other errors but I hope to learn from these three samples and perhaps correct some of the rest myself.
I wonder if this code can be salvaged? Also does it make sense to proceed? If the original designer posted the code with such errors, how much is it worth?
What you can do is move the definition of the friend function to be inside the class (at the point of declaration), or change `T' for another thing (but that changes the meaning)
template <class T>
class foo{
private:
T value;
template<class Scalar> //now is just one parameter
friend foo operator*(Scalar lhs, const foo &rhs){
//defined here
rhs.value; //it's friend so can access it
return rhs;
}
//variant
template<class U, class Scalar>
friend foo<U> operator*(Scalar lhs, const foo<U> &rhs);
};
//the definition may remain untoched
template<class T, class Scalar> //the change is only needed in the declaration
foo<T> operator*(Scalar lhs, const foo<T> &rhs){
rhs.value; //it's friend so can access it
foo<int> a; a.value = 42; //however, it is friend of all foo, so this is valid
return rhs;
}
Edit: I cannot download the code as it asked for a log in, this is a guess based on the error message.
Thanks everyone who contributed. I tried clang-3.5 and got pretty much similar errors. I also wanted to post the original source files like SHProjection.h but the post was rejected on the grounds that it was more than the max which is 8192. In fact it was about a hundred characters less. I don't know what to do at this point. With my limited experience in C++ I am looking for a "cheap" solution but it is not forthcoming.
Thank you very much. I've kind of lost faith in the code, so my attention's been diverted. I really appreciate it. @ne555, thank you so much also. Now I will try to compile it.
OK guys. Thank you again. The file SHProjection.h compiled nicely with the changes you suggested. Now I got two compile errors in SHProjection.inl. They are:
1 2 3 4 5 6 7 8 9 10
....$g++ main.cpp
In file included from SHProjection.h:199:0,
from main.cpp:1:
SHProjection.inl: In function ‘std::ostream& operator<<(std::ostream&, const SHProjection<TReal>&)’:
SHProjection.inl:614:8: error: need ‘typename’ before ‘SHProjection<TReal>::SHCoefficients’ because ‘SHProjection<TReal>’ is a dependent scope
const SHProjection< TReal >::SHCoefficients& coeffs = projection.m_coefficients;
^
SHProjection.inl:615:24: error: ‘coeffs’ was not declared in this scope
const std::size_t n = coeffs.size(); ^