It is possible to write, but the compiler dont find the function when I want to use func(x):

template <class T>
double func(const double& x, const double& error = 0)
{...}

I have to write separately, like this why?

template <class T>
double func(const double& x, const double& error)
{...}

template <class T>
double func(const double& x)
{double error = 0; ...}

Recommended Answers

All 5 Replies

Because that's not how C++ was defined.

It is possible to write, but the compiler dont find the function when I want to use func(x):

template <class T>
double func(const double& x, const double& error = 0)
{...}

Reference to ... what? NULL?

Are you passing by reference when you don't need to? Are you using T anywhere?

template <class T>
double func(const T &x, double error = 0)
{
   // ...
   return x;
}

int main(){
   double a = 1.0 / 3.0, err = 5;
   func(a,err);
}

??

but you cannaot call func(a), can you?

Instead of const double& error = 0 you could code it as const double* error = NULL . References can not be NULL, but pointers can.

but you cannaot call func(a), can you?

Simply giving it a try would have gotten you an answer much quicker.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.