Board index » Visual Studio » Error C2676 in one function but not another with namespace?

Error C2676 in one function but not another with namespace?

Visual Studio332
I am getting the following error in one function of my class but not the

other:



error C2676: binary '*' : 'GarXface4::Route' does not define this

operator or a conversion to a type acceptable to the predefined operator





For example if I define:



void RouteList::_Add(Route *pRoute)

{

Route *pRoute1;

}



This function of the class compiles fine.



but if I define:



Route* RouteList::Add(Route &Route)

{



Route* pRoute;

return pRoute;



}





then I get an error C2676.



Route and RouteList are both a part of the same namespace.



I can get it to compile if I specify the namespace



Route* RouteList::Add(Route &Route)

{



GarXface4::Route* pRoute;

return pRoute;



}





but why should I have too?











I have the following header file:



namespace GarXface4 {



class Route;

class Gps;



class GARXFACE4_API RouteList

{





friend Gps;

public:



RouteList(BOOL bAutoDeleteRoutes = TRUE);

~RouteList(void);



Route* Insert(unsigned index, Route &Route);

void Remove(unsigned nRoute);

void Clear(BOOL bDeleteRoutes=TRUE);



Route* Add(Route &Route);





unsigned GetCount();

Route *GetRoute(unsigned);





protected:

void _DeleteRoutes();

void _Add(Route* pRoute);

std::vector< Route*>m_coll;

BOOL m_bAutoDeleteRoutes;



};

}





and the following CPP file.





namespace GarXface4

{

RouteList::RouteList(BOOL bAutoDeleteRoutes)

{

m_bAutoDeleteRoutes = bAutoDeleteRoutes;



}



RouteList::~RouteList()

{

OutputDebugString("GarXface4::RouteList::~RouteList()\n");



if (m_bAutoDeleteRoutes)

_DeleteRoutes();





}



void RouteList::_Add(Route *pRoute)

{



m_coll.push_back(pRoute);

Route *pRoute1;

}



Route* RouteList::Add(Route &Route)

{



Route* p;



/*

Route* pRoute = new GarXface4::Route;



*pRoute = Route;

_Add(pRoute);





return pRoute;

*/



return NULL;





}



Route* RouteList::GetRoute(unsigned index)

{

//if (index>=m_coll.size())

// throw Exception(GX_ERR_INDEX_OUT_OF_RANGE,FALSE,0);



Route* pRoute = m_coll[index];

return pRoute;



}





unsigned RouteList::GetCount()

{



return (unsigned)m_coll.size();

}



void RouteList::_DeleteRoutes()

{



unsigned i;

unsigned c= (unsigned)m_coll.size();

for (i=0; i<c; i++)

{



Route *pRoute = m_coll[i];

#if defined _DEBUG

char szTemp[256];

sprintf(szTemp,"Deleteing routes %d\n",i);

OutputDebugString(szTemp);

#endif

delete pRoute;

}



}



void RouteList::Clear(BOOL bDeleteRoutes)

{

if (bDeleteRoutes)

_DeleteRoutes();



m_coll.clear();



}







void RouteList::Remove(unsigned nRoute)

{



Route *pRoute = GetRoute(nRoute);

delete pRoute;

m_coll.erase(m_coll.begin()+nRoute);



}



Route* RouteList::Insert(unsigned int index, Route& Route)

{

// if (index>=m_coll.size())

// throw Exception(GX_ERR_INDEX_OUT_OF_RANGE,FALSE,0);





GarXface4::Route *pRoute = new GarXface4::Route;



*pRoute = Route;



m_coll.insert(m_coll.begin()+index,pRoute);



return pRoute;



}

}


-
 

Re:Error C2676 in one function but not another with namespace?

Bruce,

Your problem lies in the fact that you named your parameter Route -- the same as the class. The

compiler thinks you are trying to multiply the parameter times something, and hence complains about

the binary (two operands) multiplication operator not being defined for that type.



"Bruce Stemplewski" <Bruce.Stemplewski@nospamstempsoft.com>wrote in message

Quote
I am getting the following error in one function of my class but not the other:



error C2676: binary '*' : 'GarXface4::Route' does not define this operator or a conversion to a

type acceptable to the predefined operator





For example if I define:



void RouteList::_Add(Route *pRoute)

