Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

namespace and external files confusion

24 views
Skip to first unread message

Popping mad

unread,
Nov 9, 2016, 10:37:24 AM11/9/16
to han...@nylxs.com, le...@nylxs.com
I'm doing this simple merge sort program and I'm tripping on this error

C++ call of overloaded ‘track(int*&, int&, int&, int*&)’ is ambiguous

I think this comes down to my confusion with namespace and I'm not finding a good reference with C++
on namespace usage in different files

I have merge.h

#ifndef MSORT_INC
#define MSORT_INC

namespace merge{

int max(int x, int y);
void track(int* , int , int , int* );
int sort(int &input , int size);

}
#endif /* ----- #ifndef MSORT_INC ----- */


and merge.cpp

#include "msort.h"

using namespace merge;

int max(int x, int y){
int ret;
x>y ? ret=x : ret=y;
return ret;
}

void track(int* in, int left, int right, int* space)
{
int i = 0;
int length = right - left;

if(right == left + 1){
return;
}

int mpt = length/2;
int pos1 = left;
int pos2 = left + mpt;
int pos3 = right;
//do the recursion now on the left and right branches
track(in, pos1, pos2, space);
track(in, pos2, pos3, space);
for(i = 0; i < length; i++)
{
if(pos1 < pos2 && ( pos2 == pos3 || max(in[pos1], in[pos2]) == in[pos1]))
{
space[i] = in[pos1];
pos1++;
}
else{
space[i] = in[pos2];
pos2++;
}
}
/* transfer array segment */
for(i = left; i < right; i++)
{
in[i] = space[i - left];
}
}



And the error I get is
C++ call of overloaded ‘track(int*&, int&, int&, int*&)’ is ambiguous

gvim sees it right away as well


|| gcc -M *.cpp >make.deps
|| g++ -Wall -ggdb -c msort.cpp
|| msort.cpp: In function ‘void track(int*, int, int, int*)’:
msort.cpp|25 col 29| error: call of overloaded ‘track(int*&, int&, int&, int*&)’ is ambiguous
|| track(in, pos1, pos2, space);
|| ^
msort.cpp|11 col 6| note: candidate: void track(int*, int, int, int*)
|| void track(int* in, int left, int right, int* space)
|| ^~~~~
msort.h|25 col 6| note: candidate: void merge::track(int*, int, int, int*)
|| void track(int* , int , int , int* );
|| ^~~~~


and it seems to identify the definition in the .cpp file as a separate declaration than the declation in the .h file
What am I doing wrong?


Scott Lurndal

unread,
Nov 9, 2016, 11:17:33 AM11/9/16
to
Popping mad <rai...@colition.gov> writes:
>I'm doing this simple merge sort program and I'm tripping on this error
>
>C++ call of overloaded ‘track(int*&, int&, int&, int*&)’ is ambiguous
>
>I think this comes down to my confusion with namespace and I'm not finding a good reference with C++
>on namespace usage in different files
>

"using namespace X" _doesn't_ imply that subsequent definitions are defined
in that namespace, only that namespace X is added to the default namespaces
list so you don't need to explictly specify it on a reference.

Either define the functions within a namespace block, or prefix
the definition function name with the namespace tag.

e.g.

void merge::track(int *, int, int, int*)
{
do stuff here
}

Bo Persson

unread,
Nov 9, 2016, 12:28:30 PM11/9/16
to
The using namespace merge; doesn't place all the following code in the
namespace, it just makes the names from the namespace visible.

You either have to reopen the namespace and place the functions inside
the braces, or prefix each function name with its namespace name:

int merge::max(int x, int y){
int ret;
x>y ? ret=x : ret=y;
return ret;
}

Otherwise you ARE actually defining new functions outside of the namespace.


Bo Persson


Popping mad

unread,
Nov 9, 2016, 11:41:31 PM11/9/16
to
On Wed, 09 Nov 2016 16:17:20 +0000, Scott Lurndal wrote:

> "using namespace X" _doesn't_ imply that subsequent definitions are
> defined in that namespace, only that namespace X is added to the default
> namespaces list so you don't need to explictly specify it on a
> reference.
>
> Either define the functions within a namespace block, or prefix the
> definition function name with the namespace tag.
>
> e.g.
>
> void merge::track(int *, int, int, int*)
> {
> do stuff here
> }



that is exactly right and beautifully explained. Thank You!!

Popping mad

unread,
Nov 9, 2016, 11:42:13 PM11/9/16
to
On Wed, 09 Nov 2016 18:28:20 +0100, Bo Persson wrote:

> The using namespace merge; doesn't place all the following code in the
> namespace, it just makes the names from the namespace visible.
>
> You either have to reopen the namespace and place the functions inside
> the braces, or prefix each function name with its namespace name:
>
> int merge::max(int x, int y){
> int ret;
> x>y ? ret=x : ret=y;
> return ret;
> }
>
> Otherwise you ARE actually defining new functions outside of the
> namespace.
>
>
> Bo Persson



thank you. That cleared up about 6 hours of my trying to figure that
out!!

0 new messages