MESSAGE
DATE | 2016-11-09 |
FROM | Christopher League
|
SUBJECT | Re: [Learn] namespace and external files confusion
|
From learn-bounces-at-nylxs.com Wed Nov 9 11:05:47 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id E6E84161312; Wed, 9 Nov 2016 11:05:46 -0500 (EST) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from liucs.net (contrapunctus.net [174.136.110.10]) by mrbrklyn.com (Postfix) with ESMTP id F1720160E77 for ; Wed, 9 Nov 2016 11:05:44 -0500 (EST) Received: from localhost (182631c.cst.lightpath.net [24.38.3.28]) by liucs.net (Postfix) with ESMTPSA id 25006E097 for ; Wed, 9 Nov 2016 11:05:43 -0500 (EST) From: Christopher League To: learn-at-nylxs.com In-Reply-To: References: User-Agent: Notmuch/0.21 (http://notmuchmail.org) Emacs/25.1.1 (x86_64-unknown-linux-gnu) Date: Wed, 09 Nov 2016 11:05:38 -0500 Message-ID: <877f8cocz1.fsf-at-contrapunctus.net> MIME-Version: 1.0 Subject: Re: [Learn] namespace and external files confusion X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: multipart/mixed; boundary="===============0689396626==" Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
--===============0689396626== Content-Type: multipart/signed; boundary="===-=-="; micalg=pgp-sha256; protocol="application/pgp-signature"
--===-=-= Content-Type: multipart/mixed; boundary="=-=-="
--=-=-= Content-Type: multipart/alternative; boundary="==-=-="
--==-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable
In the .cpp file, it's not enough to say `using namespace merge`. You also want to be *defining* things in that same namespace. Otherwise you are defining `::track` but trying to recursively call `merge::track`.
Change that `using namespace merge` into `namespace merge { ....... }` that covers the rest of the definitions.
That compiles.
CL
Ruben Safir writes:
> [This mail was also posted to comp.lang.c++.] > > I'm doing this simple merge sort program and I'm tripping on this error > > C++ call of overloaded =E2=80=98track(int*&, int&, int&, int*&)=E2=80=99 = 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=3Dx : ret=3Dy; > return ret; > } > > void track(int* in, int left, int right, int* space) > { > int i =3D 0; > int length =3D right - left; >=20=09 > if(right =3D=3D left + 1){ > return; > } > int mpt =3D length/2; > int pos1 =3D left; > int pos2 =3D left + mpt; > int pos3 =3D right; > //do the recursion now on the left and right branches > track(in, pos1, pos2, space); > track(in, pos2, pos3, space); > for(i =3D 0; i < length; i++) > { > if(pos1 < pos2 && ( pos2 =3D=3D pos3 || max(in[pos1], in[pos2]) =3D=3D > in[pos1])) > { > space[i] =3D in[pos1]; > pos1++; > } > else{ > space[i] =3D in[pos2]; > pos2++; > } > } > /* transfer array segment */ > for(i =3D left; i < right; i++) > { > in[i] =3D space[i - left]; > } > } > > > > And the error I get is > C++ call of overloaded =E2=80=98track(int*&, int&, int&, int*&)=E2=80=99 = is ambiguous > > gvim sees it right away as well > > > || gcc -M *.cpp >make.deps > || g++ -Wall -ggdb -c msort.cpp > || msort.cpp: In function =E2=80=98void track(int*, int, int, int*)=E2=80= =99: > msort.cpp|25 col 29| error: call of overloaded =E2=80=98track(int*&, int&= , int&, > int*&)=E2=80=99 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? > > > > --=20 > So many immigrant groups have swept through our town > that Brooklyn, like Atlantis, reaches mythological > proportions in the mind of the world - RI Safir 1998 > http://www.mrbrklyn.com > > DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 > http://www.nylxs.com - Leadership Development in Free Software > http://www2.mrbrklyn.com/resources - Unpublished Archive > http://www.coinhangout.com - coins! > http://www.brooklyn-living.com > > Being so tracked is for FARM ANIMALS and and extermination camps, > but incompatible with living as a free human being. -RI Safir 2013 > _______________________________________________ > Learn mailing list > Learn-at-nylxs.com > http://lists.mrbrklyn.com/mailman/listinfo/learn
--==-=-= Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
1.0, user-scalable=3Dyes">
In the .cpp file, it=E2=80=99s not enough to say using namespace m= erge . You also want to be defining things in that same name= space. Otherwise you are defining ::track but trying to recurs= ively call merge::track .
Change that using namespace merge into namespace merg= e { ....... } that covers the rest of the definitions.
That compiles.
CL
Ruben Safir ruben-at-mrbrklyn.com= writes:
[This mail was also posted to comp.lang.c++.]
I=E2=80=99m doing this simple merge sort program and I=E2=80=99m trippin= g on this error
C++ call of overloaded =E2=80=98track(int&, int&, int&, = int&)=E2=80=99 is ambiguous
I think this comes down to my confusion with namespace and I=E2=80=99m n= ot finding a good reference with C++ on namespace usage in different files<= /p>
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(in= t &input , int size); } #endif /* =E2=80=94=E2=80=93 #ifndef MSORT_INC = =E2=80=94=E2=80=93 */
and merge.cpp
include =E2=80=9Cmsort.h=E2=80=9D
using namespace merge;
int max(int x, int y){ int ret; x>y ? ret=3Dx : ret=3Dy; return ret; = }
void track(int* in, int left, int right, int* space) { int i =3D 0; int = length =3D right - left;
if(right =3D=3D left + 1){ return; } int mpt =3D length/2; int pos1 =3D = left; int pos2 =3D left + mpt; int pos3 =3D right; //do the recursion now o= n the left and right branches track(in, pos1, pos2, space); track(in, pos2,= pos3, space); for(i =3D 0; i < length; i++) { if(pos1 < pos2 &&a= mp; ( pos2 =3D=3D pos3 || max(in[pos1], in[pos2]) =3D=3D in[pos1])) { space= [i] =3D in[pos1]; pos1++; } else{ space[i] =3D in[pos2]; pos2++; } } /* tra= nsfer array segment */ for(i =3D left; i < right; i++) { in[i] =3D space= [i - left]; } }
And the error I get is C++ call of overloaded =E2=80=98track(int&= ;, int&, int&, int&)=E2=80=99 is ambiguous
gvim sees it right away as well
|| gcc -M .cpp >make.deps || g++ -Wall -ggdb -c msort.cpp || msor= t.cpp: In function =E2=80=98void track(int, int, int, int)=E2=80= =99: msort.cpp|25 col 29| error: call of overloaded =E2=80=98track(int&= amp;, int&, int&, int&)=E2=80=99 is ambiguous || track(in,= pos1, pos2, space); || ^ msort.cpp|11 col 6| note: candidate: void track(i= nt, int, int, int) || void track(int in, int left, int right,= int* space) || ^~~ msort.h|25 col 6| note: candidate: void merg= e::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 d= eclaration than the declation in the .h file What am I doing wrong?
=E2=80=93 So many immigrant groups have swept through our town that Broo= klyn, like Atlantis, reaches mythological proportions in the mind of the wo= rld - RI Safir 1998 http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 http://www.nylxs.= com - Leadership Development in Free Software http://www2.mrbrklyn.com/reso= urces - Unpublished Archive http://www.coinhangout.com - coins! http://www.= brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps, but in= compatible with living as a free human being. -RI Safir 2013 ______________= _________________________________ Learn mailing list Learn-at-nylxs.com http:/= /lists.mrbrklyn.com/mailman/listinfo/learn
--==-=-=--
--=-=-=--
--===-=-= Content-Type: application/pgp-signature; name="signature.asc"
-----BEGIN PGP SIGNATURE-----
iQEcBAEBCAAGBQJYI0lSAAoJEGuLsz1PMbCLn88IAIiVIytG7BZOyptGkyLlvnD6 SdPmrvkkxcPz5xhIVGBT1EkFfanYJWYzFggbJ6jfYkdZxRk9nOm+rHtBrS5SZ1rK XLjArCFplSfjS/TIg/ldoS7LXU3+W+5n+aXIl0h380yynrkwgCPtBoV3tGgpqNv/ F4aiZmZgfm83CBuC/7M3iWTcXutManvZj7lUfP/DNcqb9rWvIWkRvUJ4MGPw1rkR 8WQ2aBvM4iC709+RhKs89jTMeLvi/gLK83zEhR5zU1IX9wd57mwR5jgWNE8TIU4D wZASQk93R5s4oG82+oi/dtAID1BMHBdgVHz/BO25ZbjFEbiDyiZFYt/cxdrEOiU= =2pjb -----END PGP SIGNATURE----- --===-=-=--
--===============0689396626== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline
_______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
--===============0689396626==--
|
|