Hi,

I am getting the following error while compiling the code on linux machine.

"aggregate value used where an integer was expected"

I am getting this error on a simple for loop. I do not know the reason for this error.
Any help is appreciated.

Thanks

Dani AI

Generated

reported the compiler error "aggregate value used where an integer was expected" on a simple for loop. That message means the compiler found an aggregate (an array or aggregate struct/class) where a scalar integer expression was required. Typical causes are using an array/aggregate as the loop bound or passing an aggregate to a function overload that expects an integral type. was right to ask for code; the exact line and types make diagnosis trivial. suggested casting, but correctly warned that casting usually hides the underlying mistake rather than fixing it.

Quick checklist and fixes:

  • If iterating a C array, use an explicit length: either a compile-time constant, sizeof trick, or std::size (C++17+).
  • If iterating a container, use .size() or a range-based for loop.
  • If a struct/aggregate was used accidentally as the bound, replace it with the intended integer member or expression.

Example idiomatic patterns:

for (size_t i = 0; i < std::size(arr); ++i) { / ... / }

for (size_t i = 0; i < vec.size(); ++i) { / ... / }

for (auto &elem : vec) { / ... / }

Debugging tips: compile with warnings on (e.g. -Wall -Wextra -std=c++17) and inspect the exact error location. Temporarily replace the suspicious expression with a literal to confirm the expression is the cause. If the problem persists, a minimal reproducible example plus the compiler version will allow a targeted answer. For background on aggregates see aggregate initialization and for modern looping idioms see range-based for.

Recommended Answers

All 4 Replies

Try converting the value using static_cast<int>(yourvalue) ...

Showing some code would be helpful.

I'm betting you're trying to assign to an array or structure, or trying to pass an array or structure to a function that takes an integer or...could be lots of things.

>Try converting the value using static_cast<int>(yourvalue) ...
Casting is the worst possible reaction to an error like this. Putting a dirty plate in a stack of clean plates doesn't make it clean.

Could you please post your code ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.