473,771 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template won't compile when used with bool

this template won't compile when used with bool, the erorr is
"invalid initialization of non const reference"

template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>( s) { }
T& operator[](int i) { return this -at(i); }
// <- this line
const T& operator[](int i) const { return this -at(i); }
};

Vec<boolb;

Oct 19 '06 #1
3 1490
Greg wrote:
this template won't compile when used with bool, the erorr is
"invalid initialization of non const reference"

template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>( s) { }
T& operator[](int i) { return this -at(i); }
// <- this line
const T& operator[](int i) const { return this -at(i); }
};

Vec<boolb;
Don't inherit from a std classes that are not meant to be inherited
from. Prefer composition when you can (cf.
http://www.parashift.com/c++-faq-lit...html#faq-24.3).

Anyway, it's likely breaking because the standard library provides a
specialization for std::vector<boo l>.

Cheers! --M

Oct 19 '06 #2
Greg wrote:
this template won't compile when used with bool, the erorr is
"invalid initialization of non const reference"

template < typename T >
class Vec : public std::vector< T {
public:
Vec() { }
Vec( int s ) : std::vector<T>( s) { }
T& operator[](int i) { return this -at(i); }
// <- this line
const T& operator[](int i) const { return this -at(i); }
};

Vec<boolb;
std::vector<boo lis specialized and the operator[] and at()
functions do not return bool&.
Oct 19 '06 #3
I only started using this template for debugging purposes, I don't need
it for the time being.

Oct 19 '06 #4

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

Similar topics

1
3343
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
2
2086
by: CoolPint | last post by:
As a self-exercise, I am trying to write a generic Priority Queue, which would store any type and and accept any user-definable "priority" function. After much tinkering, I came up with something like below: class PMinimum { public: template <typename T> bool operator()(const T & a, const T & b)
4
2343
by: Thomi Richards | last post by:
Hi, I'm trying to create a simple stack class using C++ and templates. Everything works well and good if I amke the class totally inline. However, as soon as I try to seperate the class into a .h and a .cpp file, gcc throws some strange linking errors. Compiling like this:
4
2581
by: Mat DeLong | last post by:
I have never been stuck on programming something before to the point I give up... this is a first. I am programming what should be something very easy in C++... using Templates. Here is the code, and the error I get: -------------- namespace LIST { template <typename T> struct Link; template <typename T> class List;
3
3939
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. template<class Type> class base { public: base& operator/=(const base&); Type *image;
15
1468
by: silverburgh.meryl | last post by:
Hi, I am trying to write a transform_until template. It is bascially doing what transform is doing except if UnaryOperation return NULL, it will break out from the loop. Here is the code, but it has compile error, can you please tell me what did I do wrong? template <class _InputIter, class _OutputIter, class _UnaryOperation> _OutputIter
2
1768
by: vectorizor | last post by:
Hello all, I am attempting to vectorize few template functions with the Intel compiler, but without much success so far. Ok granted, this question is not 100% c++, but it is related enough that I felt I could post it here. Also, I did ask in the Intel forums, without much success. And maybe there are some c++ coders in here that are familiar with the Intel compiler. The code below highlights the problem I have. Essentially, I have a
8
1752
by: Tim Frink | last post by:
Hi, I want to use a callback function together with templates. Let's say I've this code: File a.h: class A { private:
4
1868
by: suman.nandan | last post by:
Hi C++ Experts ! I have a little weird requirement. I have a base class, say B and lots of classes D1 .. Dn publicly derived from it. Over the course of development the number of derived classes may increase. I want that before the execution of one of my particular code, I should have a list of pointers to all the derived classes, say std::list<B*ptrList; // contains 'n' elements which are pointers to D1 .. Dn.
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.