[Tex/LaTex] how to have a caption on top of longtable

captionslongtable

I want to have a longtable like this:

\begin{center}
\begin{longtable}{|p{2cm}|p{3cm}|p{7cm}|p{3cm}|}
\caption{my caption}
\hline
1 & 2 & 3 & 4\\ 
\hline 
\hline
1 & 2 & 3 & 4\\
\hline
1 & 2 & 3 & 4\\
\hline
.
.
.
1 & 2 & 3 & 4\\
\hline

\label{variability_impl_mech}
\end{longtable}
\end{center}

but when I try to compile this, I get this error: !Misplaced \noalign

when I write the caption after the last \hline, it works fine. I want to have the caption on top of my table but this error appears.

Best Answer

\begin{longtable}{|p{2cm}|p{3cm}|p{7cm}|p{3cm}|}
\caption{my caption}\\    %%%%<===
\hline

and you should put the \label after \caption, which makes more sense.