Ships can fire when cloaked. They just choose not to.
In early cloaked ships it was impossible to fire at all when cloaked, as the build up and discharge of energy by phasers, disrupters, etc would explode the cloaking generator because they couldn't handle the rapid change to the ships energy level.
Later models of cloaked ships could fire, as they improved the cloaks buffers and the ships power regulation.
The reason ships don't fire when cloaked is that they can't fire with any effect when cloaked. Photon and quantum torpedo's are not guided, they are aimed and travel at great speed.
When a torpedo passes thru the cloak it is deflected a small amount, a random amount. At the distances its safe to fire torpedoes at they wouldn't hit anything. Cloaked ships also can't get closed enough to a target to ensure a hit without being destroyed by blast. Phasers and disrupters are also deflected, making a hit unlikely.
It would be possible to fire guided missiles thru the cloak, but missiles travel at a fraction of the speed of a ship at impulse power, giving the target time to get away.
Cloaking is a delicate balance of the ships power output and mass versus cloak strength and balance. Firing anything thru the cloak makes the ship visible for a while until the cloak can compensate.
The Enterprise in the Search for Spock was able to detect the cloaked ship visually because cloaked ships need to travel slowly when closing to effective weapons range, or the drive power cannot be compensated for by the cloak. The Klingon captain needed to travel too fast to get to the Enterprise, exposing him.
In the Undiscovered Country the Klingons used a prototype to fire when cloaked.
However, when you see the torpedo leaving the ship you see the ship is momentariy visible. That's because the prototype was specifically designed only for firing when cloaked. Things like life support could be turned off or powered way down and were only powered by batteries. By limiting most systems on the ship, and designing them for the lowest possible power output, it was possible to re-establish the cloak very quickly after a torpedo was fired The ship was worthless in a space battle, it could only lay in wait for the enemy to come into range. They could, in fact, only travel at a very low impulse, and warp factor even when the cloak was disengaged.
Fortunately, the Federation took measures to counter act this kind of ship in the future. And educated its officers and men not to say a cloaked ship can't fire, but can't effectively fire.