Disabling / enabling a default parameter

I just don't get it. If I declare it in the .cpp, it works. If I remove the default parameter (changing it to just "bool sold"), it works. But this will not compile in the .h.

 
 Queue<string> analyzeQueue(Queue<Stock> holding, bool sold = false);



  Error: cannot overload functions distinguished by return type alone


This is the only analyzeQueue function I have.

What is happening?
Last edited on
you only need the default value on the declaration in the header file.

file.h
Queue<string> analyzeQueue(Queue<Stock> holding, bool sold = false);

file.cpp
1
2
3
4
Queue<string> analyzeQueue(Queue<Stock> holding, bool sold)
{
   // blah
}

I tried that. No luck.

.h
Queue<string> analyzeQueue(Queue<Stock> holding, bool sold = false);

.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Queue<string> analyzeQueue(Queue<Stock> holding, bool sold)
{
   Queue<string> analyzed;

   //copy by-value, shouldn't affect the source queue
   while (!holding.empty())
   {
      stringstream purchaseMessage;
      if (sold)
         purchaseMessage << "Sold ";
      else
         purchaseMessage << "Bought ";

      purchaseMessage << holding.front().numShares << " shares ";
      purchaseMessage << "at " << holding.front().price;

      if (sold)
         purchaseMessage << " for a profit of " << holding.front().profit;

      analyzed.push(purchaseMessage.str());
      holding.pop();
   }

   return analyzed;
}
Last edited on
overloaded can only mean one thing, there is more than one of them and they have the same parameter types.

check the header files that you include.
if its a class method check the base class.

check the rest of the output from the compiler, almost all compilers show you where they found the declarations.

1
2
3
4
5
1>  test.cpp
1>c:\dev\my_projects\consoleapplication1\pool.cpp(5): error C2556: 'float analyzeQueue(int,bool)' : overloaded function differs only by return type from 'int analyzeQueue(int,bool)'
1>          c:\dev\my_projects\consoleapplication1\test.h(3) : see declaration of 'analyzeQueue'
1>c:\dev\my_projects\consoleapplication1\test.cpp(5): error C2371: 'analyzeQueue' : redefinition; different basic types
1>          c:\dev\my_projects\consoleapplication1\test.h(3) : see declaration of 'analyzeQueue'
Topic archived. No new replies allowed.