473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Template and Namespace Ambiguity Issue

Hi All

Given the following:

// NamespaceTemplate.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"

namespace N1
{
class C1
{
public:
void f() {}
void g() {}
};

class C2
{
public:
void h(C1 info) {}
void i() {}
};
}

namespace N2
{
class C1
{
public:
void f() {}
void g() {}
};

class C2
{
public:
void h(C1 info) {}
void i() {}
};
}

template <typename T>
void k(T x)
{
C1 theProblem; // Ambiguous!! How to create the correct object???
x.h(theProblem);
x.i();
}

using namespace N1;
using namespace N2;
int main(int argc, char* argv[])
{
N1::C2 b;
k(b); // Invoke Template function

N2::C2 d;
k(d); // Invoke Template function

return 0;
}

What is the best way (that is, the most common way) to resolve the
issue above? I get an ambiguous symbol error when compiling, which
does make sense. I'm looking for a way to resolve this without
resorting to passing in enumerated types representing objects or
something like that.

Thanks.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jun 19 '07 #1
6 2325
bo*******@yahoo.com wrote:
Hi All

Given the following:

// NamespaceTemplate.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"

namespace N1
{
class C1
{
public:
void f() {}
void g() {}
};

class C2
{
public:
typedef C1 buddy;
void h(C1 info) {}
void i() {}
};
}

namespace N2
{
class C1
{
public:
void f() {}
void g() {}
};

class C2
{
public:

typedef C1 buddy;
void h(C1 info) {}
void i() {}
};
}

template <typename T>
void k(T x)
{
C1 theProblem; // Ambiguous!! How to create the correct object???
typename T::buddy theProblem;
x.h(theProblem);
x.i();
}

using namespace N1;
using namespace N2;
Do you really need those 'using' directives here?
int main(int argc, char* argv[])
{
N1::C2 b;
k(b); // Invoke Template function

N2::C2 d;
k(d); // Invoke Template function

return 0;
}

What is the best way (that is, the most common way) to resolve the
issue above? I get an ambiguous symbol error when compiling, which
does make sense. I'm looking for a way to resolve this without
resorting to passing in enumerated types representing objects or
something like that.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 19 '07 #2
LR
bo*******@yahoo.com wrote:
[snippage]
>
What is the best way (that is, the most common way) to resolve the
issue above?
I don't know the best way, or even the most common way.
I'm looking for a way to resolve this without
resorting to passing in enumerated types representing objects or
something like that.

Would this work for you?

I added some stuff to see what happens when I run.

#include <iostream>
#include <string>

void m(const std::string &s) { std::cout << s << std::endl; }

namespace N1
{
class C1
{
public:
void f() {m("N1::C1::f");}
void g() {m("N1::C1::g");}
};

class C2
{
public:
void h(C1 info) { info.g(); m("N1::C2::h");}
void i() {m("N1::C2::i");}
};

template<typename T>
void k(T x) {
C1 theProblem;
::k(x, theProblem);
}
}

namespace N2
{
class C1
{
public:
void f() {m("N2::C1::f");}
void g() {m("N2::C1::g");}
};

class C2
{
public:
void h(C1 info) {info.g(); m("N2::C2::h");}
void i() {m("N2::C2::i");}
};

template<typename T>
void k(T x) {
C1 theProblem;
::k(x, theProblem);
}
}

template<typename T1, typename T2>
void k(T1 x, const T2 &theProblem) {
x.h(theProblem);
x.i();
}

int main()
{
N1::C2 b;
k(b);

N2::C2 d;
k(d);

}

LR

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jun 20 '07 #3
bo*******@yahoo.com wrote:
namespace N1
{
class C1
{
public:
void f() {}
void g() {}
};

class C2
{
public:
void h(C1 info) {}
void i() {}
};
}

namespace N2
{
class C1
{
public:
void f() {}
void g() {}
};

class C2
{
public:
void h(C1 info) {}
void i() {}
};
}

