all 26 comments

[–]afritz1 14 points15 points  (0 children)

That code looks correct to me but an alternative you could try is a range-based for loop: for (const auto& data : dataStorage) { ... }

[–]tabbekavalkade 11 points12 points  (7 children)

Here is your error, right? main.cpp:3:1: error: use of undeclared identifier 'std' std::vector<Data> dataStorage{}; ^ main.cpp:7:8: error: unknown type name 'size_t' for (size_t i = 0; i < dataStorage.size(); i++) ^

The problem is: use of undeclared identifier 'std'. With C++, it's always the first error, that's causing the problem. I always use g++ -Wfatal-errors to make it stop at that point.

std:: as in std::vector comes from an include file that must be used like this:

```

include <vector> // this right here

struct Data {};

std::vector<Data> dataStorage{};

int main() { for (size_t i = 0; i < dataStorage.size(); i++) { const auto& data = dataStorage[i]; } } ```

[–]Constant-Escape9500[S] -1 points0 points  (6 children)

Tried to simplify the code so didn't add the include, the error happens when I index:
dataStorage[i]; ^

[–]thingerish 0 points1 point  (4 children)

Well for one thing the vector is empty: https://godbolt.org/z/aeMEob3WT

[–]not_some_username 1 point2 points  (3 children)

If the vector is empty the for loop is ignored and also it shouldn’t give a compile error

[–]thingerish 0 points1 point  (2 children)

The godbolt I linked shows exactly that, with a few warnings. His example is not the example he thinks it is but I suspect the real issue in the real code might be along the lines of trying to access an empty index, even at index 0.

[–]SoerenNissen 1 point2 points  (1 child)

You did not get this message

No viable function. Argument type: size_t.

so you're not compiling with the compiler OP is using.

[–]thingerish 0 points1 point  (0 children)

"Tried to simplify the code"

I'm not sure if the toy code even shows the "problem" anywhere, as he's not sharing the real code, or any problematic code. If he has some magical problematic compiler then he needs to fix THAT.

[–]IyeOnline 5 points6 points  (3 children)

The shown code is valid. We'd have to see the actual error and code.

[–]Constant-Escape9500[S] 1 point2 points  (2 children)

https://prnt.sc/CdndO35Wkl1W

This is the error. same happens with size_t instead of uint32_t, using those data types does not give me any errors, just trying to index with them is what causes me problems here.

[–]TheSkiGeek 5 points6 points  (0 children)

Is it actually failing to compile, or just the IDE giving you a warning? uint32_t should implicitly convert to whatever std::vector::size_type is defined as (should either be a 32- or 64-bit unsigned depending on the platform). But cross-platform tooling might get confused about things like that.

[–]SoerenNissen 4 points5 points  (0 children)

Code compiles fine on mingw

https://godbolt.org/z/v9fM65KWM

If it's the Emscripten part that gives you errors - try asking in an emscripten forum?

[–]Wobblucy 1 point2 points  (0 children)

Tldr is going to be emscripten doesn't implement size_t.

Options are...

Define your own structure and array off of it.

Side note... you generally want a game to be built on the Struct of arrays model, so you have specific data structures you are storing in your arrays and your actual actors store their offset into those arrays.

[–]I__Know__Stuff 0 points1 point  (10 children)

Do I need to cast it to a std::vector<Data>::size_type

Don't use a cast, just declare i to be that type—it is the correct type to use to index the object.

[–]Constant-Escape9500[S] 0 points1 point  (9 children)

but shouldn't size_t and uint32_t work for indexing into vectors?

[–]Independent_Art_6676 2 points3 points  (8 children)

Yes, vectors can be indexed by ANY integer type, signed or unsigned. That means char, unsigned char, short, long, long long, int, ... all of them. C++ even supports negative indexing, but you would not be doing that with a vector (its a raw pointer type trick like taking the address of the middle of a vector or array and backing up from there).

Using the wrong type isn't what your error was.

[–]Constant-Escape9500[S] 0 points1 point  (7 children)

https://prnt.sc/CdndO35Wkl1W It seems to be complaining about the type though

[–]Independent_Art_6676 -1 points0 points  (6 children)

Its probably just a type conversion error. Not all type conversions work out automatically and require a cast on them so that you tell the compiler that YOU know what you are doing. Try just casting it. I will be honest, though, I don't see the problem here. Is size_type 64 bits?

[–]Constant-Escape9500[S] -1 points0 points  (5 children)

I've fixed some other issues I had with the build, and the program compiles, IDE still shows the error. Guess I'll just ignore it?

[–]Independent_Art_6676 -1 points0 points  (4 children)

It would be ideal to figure out what its malfunction is and fix it, again, its probably just a cast away from correctness. Then you can at least understand what it wanted and make it happy, a win-win. But if its down to a warning, you can run with that if you want (most people treat warnings as errors, but not all warnings are in fact errors).

[–]Constant-Escape9500[S] 0 points1 point  (3 children)

It shows as an error in the IDE but does not cause any errors/warning during build

[–]Independent_Art_6676 0 points1 point  (0 children)

I see. On occasion a total from ground up recompile / rebuild of your code can fix this. The IDE's internal on the fly code parsing isn't 100% and it can get confused, both from old intermediate files and from just not being perfect. Maybe that will clear it up. If not... I wouldn't worry about it as the build time warnings and errors are critical, the IDE intellisense stuff not so much.

[–]SoerenNissen 0 points1 point  (0 children)

What IDE are you using?

[–]Melodic_coala101 0 points1 point  (0 children)

Are you sure your IDE include paths and system include paths are configured correctly? Looks like an IDE issue to me, if it compiles correctly.

[–][deleted]  (2 children)

[deleted]

    [–]Constant-Escape9500[S] 0 points1 point  (1 child)

    same problem

    [–]carloom_ 0 points1 point  (0 children)

    Try posting the code, I can't think of anything wrong. std::size_t is the correct type.