Template resolution problem

M

Mohanasundaram

Hi All,

I am facing a problem with a piece of code which compiles and
works on windows. but it does not compile on g++. I am trying to figure
out is it a problem with the code which does not adhere to C++ standard
and windows compiler is not strict on that. I am using g++ version
3.2.3. Is the code correct? If not please advise.


#include<iostream.h>

class Employee
{
public:
static void showError()
{
cout<<"this shows error"<<endl;
}
};

template <class ItemType>
class MyContainer
{
public:


ItemType add(ItemType a, ItemType b);


};


class ASLDAPServices
{
public:
template <class MemberType>
MemberType add(MemberType a, MemberType b);

};
template <class MemberType>
MemberType ASLDAPServices::add(MemberType a, MemberType b)
{
return a+b;
}

class ASLDAPGroup
{
public:
template <class MemberType>
void addIt()
{
ASLDAPServices ad;
MemberType i = ad.add<MemberType>(12,34);
cout<<i<<endl;

}
};

int main()
{

ASLDAPGroup grp;
grp.addIt<int>();
return 0;
}

$ g++ -o test1 test1.cpp
In file included from /usr/include/c++/3.2.3/backward/iostream.h:31,
from test1.cpp:1:
/usr/include/c++/3.2.3/backward/backward_warning.h:32:2: warning:
#warning This file includes at least one deprecated or antiquated
header. Please consider using one of the 32 headers found in section
17.4.1.2 of the C++ standard. Examples include substituting the <X>
header for the <X.h> header for C++ includes, or <sstream> instead of
the deprecated header <strstream.h>. To disable this warning
use -Wno-deprecated.
test1.cpp: In member function `void ASLDAPGroup::addIt()':
test1.cpp:44: syntax error before `;' token


Regards,
Mohan.
 
V

Victor Bazarov

Mohanasundaram said:
I am facing a problem with a piece of code which compiles and
works on windows. but it does not compile on g++. I am trying to figure
out is it a problem with the code which does not adhere to C++ standard
and windows compiler is not strict on that. I am using g++ version
3.2.3. Is the code correct? If not please advise.


#include<iostream.h>

#include <iostream>
using std::cout;
using std::endl;
class Employee
{
public:
static void showError()
{
cout<<"this shows error"<<endl;
}
};

template <class ItemType>
class MyContainer
{
public:


ItemType add(ItemType a, ItemType b);


};


class ASLDAPServices
{
public:
template <class MemberType>
MemberType add(MemberType a, MemberType b);

};
template <class MemberType>
MemberType ASLDAPServices::add(MemberType a, MemberType b)

template<class MemberType>
MemberType ASLDAPServices said:
{
return a+b;
}

class ASLDAPGroup
{
public:
template <class MemberType>
void addIt()
{
ASLDAPServices ad;
MemberType i = ad.add<MemberType>(12,34);
cout<<i<<endl;

}
};

int main()
{

ASLDAPGroup grp;
grp.addIt<int>();
return 0;
}

$ g++ -o test1 test1.cpp
In file included from /usr/include/c++/3.2.3/backward/iostream.h:31,
from test1.cpp:1:
/usr/include/c++/3.2.3/backward/backward_warning.h:32:2: warning:
#warning This file includes at least one deprecated or antiquated
header. Please consider using one of the 32 headers found in section
17.4.1.2 of the C++ standard. Examples include substituting the <X>
header for the <X.h> header for C++ includes, or <sstream> instead of
the deprecated header <strstream.h>. To disable this warning
use -Wno-deprecated.
test1.cpp: In member function `void ASLDAPGroup::addIt()':
test1.cpp:44: syntax error before `;' token

Which one is line 44? You have at least 3 lines in that function that
end on a semicolon. Why do we have to guess?

V
 
M

Mohanasundaram

Hi Victor,

Sorry for that
Here is that line
MemberType i = ad.add<MemberType>(12,34);

Regards,
Mohan.
 
M

Mohanasundaram

Hi Victor,
I changed the code as per your suggestion but it is giving the
following error

Changed code:
----------------------
template <class MemberType>
MemberType ASLDAPServices<MemberType>::add(MemberType a, MemberType b)
{
return a+b;
}

error:
-------
$ g++ -o test1 test1.cpp
In file included from /usr/include/c++/3.2.3/backward/iostream.h:31,
from test1.cpp:1:
/usr/include/c++/3.2.3/backward/backward_warning.h:32:2: warning:
#warning This file includes at least one deprecated or antiquated
header. Please consider using one of the 32 headers found in section
17.4.1.2 of the C++ standard. Examples include substituting the <X>
header for the <X.h> header for C++ includes, or <sstream> instead of
the deprecated header <strstream.h>. To disable this warning
use -Wno-deprecated.
test1.cpp:32: non-template type `ASLDAPServices' used as a template
test1.cpp: In member function `void ASLDAPGroup::addIt() [with
MemberType =
int]':
test1.cpp:53: instantiated from here
test1.cpp:44: no matching function for call to
`ASLDAPServices::add(int, int)'
$
 
V

Victor Bazarov

Mohanasundaram said:
Hi Victor,
I changed the code as per your suggestion but it is giving the
following error

Changed code:
----------------------
template <class MemberType>
MemberType ASLDAPServices<MemberType>::add(MemberType a, MemberType b)
{
return a+b;
}

My bad. Should be

template<class MemberType>

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top