473,770 Members | 1,870 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 #1
12 1361
>
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);
}


Sorry, the error .NET gives me is that there is a conversion error in my
return value.

Sims
Jul 22 '05 #2
Sims wrote:

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);
}


Sorry, the error .NET gives me is that there is a conversion error in my
return value.


Your return value is of type vector<...>::it erator not of MYSTRUCT*. Just
because vector iterators are most often implemented as ordinary pointers
doesn't mean iterators and pointers are interchangeable .

--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich
Jul 22 '05 #3

"Hendrik Belitz" <ho************ **@fz-juelich.de> wrote in message
news:bu******** ***@zam602.zam. kfa-juelich.de...
Sims wrote:

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);
}


Sorry, the error .NET gives me is that there is a conversion error in my
return value.


Your return value is of type vector<...>::it erator not of MYSTRUCT*. Just
because vector iterators are most often implemented as ordinary pointers
doesn't mean iterators and pointers are interchangeable .


To follow up on that...

if you want to return pointers to MYSTRUCT, then you can always actually
STORE pointers to MYSTRUCT, instead of the objects themselves. In that
case, you dereference the iterator to get your pointer. (And remember that
you'll need to delete the objects pointed to when you're done with the
vector.)

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



Jul 22 '05 #4

"Sims" <si*********@ho tmail.com> wrote in message
news:bu******** ****@ID-162430.news.uni-berlin.de...

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);
}

This is probably because Iterators for vector class on your implementation are
not implemented as pointers.
The same code (after some changes) however compiles fine on Comeau online.

So, to be safe use MYSTRUCT_VECTOR ::iterator as the return type instead of
MYSTRUCT *.
Also use std::advance instead of '+' on iterators.

Best wishes,
Sharad
Jul 22 '05 #5
Sharad Kala wrote:
So, to be safe use MYSTRUCT_VECTOR ::iterator as the return type instead
of MYSTRUCT *.
Also use std::advance instead of '+' on iterators.


Using operator+() on random access iterators is defined in the C++ Standard,
so I don't think you should use std::advance() here.

--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich
Jul 22 '05 #6
Howard wrote:
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.)


AFAIK you can do the following:

vector<object> v;
....
vector<object>: :iterator it = v.begin();
....
object* ptr = &(*it);

So this works, altough I think working with references or even copies
(Insteas of returning a pointer) may be a better way.

--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich
Jul 22 '05 #7

"Hendrik Belitz" <ho************ **@fz-juelich.de> wrote in message
news:bu******** ***@zam602.zam. kfa-juelich.de...
Sharad Kala wrote:
So, to be safe use MYSTRUCT_VECTOR ::iterator as the return type instead
of MYSTRUCT *.
Also use std::advance instead of '+' on iterators.


Using operator+() on random access iterators is defined in the C++ Standard,
so I don't think you should use std::advance() here.


True :-)
But to make the code generic for other containers one should be using advance.
Since here it's a vector using '+' is justified but otherwise if the container
itself is generic then it's preferable to use advance.

Best wishes,
Sharad
Jul 22 '05 #8

"Sims" <si*********@ho tmail.com> wrote in message
news:bu******** ****@ID-162430.news.uni-berlin.de...

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);
}


Sorry, the error .NET gives me is that there is a conversion error in my
return value.

Sims


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!).

Cheers
Chris
Jul 22 '05 #9
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?

Sims
Jul 22 '05 #10

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
2579
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
1691
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
3449
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
9617
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
9453
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
10254
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
10099
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
10036
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
9904
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...
0
6710
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();...
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
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.