473,785 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is wrong with this code?(returning an item in the vector)

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
Jul 22 '05 #11

"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
Jul 22 '05 #12

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
Jul 22 '05 #13

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

Similar topics

1
2228
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;
1
1664
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'
12
3304
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,...
11
2907
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...
6
2580
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:
10
1692
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
10
1862
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;
8
2327
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?
16
3450
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) {}
0
9646
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
10350
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, 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...
0
10157
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...
0
9957
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
7505
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
5386
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
4055
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
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
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...

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.