{

Route *pRoute1;

}



This function of the class compiles fine.



but if I define:



Route* RouteList::Add(Route &Route)

{



Route* pRoute;

return pRoute;



}





then I get an error C2676.



Route and RouteList are both a part of the same namespace.



I can get it to compile if I specify the namespace



Route* RouteList::Add(Route &Route)

{



GarXface4::Route* pRoute;

return pRoute;



}





but why should I have too?











I have the following header file:



namespace GarXface4 {



class Route;

class Gps;



class GARXFACE4_API RouteList

{





friend Gps;

public:



RouteList(BOOL bAutoDeleteRoutes = TRUE);

~RouteList(void);



Route* Insert(unsigned index, Route &Route);

void Remove(unsigned nRoute);

void Clear(BOOL bDeleteRoutes=TRUE);



Route* Add(Route &Route);



unsigned GetCount();

Route *GetRoute(unsigned);





protected:

void _DeleteRoutes();

void _Add(Route* pRoute);

std::vector< Route*>m_coll;

BOOL m_bAutoDeleteRoutes;



};

}





and the following CPP file.





namespace GarXface4

{

RouteList::RouteList(BOOL bAutoDeleteRoutes)

{

m_bAutoDeleteRoutes = bAutoDeleteRoutes;



}



RouteList::~RouteList()

{

OutputDebugString("GarXface4::RouteList::~RouteList()\n");



if (m_bAutoDeleteRoutes)

_DeleteRoutes();





}



void RouteList::_Add(Route *pRoute)

{



m_coll.push_back(pRoute);

Route *pRoute1;

}



Route* RouteList::Add(Route &Route)

{



Route* p;



/*

Route* pRoute = new GarXface4::Route;



*pRoute = Route;

_Add(pRoute);





return pRoute;

*/



return NULL;





}



Route* RouteList::GetRoute(unsigned index)

{

//if (index>=m_coll.size())

// throw Exception(GX_ERR_INDEX_OUT_OF_RANGE,FALSE,0);



Route* pRoute = m_coll[index]; return pRoute;



}





unsigned RouteList::GetCount()

{



return (unsigned)m_coll.size();

}



void RouteList::_DeleteRoutes()

{



unsigned i;

unsigned c= (unsigned)m_coll.size();

for (i=0; i<c; i++)

{



Route *pRoute = m_coll[i];

#if defined _DEBUG

char szTemp[256];

sprintf(szTemp,"Deleteing routes %d\n",i);

OutputDebugString(szTemp);

#endif

delete pRoute;

}



}



void RouteList::Clear(BOOL bDeleteRoutes)

{

if (bDeleteRoutes)

_DeleteRoutes();



m_coll.clear();



}







void RouteList::Remove(unsigned nRoute)

{



Route *pRoute = GetRoute(nRoute);

delete pRoute;

m_coll.erase(m_coll.begin()+nRoute);



}



Route* RouteList::Insert(unsigned int index, Route& Route)

{

// if (index>=m_coll.size())

// throw Exception(GX_ERR_INDEX_OUT_OF_RANGE,FALSE,0);





GarXface4::Route *pRoute = new GarXface4::Route;



*pRoute = Route;



m_coll.insert(m_coll.begin()+index,pRoute);



return pRoute;



}

}





-

Re:Error C2676 in one function but not another with namespace?

Thanks!



Wow how obvious! I feel like an idiot! :)



Scot T Brennecke wrote:

Quote
Bruce,

Your problem lies in the fact that you named your parameter Route -- the same as the class. The

compiler thinks you are trying to multiply the parameter times something, and hence complains about

the binary (two operands) multiplication operator not being defined for that type.



"Bruce Stemplewski" <Bruce.Stemplewski@nospamstempsoft.com>wrote in message

news:bwTLe.2492$Hf6.1035@trndny07...



>I am getting the following error in one function of my class but not the other:

>

>error C2676: binary '*' : 'GarXface4::Route' does not define this operator or a conversion to a

>type acceptable to the predefined operator

>

>

>For example if I define:

>

>void RouteList::_Add(Route *pRoute)

>{

>Route *pRoute1;

>}

>

>This function of the class compiles fine.

>

>but if I define:

>

>Route* RouteList::Add(Route &Route)