template <typename T>
void k(T x)
{
C1 theProblem; // Ambiguous!! How to create the correct object???
x.h(theProblem);
x.i();
}
Well, what I would perhaps do is define what the correct object is! My guess
is that you want to call this function with either an N2::C2 or an N1::C2,
right? In that case, I see two options:
1. You specialise the function.
2. You introduce a typedef, nested into the C1s, which declares the correct
C2:
template<typename T>
void foo(T x) {
typename T::c1_t theSolution;
x.h(theSolution);
}
using namespace N1;
using namespace N2;
Eww, first this...
int main(int argc, char* argv[])
{
N1::C2 b;
....and then this? What's the point of separating things in namespaces if you
afterwards throw everything together again?
What is the best way (that is, the most common way) to resolve the
issue above? I get an ambiguous symbol error when compiling, which
does make sense.
Hmmm, errors about symbols are typically linker errors. It would be helpful
to quote error message. It would also have helped if you had trimmed your
example, e.g. the i() is completely unnecessary (I think).

Uli

--
Sator Laser GmbH
Geschäftsführer: Ronald Boers, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jun 20 '07 #4
In article <11**********************@q75g2000hsh.googlegroups .com>,
<bo*******@yahoo.comwrote:
Hi All

Given the following:

// NamespaceTemplate.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"

namespace N1
{
class C1
{
};

class C2
{
};
}

namespace N2
{
class C1
{
};

class C2
{
};
}

template <typename T>
void k(T x)
{
C1 theProblem; // Ambiguous!! How to create the correct object???
// ...
}

using namespace N1;
using namespace N2;
int main(int argc, char* argv[])
{
N1::C2 b;
k(b); // Invoke Template function

N2::C2 d;
k(d); // Invoke Template function

return 0;
}

What is the best way (that is, the most common way) to resolve the
issue above? I get an ambiguous symbol error when compiling, which
does make sense. I'm looking for a way to resolve this without
resorting to passing in enumerated types representing objects or
something like that.

Thanks.
The obvious is template <class U,class T>
void k(T x)
{
U theproblem;
` // etc.
}

int main()
{
// ...
k<N1::C1>(b);
// ...
k<N2::C1>(b);
}

or you can create a meta function that calculates the type of the
problem from the argument type:

template <class Tstruct problem;
template <struct problem<N1::C2{typedef N1::C1 type;};
template <struct problem<N2::C2{typedef N2::C1 type;};

template <class T>
void k(T x)
{
problem<T>::type theProblem;
// as original without the C1 theProblem declaration.
}

original main code

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jun 20 '07 #5
template <typename T>
void k(T x)
{
C1 theProblem; // Ambiguous!! How to create the correct
object???
x.h(theProblem);
x.i();

}
It's not ambigous, C1 is just undeclared.

C1 does not depend on template parameter =it should be declared
before the definition of template <typename Tvoid k(T x). This
function template definition is illegal even if you never instantiate
it.

There are plenty possible solutions to your problem. Here is one of
them.

namespace N1
{
class C1
{
public:
void f() {}
void g() {}
};
class C2
{
public:
typedef C1 Info;
void h(Info info) {}
void i() {}
};
}
namespace N2
{
class C1
{
public:
void f() {}
void g() {}
};

class C2
{
public:
typedef C1 Info;
void h(Info info) {}
void i() {}
};
}

template <typename T>
void k(T x)
{
typename T::Info theProblem;
x.h(theProblem);
x.i();
}
int main(int argc, char* argv[])
{
N1::C2 b;
k(b);

N2::C2 d;
k(d);

return 0;
}

Roman Perepelitsa.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jun 20 '07 #6
bo*******@yahoo.com wrote:
Hi All

Given the following:

// NamespaceTemplate.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"

namespace N1
{
class C1
{
public:
void f() {}
void g() {}
};

class C2
{
public:
void h(C1 info) {}
void i() {}
};
}

namespace N2
{
class C1
{
public:
void f() {}
void g() {}
};

class C2
{
public:
void h(C1 info) {}
void i() {}
};
}

template <typename T>
void k(T x)
{
C1 theProblem; // Ambiguous!! How to create the correct object???
x.h(theProblem);
x.i();
}

using namespace N1;
using namespace N2;
int main(int argc, char* argv[])
{
N1::C2 b;
k(b); // Invoke Template function

N2::C2 d;
k(d); // Invoke Template function

return 0;
}

What is the best way (that is, the most common way) to resolve the
issue above? I get an ambiguous symbol error when compiling, which
does make sense. I'm looking for a way to resolve this without
resorting to passing in enumerated types representing objects or
something like that.

Thanks.

This works for me and is pretty generic imo:
template <typename T, class C>
void k(T x)
{
C theProblem; // Ambiguous!! How to create the correct object???
x.h(theProblem);
x.i();
}

using namespace N1;
using namespace N2;
int main(int argc, char* argv[])
{
N1::C2 b;
k<N1::C2, N1::C1>(b); // Invoke Template function

N2::C2 d;
k<N2::C2, N2::C1>(d); // Invoke Template function

return 0;
}

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Jun 20 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Georg Teichtmeister | last post by:
Hello! We are developing a math - library for realtime applications and want to use some given mathlibraries as base(ipp, MTL, .. ). Our library is a wrapper for those and you should be able to...
7
by: zbyszek | last post by:
I am working with a large C++ program which, for reasons of backward compatibility, uses C's printf and fprintf rather than iostreams. For a certain type of build I want to provide new functions...
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
3
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
3
by: David Komanek | last post by:
Hi all, I am trying to learn more about how to use g++/Cygwin to produce dll files on WinXP. And I have a problem which at the first look seems to be an obvious dll-export problem, but I don't...
8
by: Raider | last post by:
Is it possible to create a template for POD types only? I want to code something like this: class Object // base class { virtual void WriteToStream(Stream &) = 0; }; class Stream
16
by: xman | last post by:
I defined namespace hpc in main.cpp, so not to clash with other libraries. But I found that, in namespace boo, instantiating a template with a class in namespace hpc, causes compilation errors by...
3
by: mojmir | last post by:
hello, i have problem with following code (see below). compiler (vs2005sp1) complains that the call to fn is ambiguous: error C2668: 'BB::fn' : ambiguous call to overloaded function could be...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.