这段代码有什么问题?(在向量中返回一个项目) [英] What is wrong with this code?(returning an item in the vector)

查看:56
本文介绍了这段代码有什么问题?(在向量中返回一个项目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




这是.NET似乎不喜欢的代码,但据我所知它

是有效的C ++代码。

我错了吗?


....

//矢量标题

。 ...


struct MYSTRUCT

{

int m_iSomething;

};


typedef std :: vector< MYSTRUCT,std :: allocator< MYSTRUCT> > MYSTRUCT_VECTOR;


....

//课堂内

....


MYSTRUCT_VECTOR g_MyVector;


int CMyClass :: FindSomeThing(int pos)

{

//返回项目< br $>
}


MYSTRUCT * CMyClass :: GetStructure(int iSomething)

{

//查找向量中的项目

int pos = FindSomeThing(iSomething);

//找到任何内容

if(pos< 0)返回NULL ;

//返回我们的内容

return(g_MyVector.begin()+ pos);

}

我认为上面的代码应该可行,但.NET 2002给我一个错误?什么是

i缺少?


非常感谢


Sims

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::FindSomeThing( int pos )
{
// return item
}

MYSTRUCT * CMyClass::GetStructure( 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.begin()+pos);
}

The code above should work i think but .NET 2002 gives me an error? What am
i missing?

Many thanks

Sims

推荐答案

>
MYSTRUCT * CMyClass :: GetStructure(int iSomething)
//
//在向量中找到项目 int pos = FindSomeThing(iSomething);
//找不到任何东西
if(pos< 0)返回NULL;
//返回我们返回的内容
返回(g_MyVector.begin ()+ pos);
}
MYSTRUCT * CMyClass::GetStructure( 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.begin()+pos);
}




抱歉,.NET给我的错误是我的
$ b中存在转换错误$ b返回值。


Sims



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

Sims


Sims写道:
Sims wrote:

MYSTRUCT * CMyClass :: GetStructure(int iSomething)
{
//在向量中找到项目
int pos = FindSomeThing(iSomething);
//找不到任何东西
if(pos< 0)返回NULL;
//返回我们的内容
返回(g_MyVector.begin()+ pos);
}

MYSTRUCT * CMyClass::GetStructure( 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.begin()+pos);
}



抱歉,.NET给我的错误是有转换我的
返回值中的错误。



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




您的返回值是vector< ...> :: iterator类型而不是MYSTRUCT *。只是

因为向量迭代器通常被实现为普通指针

并不意味着迭代器和指针是可以互换的。


-

要获得我真正的电子邮件地址,请删除两个onkas

-

Dipl.-Inform。 Hendrik Belitz

中央电子学院

研究中心Juelich



Your return value is of type vector<...>::iterator 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




" Hendrik Belitz <豪************** @ fz-juelich.de>在消息中写道

news:bu *********** @ zam602.zam.kfa-juelich.de ...

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

MYSTRUCT * CMyClass :: GetStructure(int iSomething)
{
//在向量中找到项目
int pos = FindSomeThing(iSomething);
//找不到任何东西
if(pos< 0)返回NULL;
//返回我们有的东西
返回( g_MyVector.begin()+ pos);
}

MYSTRUCT * CMyClass::GetStructure( 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.begin()+pos);
}



抱歉,.NET给我的错误是我的
返回值有转换错误。



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



你的返回值是vector< ...> :: iterator类型而不是MYSTRUCT *。只是
因为矢量迭代器通常被实现为普通指针
并不意味着迭代器和指针是可以互换的。



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




跟进那...


如果你想返回指向MYSTRUCT的指针,那么你总是可以实际上将指针指向MYSTRUCT而不是对象本身。在那个

的情况下,你取消引用迭代器来获取你的指针。 (并记住

你需要删除完成

向量时指向的对象。)


我不确定你是否可以返回一个指向向量中对象的指针

你自己存储对象。别人不得不回答那个问题。 (我只使用了存储指针的向量,

个人。)


-Howard



这篇关于这段代码有什么问题?(在向量中返回一个项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