473,387 Members | 1,512 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,387 software developers and data experts.

Reg. Function Templates

VSP
Hi,

In the following code, we have created a template function,
We expected the output to display "A::A()" and "B::B()" but we are getting
both as "B::B()".

class A
{
public:
A()
{
cout<<endl<<"A::A()";
}
};

class B
{
public:
B()
{
cout<<endl<<"B::B()";
}
};

template<class T>
void func()
{
T t;
}

int main(int argc, char* argv[])
{
func<A>();
func<B>();
return 0;
}

Please clarify.
Compiler Used: VC++ 6.0

Regards,
VSP
Aug 4 '07 #1
2 1205
"VSP" <ab*@abc.comwrote in message
news:f9**********@daniel-new.mch.sbs.de...
Hi,

In the following code, we have created a template function,
We expected the output to display "A::A()" and "B::B()" but we are getting
both as "B::B()".

class A
{
public:
A()
{
cout<<endl<<"A::A()";
}
};

class B
{
public:
B()
{
cout<<endl<<"B::B()";
}
};

template<class T>
void func()
{
T t;
}

int main(int argc, char* argv[])
{
func<A>();
func<B>();
return 0;
}

Please clarify.
Compiler Used: VC++ 6.0
In VC++ .net 2003 the output is the expected

A::A()
B::B()

Of course after I included <iostreamand changed cout and endl to std::cout
and std::endl.

Try a fresh build. If that doesn't work, it's a bug in VC++ 6.0. 6.0 is
fairly old, prestandard and has a few bugs. You definately need to upgrade.
Aug 4 '07 #2
Jim Langston wrote:
:: "VSP" <ab*@abc.comwrote in message
:: news:f9**********@daniel-new.mch.sbs.de...
::: Hi,
:::
::: In the following code, we have created a template function,
::: We expected the output to display "A::A()" and "B::B()" but we
::: are getting both as "B::B()".
:::
::: class A
::: {
::: public:
::: A()
::: {
::: cout<<endl<<"A::A()";
::: }
::: };
:::
::: class B
::: {
::: public:
::: B()
::: {
::: cout<<endl<<"B::B()";
::: }
::: };
:::
::: template<class T>
::: void func()
::: {
::: T t;
::: }
:::
::: int main(int argc, char* argv[])
::: {
::: func<A>();
::: func<B>();
::: return 0;
::: }
:::
::: Please clarify.
::: Compiler Used: VC++ 6.0
::
:: In VC++ .net 2003 the output is the expected
::
:: A::A()
:: B::B()
::
:: Of course after I included <iostreamand changed cout and endl to
:: std::cout and std::endl.
::
:: Try a fresh build. If that doesn't work, it's a bug in VC++ 6.0.
:: 6.0 is fairly old, prestandard and has a few bugs. You definately
:: need to upgrade.

Yes, this is a known bug in VC6. The compiler uses function
parameters, but not template parameters, in the function signature.
This fools the linker into believing that there is only one func().
If you change the function to

template<class T>
void func(const T* = 0)
{
T t;
}

it will work as it should.

If you try to learn C++, the free Visual C++ Express (VC8) is a much
better choice.
Bo Persson
Aug 4 '07 #3

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

Similar topics

6
by: Suzanne | last post by:
Hi++, I get a link error when I try to call a template function that is declared in a header file and defined in a non-header file. I do *not* get this link error if I define the template...
3
by: Dennis Pais | last post by:
Here's a piece of code that I found in C++ Primer by Stan Lippman ! ------------------------------------------------- template<typename T, int size> T min(T (&r_array) ) { Type min_value =...
5
by: nifsmith | last post by:
Hi I am trying to learn about Queues and use templates at the same time. I have written the following code and I am getting a link error, stating "unresolved external symbol, "int__cdecl...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
2
by: recover | last post by:
#include <stdio.h> template<class T> class TpHello { public: int GetHash(){return 0;} protected: private: T a;
5
by: desktop | last post by:
I have this example: template<class T(1) void f( T ); template<class T(2) void f( T* ); template< (3)
4
by: nickyspace | last post by:
HI all I have a little issue with this php code. Below is the code CODE: PHP 1.<?
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.