473,794 Members | 3,056 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

returning constant reference of element from a template

Hi all:

I have defined the template

template<class T>
class CGroup
private:
int nSize;
public:
std::deque<T> c;
};

I would like to add a function that returns an element in the deque
with a given index (for other reasons, I chose not to use vector).
Below is my first attempt:

template<class T>
inline T& CGroup<T>::getE lement( int nIndex ) const
{
if( (nIndex < 0) || (nIndex > c.size() ) ) {
std::cerr << "Index = " << nIndex << " out of range in
getElement()!" <<
std::endl;
exit(1);
}
return *(c.begin()+nIn dex);
}

The compiler gave the error: return' : cannot convert from 'const
struct TFrame' to 'struct TFrame &'. How can I specify that the
reference to T that is being returned is a constant reference?

Thanks,

Cuneyt

Jul 23 '05 #1
2 1605
cuneyt wrote:
I have defined the template

template<class T>
class CGroup
private:
int nSize;
public:
std::deque<T> c;
};

I would like to add a function that returns an element in the deque
with a given index (for other reasons, I chose not to use vector).
Below is my first attempt:

template<class T>
inline T& CGroup<T>::getE lement( int nIndex ) const
template<class T>
inline T const& CGroup<T>::getE lement( int nIndex ) const

That should do it...
{
if( (nIndex < 0) || (nIndex > c.size() ) ) {
std::cerr << "Index = " << nIndex << " out of range in
getElement()!" <<
std::endl;
exit(1);
}
return *(c.begin()+nIn dex);
}

The compiler gave the error: return' : cannot convert from 'const
struct TFrame' to 'struct TFrame &'. How can I specify that the
reference to T that is being returned is a constant reference?


See above

V
Jul 23 '05 #2
Yes, it worked.

Thanks a lot!

Cuneyt

Jul 23 '05 #3

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

Similar topics

2
3950
by: nielson | last post by:
I have a class which contains (1) a class variable and (2) a method (e.g., methodA) which returns a reference to the class variable. If the variable has been assigned a value by a constant initializer in the "var" statement, my httpd process crashes with a segmentation fault when I call methodA. If the variable is assigned a value in one of the class methods, the httpd process does not crash when methodA is called and the value returned...
7
7304
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic array e.g: array *a; a = function(...)
10
10298
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. Returning a pointer to the first element might do but I want to do what I've said. Fraser.
3
2050
by: Matthias Lohrer | last post by:
Hi, my XML-Document hat this structure <book> <section> <para /> <para /> </section> <section> <para />
3
9927
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ========================= <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0"
3
3149
by: Blaise Garant | last post by:
Hi I've made a stylesheet to transform my data into XSL-FO. This stylesheet used to work with MSXSL 4.0 but I've got some issues in ..NET. First, I changed removed all the "node-set()" function since they're not used anymore. But now, I used an <xsl:apply-templates/> and for some reason, it works in some situation but not in others. I didn't put the whole XSL file since it's quite big. Here's the part that bugs
7
3171
by: icosahedron | last post by:
Is there a way to determine if a parameter to a function is a constant (e.g. 2.0f) versus a variable? Is there some way to determine if this is the case? (Say some metaprogramming tip or type trait?) I have a function with an if statement, that I would like to optimize away somehow. I was hoping the compiler would do it for me, but it doesn't seem to. Jay
2
1471
by: Joe Van Dyk | last post by:
Hi, I have a class that looks like: class ResultSet Element* getNext(); // more Now, I'd like to allow syntax similar to
3
2802
by: Belebele | last post by:
I have an Element class which is abstract and I would like to have an object of the Iterator class to iterate over a range of elements. I would like to use std::for_each to instrument the iteration, which forces me to have certain interface on the Iterator class. struct Element { virtual ~Element() = 0; };
0
9672
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
9519
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
10213
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
10163
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
10000
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
7538
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
6779
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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.