Hi,
Here is the code that .NET does not seem to like, but as far as i can see it
is valid C++ code.
Am i wrong?
....
// vector headers
....
struct MYSTRUCT
{
int m_iSomething;
};
typedef std::vector< MYSTRUCT, std::allocator< MYSTRUCT > > MYSTRUCT_VECTOR ;
....
// Within class
....
MYSTRUCT_VECTOR g_MyVector;
int CMyClass::FindS omeThing( int pos )
{
// return item
}
MYSTRUCT * CMyClass::GetSt ructure( int iSomething)
{
// Find the item in the vector
int pos = FindSomeThing( iSomething );
// Anything found
if( pos <0 ) return NULL;
// return what we have
return (g_MyVector.beg in()+pos);
}
The code above should work i think but .NET 2002 gives me an error? What am
i missing?
Many thanks
Sims
Jul 22 '05
12 1363
Sims wrote: Hi,
The reason for the error is that what you actually return is a vector::iterato r and not (necessarily) a pointer to MYSTRUCT. Although vector iterators are very often implemented in terms of ordinary pointers they still have an implementation specific type, which can be totally different from an ordinary pointer. The portable way to solve this problem is the following for example:
return &(*g_MyVector.b egin());
However, I'm amazed that .NET 2002 complains as VC++ 6.0 has no problem with your approach (although I wouldn't recommend it!).
Generally your approach is not portable and is also no good programming
style. I would recommend to strictly distinct between iterators and
pointers. Or see pointers as iterators, but not the other way round.
--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich
"Sims" <si*********@ho tmail.com> wrote in message
news:bu******** ****@ID-162430.news.uni-berlin.de... Hi,
The reason for the error is that what you actually return is a vector::iterato r and not (necessarily) a pointer to MYSTRUCT. Although vector iterators are very often implemented in terms of ordinary
pointers they still have an implementation specific type, which can be totally different from an ordinary pointer. The portable way to solve this
problem is the following for example:
return &(*g_MyVector.b egin());
However, I'm amazed that .NET 2002 complains as VC++ 6.0 has no problem with your approach (although I wouldn't recommend it!).
Sorry, what do you not recommend? VC++6 or My approach?, if it is my approach what would you do instead?
My posting might be a little misleading regarding what I do not recommend.
If you prefer to leave the code as it is returning a pointer to MYSTRUCT
then you should do it as I showed above:
return &(*g_MyVector.b egin() );
On the other hand you can also change the return type to
MYSTRUCT_VECTOR ::iterator and leave the return statement as it is. The way
you implemented it works under VC++ 6.0 and Comeau although it is not
required to do so and it is not portable. Hence, I'd recommend to change
either the return type or the return statement.
Regards
Chris I'm not sure if you can return a pointer to an object in a vector when you're storing the objects themselves, however. Someone else would have
to answer that. (I've only used vectors where I store the pointers, personally.)
-Howard
You must be very careful if you return pointers to objects stored in a
vector. The vector can and likely
will reassign the objects to other locations when you add data to the
vector. Storing pointers rather than
object is a better solution but doesn't utilize the capabilities of vectors
and STL in general. Obviously you must look at how you are using the array
and decide what is appropriate.
James This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Matthew |
last post by:
I am working on a simple text adventure game. I have implemented one
word commands and a bit for two words. I have a problem though the
movement commands n and s (north and south respectivly) don't work. I
can't figure out why!
#include <string>
#include <iostream>
#include <vector>
using namespace std;
|
by: sop3k |
last post by:
Compiling the code below, by VC++ 6.0 I get an error like this:
visual c++\vc98\include\functional(263) : error C2562: '()' : 'void'
function returning a value
visual c++\vc98\include\functional(262) : see declaration of '()'
visual c++\vc98\include\functional(263) : while compiling
class-template member function 'void __thiscall
std::mem_fun_ref_t<void,class CStr>::operator
()(class CStr &) const'
|
by: Steven T. Hatton |
last post by:
This is something I've been looking at because it is central to a currently
broken part of the KDevelop new application wizard. I'm not complaining
about it being broken, It's a CVS images. Such things happen. The whole
subsystem is going through radical changes. I don't really want to say
what I think of the code just yet. That would influence the opinions of
others, and I really want to know how other people view these things,...
|
by: Richard Thompson |
last post by:
I've got a memory overwrite problem, and it looks as if a vector has
been moved, even though I haven't inserted or deleted any elements in
it. Is this possible? In other words, are there any circumstances in
which the STL will move a vector, or invalidate iterators to elements
in the vector, if you don't insert or remove elements?
My actual problem seems to be as follows:
I have class X, which contains an STL vector. The constructor...
|
by: Alfonso Morra |
last post by:
I have written the following code, to test the concept of storing
objects in a vector. I encounter two run time errors:
1). myClass gets destructed when pushed onto the vector
2). Prog throws a "SEGV" when run (presumably - attempt to delete
deleted memory.
Please take a look and see if you can notice any mistakes I'm making.
Basically, I want to store classes of my objects in a vector. I also
have three further questions:
| |
by: john.burton |
last post by:
I used to use c++ a lot but recently I've been using java and pythons
and I'm finding it a lot harder to think of the best way to do
something in C++
Imagine I want a function that returns a "list" of selected users from
a big list of users, or a database or something based on some
selection.
In python I might use a generator function which yielded the next
relevent user each time it was called. In java I'd probably return an
|
by: andrew browning |
last post by:
i have overlaoded all of my arithmetic operators but all are
functioning as multiplication. below is a sample of the addition
operator:
Rational operator + (const Rational& r1, const Rational& r2){
//Postcondition: sum of r1 and r2 are returned
int numerator;
int denominator;
|
by: martin-g |
last post by:
Hi.
I have a question on arrays in VBA. I'm a C++/C# programmer and didn't
expected arrays in VBA to be so helpless, or maybe it only seems to me
that they are helpless.
For example in C++ we can easily create a dynamic array using
stl::vector, add elements at every position, remove elements at every
position and it will automatically organize memory for us. How can such
things be done in VBA?
|
by: John Doe |
last post by:
Hi,
I wrote a small class to enumerate available networks on a smartphone :
class CNetwork
{
public:
CNetwork() {};
CNetwork(CString& netName, GUID netguid):
_netname(netName), _netguid(netguid) {}
|
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...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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...
|
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...
|
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...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |