Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a typedef'd uint causes error, while "unsigned int" does not...?

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.

like image 336
Paul Molodowitch Avatar asked Nov 17 '11 07:11

Paul Molodowitch


People also ask

Is Uint the same as unsigned int?

Unsigned Integers (often called "uints") are just like integers (whole numbers) but have the property that they don't have a + or - sign associated with them. Thus they are always non-negative (zero or positive). We use uint's when we know the value we are counting will always be non-negative.

Is unsigned int same as unsigned?

There is no difference. unsigned and unsigned int are both synonyms for the same type (the unsigned version of the int type).

Is Size_t guaranteed to be unsigned?

Yes, size_t is guaranteed to be an unsigned type.

What does Uint mean in C++?

Unsigned int data type in C++ is used to store 32-bit integers. The keyword unsigned is a data type specifier, which only represents non-negative integers i.e. positive numbers and zero.


1 Answers

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; ....
like image 101
Ant Avatar answered Sep 21 '22 09:09

Ant