5

I've got the following method within a header file called 'filter.h':

namespace std{

//some code

template <class T, class S, class BorderMethod>
tImage<T> binomial(const tImage<T> &in, const size_t k = 3) {
   //do some computations

  tImage<T> img = in.convolve<T, BorderMethod>(in, kernel);
  return img;
}
}

First thing I've noticed: the definition of this method takes place within the header-file. Is that standard procedure?

Now, the actual problem: The call to the method convolve won't work, even though in does possess such a method. Here's the definition of the method convolve within the class tImage<T>:

tImage<T> convolve(const gravis::tImage<T>& image, const gravis::tArray<typename tImageTraits<T>::Float_t>& kernel);

How do I have to call this function?

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
kafman
  • 2,862
  • 1
  • 29
  • 51
  • `in` is `const` argument, so a non-`const` member function cannot be invoked upon it. – hmjd Mar 18 '13 at 21:07
  • `binomial` seems to think `convolve` is a function template, but `convolve` doesn't look like a function template (just a member function of a template class). – aschepler Mar 18 '13 at 21:08
  • Why is your code in `namespace std`? It's illegal to add to the `std` namespace (other than specializations). – Praetorian Mar 18 '13 at 21:17

2 Answers2

6

First thing I've noticed: the definition of this method takes place within the header-file. Is that standard procedure?

Yes. Normally the definitions of function templates are put in headers. If they were relegated in a separate .cpp file, the compiler wouldn't be able to instantiate them implicitly when invoked, and unless you take proper action this will result in the linker complaining about undefined references. See this Q&A on StackOverflow for more information.

The call to the method convolve won't work, even though in does possess such a method.

convolve() is a non-const member function, and you are trying to invoke it through a reference to const. This is illegal and the compiler is telling you that.


Moreover, as correctly pointed out by JBentley in his answer, you cannot add new declarations or definitions to the std namespace. Per Paragraph 17.6.4.2.1/1 of the C++11 Standard:

The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. [...]

Community
  • 1
  • 1
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • Thank you! I called the namespace `std` because of lack of another name for this specific question ... but still, I wasn't aware of the fact, that I can't name my own namespace `std`, so thanks for that anyway. – kafman Mar 18 '13 at 22:18
3

Although not the cause of your problem, your code has undefined behaviour because you have illegally placed it inside the std namespace.

See this question or this one.

Community
  • 1
  • 1
JBentley
  • 6,099
  • 5
  • 37
  • 72