>{

>

>Route* pRoute;

>return pRoute;

>

>}

>

>

>then I get an error C2676.

>

>Route and RouteList are both a part of the same namespace.

>

>I can get it to compile if I specify the namespace

>

>Route* RouteList::Add(Route &Route)

>{

>

>GarXface4::Route* pRoute;

>return pRoute;

>

>}

>

>

>but why should I have too?

>

>

>

>

>

>I have the following header file:

>

>namespace GarXface4 {

>

>class Route;

>class Gps;

>

>class GARXFACE4_API RouteList

>{

>

>

>friend Gps;

>public:

>

>RouteList(BOOL bAutoDeleteRoutes = TRUE);

>~RouteList(void);

>

>Route* Insert(unsigned index, Route &Route);

>void Remove(unsigned nRoute);

>void Clear(BOOL bDeleteRoutes=TRUE);

>

>Route* Add(Route &Route);

>

>unsigned GetCount();

>Route *GetRoute(unsigned);

>

>

>protected:

>void _DeleteRoutes();

>void _Add(Route* pRoute);

>std::vector< Route*>m_coll;

>BOOL m_bAutoDeleteRoutes;

>

>};

>}

>

>

>and the following CPP file.

>

>

>namespace GarXface4

>{

>RouteList::RouteList(BOOL bAutoDeleteRoutes)

>{

>m_bAutoDeleteRoutes = bAutoDeleteRoutes;

>

>}

>

>RouteList::~RouteList()

>{

>OutputDebugString("GarXface4::RouteList::~RouteList()\n");

>

>if (m_bAutoDeleteRoutes)

>_DeleteRoutes();

>

>

>}

>

>void RouteList::_Add(Route *pRoute)

>{

>

>m_coll.push_back(pRoute);

>Route *pRoute1;

>}

>

>Route* RouteList::Add(Route &Route)

>{

>

>Route* p;

>

>/*

>Route* pRoute = new GarXface4::Route;

>

>*pRoute = Route;

>_Add(pRoute);

>

>

>return pRoute;

>*/

>

>return NULL;

>

>

>}

>

>Route* RouteList::GetRoute(unsigned index)

>{

>//if (index>=m_coll.size())

>// throw Exception(GX_ERR_INDEX_OUT_OF_RANGE,FALSE,0);

>

>Route* pRoute = m_coll[index]; return pRoute;

>

>}

>

>

>unsigned RouteList::GetCount()

>{

>

>return (unsigned)m_coll.size();

>}

>

>void RouteList::_DeleteRoutes()

>{

>

>unsigned i;

>unsigned c= (unsigned)m_coll.size();

>for (i=0; i<c; i++)

>{

>

>Route *pRoute = m_coll[i];

>#if defined _DEBUG

>char szTemp[256];

>sprintf(szTemp,"Deleteing routes %d\n",i);

>OutputDebugString(szTemp);

>#endif

>delete pRoute;

>}

>

>}

>

>void RouteList::Clear(BOOL bDeleteRoutes)

>{

>if (bDeleteRoutes)

>_DeleteRoutes();

>

>m_coll.clear();

>

>}

>

>

>

>void RouteList::Remove(unsigned nRoute)

>{

>

>Route *pRoute = GetRoute(nRoute);

>delete pRoute;

>m_coll.erase(m_coll.begin()+nRoute);

>

>}

>

>Route* RouteList::Insert(unsigned int index, Route& Route)

>{

>// if (index>=m_coll.size())

>// throw Exception(GX_ERR_INDEX_OUT_OF_RANGE,FALSE,0);

>

>

>GarXface4::Route *pRoute = new GarXface4::Route;

>

>*pRoute = Route;

>

>m_coll.insert(m_coll.begin()+index,pRoute);

>

>return pRoute;

>

>}

>}







-

Re:Error C2676 in one function but not another with namespace?

No problem. Many people criticize the concept of "Hungarian Notation", but this problem is one of

the many arguments in favor of using it. If you had name your parameter rRoute (starting with an r

to indicate a reference), you wouldn't have collided with the class name.



"Bruce Stemplewski" <Bruce.Stemplewski@nospamstempsoft.com>wrote in message

Quote
Thanks!



Wow how obvious! I feel like an idiot! :)



Scot T Brennecke wrote:

>Bruce,

