使用typedef'd uint会导致错误,而“unsigned int”才不是...? [英] Using a typedef'd uint causes error, while "unsigned int" does not...?

查看:982
本文介绍了使用typedef'd uint会导致错误,而“unsigned int”才不是...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,当我在程序中将变量定义为uint而不是unsigned int时,它会出错。这看起来很奇怪,因为uint是typedef'd:

  typedef unsigned int uint; 

...所以我想我可以交换使用这两个。更确切地说,我将一个返回unsigned int的函数的结果赋值给一个uint变量,然后
使用该向量中的那个uint调整大小调用......在这一点上它会出错。即,我的代码看起来像这样:

  unsigned int getUInt()
{
return 3;
}

int main(void){
vector< vector< float> > vectVect(100000);
for(uint i = 0; i< vectVect.size(); ++ i)
{
vector< float>& myVect = vectVect [i];
uint myUnsignedInt = getUInt();
myVect.resize(myUnsignedInt);
}
cout<< 完成<< ENDL;
}

...它的错误行是myVect.resize行。



显然,我已经有了一个解决方案,但我想知道为什么会发生这种情况,因为我很困惑。任何人有任何想法?

PS - 如果有人认为它可能很重要,我在Fedora 15上使用gcc v4.1.2 ...以及包含文件uint是/usr/include/sys/types.h。

解决方案

我的猜测是系统中还有一个uint。尝试将你的名字重命名为不寻常的东西,甚至更好地将它包装在名称空间中。

  namespace MY {
typedef unsigned int uint ; (MY :: uint i = 0; ....





For some reason, when I define a variable as "uint" instead of "unsigned int" in my program, it errors. This seems strange, because uint is typedef'd as:

typedef unsigned int uint;

...so I would think that I could use the two interchangeably. To be more exact, I am assigning the result of a function which returns "unsigned int" into a uint variable, then using that uint in a vector resize call... at which point it errors. Ie, my code looks something like this:

unsigned int getUInt()
{
    return 3;
}

int main(void) {
    vector<vector<float> > vectVect(100000);
    for(uint i = 0; i < vectVect.size(); ++i)
    {
        vector<float>& myVect = vectVect[i];
        uint myUnsignedInt = getUInt();
        myVect.resize(myUnsignedInt);
    }
    cout << "finished" << endl;
}

...and the line it errors at is the myVect.resize line.

Obviously, I already have a solution, but I'd like to understand WHY this is happening, as I'm pretty baffled. Anyone have any ideas?

PS - In case anyone thinks it may matter, I'm using gcc v4.1.2 on fedora 15... and the include file which defines uint is /usr/include/sys/types.h.

解决方案

My guess is that there is another uint in the system. Try renaming yours to something unusual or even better wrap it in a namespace.

namespace MY {
    typedef unsigned int uint;
}

for (MY::uint i = 0; ....

这篇关于使用typedef'd uint会导致错误,而“unsigned int”才不是...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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