This is asked and answered several times each week. The error message is correct. Read the manual entry for setcookie(), which is clear on this point:
"setcookie() defines a cookie to be sent along with the rest of the header information. Cookies must be sent before any other headers are sent (this is a restriction of cookies, not PHP)."
Don't send ANYTHING to the browser before the cookie.
You can have as many lines of PHP code as you wish, but none may send data.