>Your problem lies in the fact that you named your parameter Route -- the same as the class.

>The compiler thinks you are trying to multiply the parameter times something, and hence complains

>about the binary (two operands) multiplication operator not being defined for that type.

>

>"Bruce Stemplewski" <Bruce.Stemplewski@nospamstempsoft.com>wrote in message

>news:bwTLe.2492$Hf6.1035@trndny07...

>

>>I am getting the following error in one function of my class but not the other:

>>

>>error C2676: binary '*' : 'GarXface4::Route' does not define this operator or a conversion to a

>>type acceptable to the predefined operator

>>

>>

>>For example if I define:

>>

>>void RouteList::_Add(Route *pRoute)

>>{

>>Route *pRoute1;

>>}

>>

>>This function of the class compiles fine.

>>

>>but if I define:

>>

>>Route* RouteList::Add(Route &Route)

>>{

>>

>>Route* pRoute;

>>return pRoute;

>>

>>}

>>

>>

>>then I get an error C2676.

>>

>>Route and RouteList are both a part of the same namespace.

>>

>>I can get it to compile if I specify the namespace

>>

>>Route* RouteList::Add(Route &Route)

>>{

>>

>>GarXface4::Route* pRoute;

>>return pRoute;

>>

>>}

>>

>>

>>but why should I have too?

>>

>>

>>

>>

>>

>>I have the following header file:

>>

>>namespace GarXface4 {

>>

>>class Route;

>>class Gps;

>>

>>class GARXFACE4_API RouteList

>>{

>>

>>

>>friend Gps;

>>public:

>>

>>RouteList(BOOL bAutoDeleteRoutes = TRUE);

>>~RouteList(void);

>>

>>Route* Insert(unsigned index, Route &Route);

>>void Remove(unsigned nRoute);

>>void Clear(BOOL bDeleteRoutes=TRUE);

>>

>>Route* Add(Route &Route);

>>

>>unsigned GetCount();

>>Route *GetRoute(unsigned);

>>

>>

>>protected:

>>void _DeleteRoutes();

>>void _Add(Route* pRoute);

>>std::vector< Route*>m_coll;

>>BOOL m_bAutoDeleteRoutes;

>>

>>};

>>}

>>

>>

>>and the following CPP file.

>>

>>

>>namespace GarXface4

>>{

>>RouteList::RouteList(BOOL bAutoDeleteRoutes)

>>{

>>m_bAutoDeleteRoutes = bAutoDeleteRoutes;

>>

>>}

>>

>>RouteList::~RouteList()

>>{

>>OutputDebugString("GarXface4::RouteList::~RouteList()\n");

>>

>>if (m_bAutoDeleteRoutes)

>>_DeleteRoutes();

>>

>>

>>}

>>

>>void RouteList::_Add(Route *pRoute)

>>{

>>

>>m_coll.push_back(pRoute);

>>Route *pRoute1;

>>}

>>

>>Route* RouteList::Add(Route &Route)

>>{

>>

>>Route* p;

>>

>>/*

>>Route* pRoute = new GarXface4::Route;

>>

>>*pRoute = Route;

>>_Add(pRoute);

>>

>>

>>return pRoute;

>>*/

>>

>>return NULL;

>>

>>

>>}

>>

>>Route* RouteList::GetRoute(unsigned index)

>>{

>>//if (index>=m_coll.size())

>>// throw Exception(GX_ERR_INDEX_OUT_OF_RANGE,FALSE,0);

>>

>>Route* pRoute = m_coll[index]; return pRoute;

>>

>>}

>>

>>

>>unsigned RouteList::GetCount()

>>{

>>

>>return (unsigned)m_coll.size();

>>}

>>

>>void RouteList::_DeleteRoutes()

>>{

>>

>>unsigned i;

>>unsigned c= (unsigned)m_coll.size();

>>for (i=0; i<c; i++)

>>{

>>

>>Route *pRoute = m_coll[i];

>>#if defined _DEBUG

>>char szTemp[256];

>>sprintf(szTemp,"Deleteing routes %d\n",i);

>>OutputDebugString(szTemp);

>>#endif

>>delete pRoute;

>>}

>>

>>}

>>

>>void RouteList::Clear(BOOL bDeleteRoutes)

>>{

>>if (bDeleteRoutes)

>>_DeleteRoutes();

>>

>>m_coll.clear();

>>

>>}

>>

>>

>>

>>void RouteList::Remove(unsigned nRoute)

>>{

>>

>>Route *pRoute = GetRoute(nRoute);

>>delete pRoute;

>>m_coll.erase(m_coll.begin()+nRoute);

>>

>>}

>>

>>Route* RouteList::Insert(unsigned int index, Route& Route)

>>{

>>// if (index>=m_coll.size())

>>// throw Exception(GX_ERR_INDEX_OUT_OF_RANGE,FALSE,0);

>>

>>

>>GarXface4::Route *pRoute = new GarXface4::Route;

>>

>>*pRoute = Route;

>>

>>m_coll.insert(m_coll.begin()+index,pRoute);

>>

>>return pRoute;

>>

>>}

>>}

