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

How to define function which operates on iterator convertible toT*

Hello,

maybe my question is stupid, but...

I have function/algorithm which operates on sequence holding objects of
type T. The problem is that I do not know (at the declaration time)
which sequence (vector<T> or list<T>) will be used.

class element_t {
void some_method(void);
}

template <class InputIterator>
void visitor(InputIterator first, InputIterator last)
{
// some code
first->some_method(); // Error here
}

How can I specify that InputIterator is convertible to T*?

Thanks in advance, Ales
Jul 22 '05 #1
3 1292
"AlesD" <al****@seznam.cz> wrote in message
news:cf**********@mrazik2.dkm.cz...
Hello,

maybe my question is stupid, but...

I have function/algorithm which operates on sequence holding objects of
type T. The problem is that I do not know (at the declaration time)
which sequence (vector<T> or list<T>) will be used.

class element_t {
void some_method(void);
}

template <class InputIterator>
void visitor(InputIterator first, InputIterator last)
{
// some code
first->some_method(); // Error here
}

How can I specify that InputIterator is convertible to T*?


What's the error? The following works just fine:

#include <list>

struct element_t {
void some_method(){}
};

template <class It>
void visitor(It beg, It end) {
beg->some_method();
}

int main() {
std::list<element_t> elems(1);
visitor( elems.begin(), elems.end() );
}

If the container holds pointers instead of instances of element_t, then you
should dereference the iterator before using operator->
((*beg)->some_method() instead of beg->some_method()).

--
David Hilsee
Jul 22 '05 #2
Hi,

I am using ostrstream object.I am using the str() function to get the char
buffer. The function is returning the pointer of char buffer , but it is
adding some garbage at the end. This behaviour is not predictable .
Sometimes it is returning the char buffer correctly , sometimes it is
returning the char buffer and some garbage attached to it at the end.I am
initialising the ostrstream object by fill('\0') function.

Can U please suggest some solution.

I am writing the code below for reference.

