问题
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.
回答1:
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; ....
回答2:
My guess is that it's trying to form a "hierarchy" of some sort.
In other words, we have:
typedef unsigned int size_t;
typedef unsigned int uint;
If size_t
is "more specific" than a generic unsigned int
, then it makes sense to prevent it from being converted to uint
, which may be "more specific" than any old unsigned int
.
I would expect this to be a warning if anything, though, not an error...
来源:https://stackoverflow.com/questions/8163262/using-a-typedefd-uint-causes-error-while-unsigned-int-does-not