We show that discreteness effects related to classical two-body relaxation produce spurious heating of the gaseous component in numerical simulations of galaxy formation. A simple analytic calculation demonstrates that this artificial heating will dominate radiative cooling in any simulation where the mass of an individual dark matter particle exceeds a certain critical value, This maximum mass depends only on the cooling function of the gas, on the fraction of the material in gaseous form, and (weakly) on typical temperatures in the gas. It is comparable to, or smaller than, the dark matter particle masses employed in most published simulations of cosmological hydrodynamics and galaxy formation. Any simulation that violates this constraint will be unable to follow cooling flows, although catastrophic cooling of gas may still occur in regions with very short cooling times. We use a series of N-body/smoothed particle hydrodynamics simulations to explore this effect. In simulations that neglect radiative cooling, two-body heating causes a gradual expansion of the gas component. When radiative effects are included, we find that gas cooling is almost completely suppressed for dark matter particle masses above our limit. Although our test simulations use smoothed particle hydrodynamics, similar effects, and a similar critical mass, are expected in any simulation where the dark matter is represented by discrete particles.