tBool clClusterCheckResults::logUplinkZeroCellError(cons t clCluster&
corfCluster,
RBINTValOrderedVector <int> a_vZCsNotRetained)
{
bool a_bReturnValue(false);
ostrstream myBuffer;
myBuffer.fill('\0');
a_bReturnValue = true;

myBuffer << corfCluster // << opearor is overloaded for clCluster class
<< " WARNING: The Cluster has no Uplink ZeroCells. "
<< " The following Zero Cells with Cobounding OC, with Uplinks are not
retained "
<< " in the higher level. (ZC Index) - ";

for(int iZeroCellIterator = 0 ;
iZeroCellIterator < a_vZCsNotRetained.length() ;
iZeroCellIterator++)
{
myBuffer << a_vZCsNotRetained[iZeroCellIterator] << " ";
}

myBuffer << "END\n";
cout<< "``````````PRINTING mybuffer logUplinkZeroCellError`````````\n";
cout<< myBuffer.str(); //Here the output is coming errorous
cout<< "``````````OVER``````````````````````````````````` ``````````\n";

m_oReport.logErrorMessage(myBuffer);

m_oResultsCounter.incrementErrorCount(
corfCluster.getLevel(),
UplinkZeroCellCheck,
"David Hilsee" <da*************@yahoo.com> wrote in message
news:pv********************@comcast.com...
"AlesD" <al****@seznam.cz> wrote in message
news:cf**********@mrazik2.dkm.cz...
Hello,

maybe my question is stupid, but...

I have function/algorithm which operates on sequence holding objects of
type T. The problem is that I do not know (at the declaration time)
which sequence (vector<T> or list<T>) will be used.

class element_t {
void some_method(void);
}

template <class InputIterator>
void visitor(InputIterator first, InputIterator last)
{
// some code
first->some_method(); // Error here
}

How can I specify that InputIterator is convertible to T*?
What's the error? The following works just fine:

#include <list>

struct element_t {
void some_method(){}
};

template <class It>
void visitor(It beg, It end) {
beg->some_method();
}

int main() {
std::list<element_t> elems(1);
visitor( elems.begin(), elems.end() );
}

If the container holds pointers instead of instances of element_t, then

you should dereference the iterator before using operator->
((*beg)->some_method() instead of beg->some_method()).

--
David Hilsee

Jul 22 '05 #3
News For Mohan wrote:
Hi,
Let me ask you right away, why did you reply to a completely unrelated
post instead of posting a new thread?

I am using ostrstream object.I am using the str() function to get the char
buffer. The function is returning the pointer of char buffer , but it is
adding some garbage at the end. This behaviour is not predictable .
Sometimes it is returning the char buffer correctly , sometimes it is
returning the char buffer and some garbage attached to it at the end.I am
initialising the ostrstream object by fill('\0') function.
There is no need to "initialise" an ostringstream object in any way.
The constructor for the ostringstream class takes care of that.

Can U please suggest some solution.
Drop the call to 'fill' for starters and RTFM about ostringstream class
to learn what 'fill' does to it.

I am writing the code below for reference.

tBool clClusterCheckResults::logUplinkZeroCellError(cons t clCluster&
corfCluster,
RBINTValOrderedVector <int> a_vZCsNotRetained)
{
bool a_bReturnValue(false);
ostrstream myBuffer;
myBuffer.fill('\0');
a_bReturnValue = true;

myBuffer << corfCluster // << opearor is overloaded for clCluster class
<< " WARNING: The Cluster has no Uplink ZeroCells. "
<< " The following Zero Cells with Cobounding OC, with Uplinks are not
retained "
<< " in the higher level. (ZC Index) - ";

for(int iZeroCellIterator = 0 ;
iZeroCellIterator < a_vZCsNotRetained.length() ;
iZeroCellIterator++)
{
myBuffer << a_vZCsNotRetained[iZeroCellIterator] << " ";
}

myBuffer << "END\n";
cout<< "``````````PRINTING mybuffer logUplinkZeroCellError`````````\n";
cout<< myBuffer.str(); //Here the output is coming errorous
cout<< "``````````OVER``````````````````````````````````` ``````````\n";

m_oReport.logErrorMessage(myBuffer);

m_oResultsCounter.incrementErrorCount(
corfCluster.getLevel(),
UplinkZeroCellCheck,

[...]

Jul 22 '05 #4

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

Similar topics

12
by: Roel | last post by:
Hello all, The following will not compile under gcc 3.2.2: #include <string> #include <vector> #include <iostream> #include <ctype.h> class A
8
by: Alexander Stippler | last post by:
Hello, the standard requires a type reverse_iterator. But in the form it is required and implemented for all compilers I know, there is IMO something missing, which is essential for its usage....
11
by: franklini | last post by:
hello people, just wanted to say thanks again for the help in the past. i have a new problem which am wondering if any body can help me with. i have written this abtract class shape and its...
1
by: PengYu.UT | last post by:
Hi, I have to define a composite class. I'm wondering if there are any easy way to define its corresponding iterator class. Can I inherent the iterator from some library? Best wishes, Peng
13
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} };...
12
by: Raider | last post by:
Why it's impossible for compiler to implicitly cast vector<>::iterator to void*? I have: void f(void *, size_t); .... vector<int> v; v.reserve(n); f(v.begin(), n * sizeof(int));
2
by: Mark P | last post by:
For STL Containers, is it well-defined what happens when comparing (for equality) an iterator and a const_iterator which "point" to the same container element?
10
by: Zhou Yan | last post by:
I want to define a pointer to a multiple-subscripted array. For instance, I have an array defined as below( I have reduced the size of the array as well as the dimension, but I think it OK to ask...
3
by: jarek | last post by:
Hi! How can I declare iterator to const object ? I've the following: class ClassA : public list<ClassB> { ..... }
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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
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...

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.