Q:

Caption and fonts size issues with tabularx

This table works OK. However,
\begin{tabularx}{\textwidth}{lllX}
    \toprule
    A & B & C & D\\
    \midrule

    A & B & C & aaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaa \\
    \hline

\end{tabularx}

when I change the font size, as in the next table,
\begin{tabularx}{\textwidth}{lllX}
    \toprule
    A & B & C & D\\
    \midrule
    \footnosize
    A & B & C & aaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaa \\
    \hline
\end{tabularx}

I get the error message: 
Undefined control sequence. \end{tabularx}.

Also, I cannot add a caption; if I do,
\begin{tabularx}{\textwidth}{lllX}
    \toprule
    A & B & C & D\\
    \midrule
    \caption{ABC}
    A & B & C & aaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaa \\
    \hline
\end{tabularx}

I get a list of error messages:
! You can't use `\hrule' here except with leaders.
\caption@hrule ->\hrule 
                        \@height \z@ 
l.167 \end{tabularx}

To put a horizontal rule in an hbox or an alignment,
you should use \leaders or \hrulefill (see The TeXbook).

! Missing number, treated as zero.
<to be read again> 
                   \vskip 
l.167 \end{tabularx}

A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

! Illegal unit of measure (pt inserted).
<to be read again> 
                   \vskip 
l.167 \end{tabularx}
...

I have worked around the title issue by adding the line
\captionof{table}{\textbf{Inflation categories}} before \begin{tabularx}, but the downside is that the title and table can end up in different pages.
How can I solve this issues?
EDIT 1
After the messages below, this is a working solution:
\begin{table}
    \caption{ABC}
    \footnotesize
    \begin{tabularx}{\textwidth}{lllX}
    \toprule
    A & B & C & D\\
    \midrule

    A & B & C & aaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaa \\
    \hline
\end{tabularx}
\end{table}

I had a typo in \footnotesize and tabularx needs to be encapsulated by table.

A:

When I compile the following document,
\documentclass{article}
\usepackage{tabularx,booktabs}
\begin{document}
\begin{tabularx}{\textwidth}{lllX}
    \toprule
    A & B & C & D\\
    \midrule
    \footnosize
    A & B & C & aaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaaaaaa aaaaa aaaaa \\
    \hline
\end{tabularx}
\end{document}

I get the following error message:
! Undefined control sequence.
<recently read> \footnosize 

l.11 \end{tabularx}

Thus, it is \footnosize, not \end{tabularx}, that's the subject of the complaint. What to do? Just in case it's not painfully obvious: change \footnosize to \footnotesize.
I assume you're aware that the \footnotesize directive doesn't affect the entire row, but only the first cell of the row in question.
To apply a \caption to this table, it's necessary to embed the tabularx environment in a table environment, something like this:
\begin{table}
\caption{Stuff} \label{tab:stuff}
\begin{tabularx}{\textwidth}{lllX}
...
\end{tabularx}
\end{table}