>

>



-

Re:Error C2676 in one function but not another with namespace?



"Scot T Brennecke" <ScotB@MVPs.spamhater.org>skrev i meddelandet

Quote
No problem. Many people criticize the concept of "Hungarian

Notation", but this problem is one of the many arguments in favor of

using it. If you had name your parameter rRoute (starting with an r

to indicate a reference), you wouldn't have collided with the class

name.



On the other hand, having names that differ only in the hungarian

warts is a terrible convention. You will just end up in the

lParam-wParam marshes - some of the worst names ever chosen.





Bo Persson





Quote


"Bruce Stemplewski" <Bruce.Stemplewski@nospamstempsoft.com>wrote in

message news:p59Me.599$wb.278@trndny09...

>Thanks!

>

>Wow how obvious! I feel like an idiot! :)

>

>Scot T Brennecke wrote:

>>Bruce,

>>Your problem lies in the fact that you named your parameter

>>Route -- the same as the class. The compiler thinks you are trying

>>to multiply the parameter times something, and hence complains

>>about the binary (two operands) multiplication operator not being

>>defined for that type.

>>

>>"Bruce Stemplewski" <Bruce.Stemplewski@nospamstempsoft.com>wrote

>>in message news:bwTLe.2492$Hf6.1035@trndny07...

>>

>>>I am getting the following error in one function of my class but

>>>not the other:

>>>

>>>error C2676: binary '*' : 'GarXface4::Route' does not define this

>>>operator or a conversion to a type acceptable to the predefined

>>>operator

>>>

>>>

>>>For example if I define:

>>>

>>>void RouteList::_Add(Route *pRoute)

>>>{

>>>Route *pRoute1;

>>>}

>>>

>>>This function of the class compiles fine.

>>>

>>>but if I define:

>>>

>>>Route* RouteList::Add(Route &Route)

>>>{

>>>

>>>Route* pRoute;

>>>return pRoute;

>>>

>>>}

>>>

>>>

>>>then I get an error C2676.

>>>

>>>Route and RouteList are both a part of the same namespace.

>>>

>>>I can get it to compile if I specify the namespace

>>>

>>>Route* RouteList::Add(Route &Route)

>>>{

>>>

>>>GarXface4::Route* pRoute;

>>>return pRoute;

>>>

>>>}

>>>

>>>

>>>but why should I have too?

>>>

>>>

>>>

>>>

>>>

>>>I have the following header file:

>>>

>>>namespace GarXface4 {

>>>

>>>class Route;

>>>class Gps;

>>>

>>>class GARXFACE4_API RouteList

>>>{

>>>

>>>

>>>friend Gps;

>>>public:

>>>

>>>RouteList(BOOL bAutoDeleteRoutes = TRUE);

>>>~RouteList(void);

>>>

>>>Route* Insert(unsigned index, Route &Route);

>>>void Remove(unsigned nRoute);

>>>void Clear(BOOL bDeleteRoutes=TRUE);

>>>

>>>Route* Add(Route &Route);

>>>

>>>unsigned GetCount();

>>>Route *GetRoute(unsigned);

>>>

>>>

>>>protected:

>>>void _DeleteRoutes();

>>>void _Add(Route* pRoute);

>>>std::vector< Route*>m_coll;

>>>BOOL m_bAutoDeleteRoutes;

>>>

>>>};

>>>}

>>>

>>>

>>>and the following CPP file.

>>>

>>>

>>>namespace GarXface4

