473,387 Members | 1,638 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.

Template operators

quick:

struct A
{
template< typename T >
operator[](size_t index)
{
return vec_[index];
}
std::vector< int > vec_;
};

int main()
{
A a;
float f = a[1]; // doesn't specify template parameter
or
float f = a.operator()< float >(1); // gcc 3.3.2 Debian gives error
"operator[] not defined"
or what?
}

Any clues?

alex
Jul 22 '05 #1
2 1258


Alex Tibbles wrote:
quick:

struct A
{
template< typename T >
operator[](size_t index)
{
return vec_[index];
}
std::vector< int > vec_; you don't even use the template parameter! I guess you mean

template< typename T >
T operator[](size_t index)
{
return vec_[index];
}
};

int main()
{
A a;
float f = a[1]; // doesn't specify template parameter not necessary since your code doesn't use the template parameter and by
default the result is int so everythings working... or
float f = a.operator()< float >(1); // gcc 3.3.2 Debian gives error

should be
a.operator[]<float>(1);
?

Jul 22 '05 #2
On Fri, 23 Jan 2004 12:34:19 -0000, "Alex Tibbles"
<al**********@yahoo.co.uk> wrote:
quick:

struct A
{
template< typename T >
operator[](size_t index)
Did you mean:
T operator[](size_t index)
?
Or
int operator[](size_t index)
perhaps?
{
return vec_[index];
}
std::vector< int > vec_;
};

int main()
{
A a;
float f = a[1]; // doesn't specify template parameter
or
float f = a.operator()< float >(1); // gcc 3.3.2 Debian gives error
"operator[] not defined"
or what?
}

Any clues?


Ignoring the typos and missing headers,

float f = a.operator[]<float>(1);

should compile (it does on my compilers other than GCC). But why make
the operator a template anyway? It means you have to call it with
explicitly specified template parameters...

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3

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

Similar topics

2
by: arch | last post by:
Hi, I am trying to implement a class hierarchy where there will be a base class defining the interface and some common operations (interface+implentation): Constraints : no STL. This will be...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
5
by: Lionel B | last post by:
Greetings, I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example): // ----- BEGIN CODE ----- struct X { int a,b;
3
by: Ondrej Spanel | last post by:
I defined operator = using template member function for a template class. However compiler failed to recognize it is defined and created its own version (which was member-wise copy, and a result...
7
by: AlanJSmith | last post by:
I am converting some C++ projects from 6 to vs2005 in one of the headers i have the code that follows this passage in which I get an error saying int default cant be assumed so i added...
5
by: 2beagles | last post by:
I have a template for a three dimensional array that I have been working on using Visual C++ 6.0. Under version 6 this code worked fine. However, last night I downloaded Visual C++ 2005 Express and...
9
by: rtalbot | last post by:
I've got a container that looks like this: template <class T> class Foo { public: Foo() : _data(), _status(1) { } Foo(T) : _data(T), _status(0) { } ~Foo() { }
3
by: =?gb2312?B?wfXquw==?= | last post by:
Hi, folks, I'm running into a question as below: template<typename T> class A { private: T _a; public: A(T t): _a(t) { }
9
by: neildferguson | last post by:
I am using templates with a little project I am working on. My compiler (GCC) is finding a particular construct ambiguous. Can anyone suggest something I might change in the declaration of class...
5
by: Harinezumi | last post by:
Hello, I need help with the design of a serialization template base class. The idea is that any class derived from this template base class would only need to implement the input and output...
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: 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: 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
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.