G
Gonçalo Rodrigues
Hi all,
Sorry to bother you guys again with a template question but I'm really
trying to understand them and there's some piece of info that I'm
missing. I'm sure I'm being really dumb here so, please bear with me.
I've reduced my problem to the following:
Remark: the example below is just to highlight my problems with
templates. I know very well that there are already ways of doing what
the example tries to do (for example, with the boost library).
So, for the example. I declare the template in a header file like:
#ifndef TEMPLATESHEADER
#define TEMPLATESHEADER
namespace python {
template<typename T>
class IComparable {
public:
//Assumes operator==.
bool operator!=(const T& other) const;
};
//Methods.
template<typename T>
bool IComparable<T>:perator!=(const T& other) const {
return !(*this == other);
}
}
#endif
Now, I try to use the template in the following:
#include <iostream>
#include "object.hpp"
using namespace python;
//Constructor(s).
Object::Object() {}
//Compare operators.
bool Object:perator==(const Object& other) const {
return this == &other;
}
//Simple testing: later, move this into a testobject.cpp.
int main() {
//Force instantiation.
Object obj;
std::cout << "Size of Object is " << sizeof(obj) << "." <<
std::endl;
Object objA;
std::cout << "Compare equal: " << (obj == objA) << "." <<
std::endl;
std::cout << "Compare unequal: " << (obj != objA) << "." <<
std::endl;
}
For completion's sake let me paste the object.hpp header file
#ifndef OBJECTHEADER
#define OBJECTHEADER
#include "templates.hpp"
namespace python {
class Object : public IComparable<Object> {
public:
//Default constructor.
Object();
//Compare operators.
bool operator==(const Object& other) const;
};
}
#endif
When I try to compile this with the free MS .NET c++ compiler I get:
d:\Goncalo\Programming\C++\Python--\include\templates.hpp(17) : error
C2679: bin
ary '==' : no operator found which takes a right-hand operand of type
'const pyt
hon::IComparable<T>' (or there is no acceptable conversion)
with
[
T=python::Object
]
C:\Programas\Microsoft Visual C++ Toolkit
2003\include\xstring(502) : wh
ile compiling class-template member function 'bool
python::IComparable<T>:pera
tor !=(const T) const'
with
[
T=python::Object
]
D:\Goncalo\Programming\C++\Python--\include\object.hpp(12) :
see referen
ce to class template instantiation 'python::IComparable<T>' being
compiled
with
[
T=python::Object
]
Huh? Can anybody enlighten me in what I am doing wrong?
With my best regards,
G. Rodrigues
Sorry to bother you guys again with a template question but I'm really
trying to understand them and there's some piece of info that I'm
missing. I'm sure I'm being really dumb here so, please bear with me.
I've reduced my problem to the following:
Remark: the example below is just to highlight my problems with
templates. I know very well that there are already ways of doing what
the example tries to do (for example, with the boost library).
So, for the example. I declare the template in a header file like:
#ifndef TEMPLATESHEADER
#define TEMPLATESHEADER
namespace python {
template<typename T>
class IComparable {
public:
//Assumes operator==.
bool operator!=(const T& other) const;
};
//Methods.
template<typename T>
bool IComparable<T>:perator!=(const T& other) const {
return !(*this == other);
}
}
#endif
Now, I try to use the template in the following:
#include <iostream>
#include "object.hpp"
using namespace python;
//Constructor(s).
Object::Object() {}
//Compare operators.
bool Object:perator==(const Object& other) const {
return this == &other;
}
//Simple testing: later, move this into a testobject.cpp.
int main() {
//Force instantiation.
Object obj;
std::cout << "Size of Object is " << sizeof(obj) << "." <<
std::endl;
Object objA;
std::cout << "Compare equal: " << (obj == objA) << "." <<
std::endl;
std::cout << "Compare unequal: " << (obj != objA) << "." <<
std::endl;
}
For completion's sake let me paste the object.hpp header file
#ifndef OBJECTHEADER
#define OBJECTHEADER
#include "templates.hpp"
namespace python {
class Object : public IComparable<Object> {
public:
//Default constructor.
Object();
//Compare operators.
bool operator==(const Object& other) const;
};
}
#endif
When I try to compile this with the free MS .NET c++ compiler I get:
d:\Goncalo\Programming\C++\Python--\include\templates.hpp(17) : error
C2679: bin
ary '==' : no operator found which takes a right-hand operand of type
'const pyt
hon::IComparable<T>' (or there is no acceptable conversion)
with
[
T=python::Object
]
C:\Programas\Microsoft Visual C++ Toolkit
2003\include\xstring(502) : wh
ile compiling class-template member function 'bool
python::IComparable<T>:pera
tor !=(const T) const'
with
[
T=python::Object
]
D:\Goncalo\Programming\C++\Python--\include\object.hpp(12) :
see referen
ce to class template instantiation 'python::IComparable<T>' being
compiled
with
[
T=python::Object
]
Huh? Can anybody enlighten me in what I am doing wrong?
With my best regards,
G. Rodrigues