>>>{

>>>RouteList::RouteList(BOOL bAutoDeleteRoutes)

>>>{

>>>m_bAutoDeleteRoutes = bAutoDeleteRoutes;

>>>

>>>}

>>>

>>>RouteList::~RouteList()

>>>{

>>>OutputDebugString("GarXface4::RouteList::~RouteList()\n");

>>>

>>>if (m_bAutoDeleteRoutes)

>>>_DeleteRoutes();

>>>

>>>

>>>}

>>>

>>>void RouteList::_Add(Route *pRoute)

>>>{

>>>

>>>m_coll.push_back(pRoute);

>>>Route *pRoute1;

>>>}

>>>

>>>Route* RouteList::Add(Route &Route)

>>>{

>>>

>>>Route* p;

>>>

>>>/*

>>>Route* pRoute = new GarXface4::Route;

>>>

>>>*pRoute = Route;

>>>_Add(pRoute);

>>>

>>>

>>>return pRoute;

>>>*/

>>>

>>>return NULL;

>>>

>>>

>>>}

>>>

>>>Route* RouteList::GetRoute(unsigned index)

>>>{

>>>//if (index>=m_coll.size())

>>>// throw Exception(GX_ERR_INDEX_OUT_OF_RANGE,FALSE,0);

>>>

>>>Route* pRoute = m_coll[index]; return pRoute;

>>>

>>>}

>>>

>>>

>>>unsigned RouteList::GetCount()

>>>{

>>>

>>>return (unsigned)m_coll.size();

>>>}

>>>

>>>void RouteList::_DeleteRoutes()

>>>{

>>>

>>>unsigned i;

>>>unsigned c= (unsigned)m_coll.size();

>>>for (i=0; i<c; i++)

>>>{

>>>

>>>Route *pRoute = m_coll[i];

>>>#if defined _DEBUG

>>>char szTemp[256];

>>>sprintf(szTemp,"Deleteing routes %d\n",i);

>>>OutputDebugString(szTemp);

>>>#endif

>>>delete pRoute;

>>>}

>>>

>>>}

>>>

>>>void RouteList::Clear(BOOL bDeleteRoutes)

>>>{

>>>if (bDeleteRoutes)

>>>_DeleteRoutes();

>>>

>>>m_coll.clear();

>>>

>>>}

>>>

>>>

>>>

>>>void RouteList::Remove(unsigned nRoute)

>>>{

>>>

>>>Route *pRoute = GetRoute(nRoute);

>>>delete pRoute;

>>>m_coll.erase(m_coll.begin()+nRoute);

>>>

>>>}

>>>

>>>Route* RouteList::Insert(unsigned int index, Route& Route)

>>>{

>>>// if (index>=m_coll.size())

>>>// throw Exception(GX_ERR_INDEX_OUT_OF_RANGE,FALSE,0);

>>>

>>>

>>>GarXface4::Route *pRoute = new GarXface4::Route;

>>>

>>>*pRoute = Route;

>>>

>>>m_coll.insert(m_coll.begin()+index,pRoute);

>>>

>>>return pRoute;

>>>

>>>}

>>>}

>>

>>







-

Re:Error C2676 in one function but not another with namespace?

Bo Persson wrote:

Quote
"Scot T Brennecke" <ScotB@MVPs.spamhater.org>skrev i

meddelandet news:ODW1g0poFHA.3300@TK2MSFTNGP15.phx.gbl...

>No problem. Many people criticize the concept of

>"Hungarian Notation", but this problem is one of the

>many arguments in favor of using it. If you had name

>your parameter rRoute (starting with an r to indicate a

>reference), you wouldn't have collided with the class

>name.



On the other hand, having names that differ only in the

hungarian warts is a terrible convention. You will just

end up in the lParam-wParam marshes - some of the worst

names ever chosen.



That's why one should not use hungarian notation for

implementation/platform specific things. However, using

hungarian notation for _conceptual_ things makes code more

readable. Consider



instead of



lParam // parameter of type LONG

wParam // parameter of type WORD



do that



nParam1 // first arbitrary numeric parameter

nParam2 // second arbitrary numeric parameter



Then nParam1 and nParam2 could be in sync with whatever

current type of parameters is and newbies wouldn't be

confused by irrelevant prefixes.





-