Question: "Is there an error in the counting of the generations in Matthew chapter 1?"
Answer: Matthew's genealogy traces the ancestors of Joseph, the legal father of Jesus. The structure of the genealogy descends from father to son, beginning with Abraham. Additionally, Matthew divides the genealogy into three groups of fourteen generations, separated by important historic points (Matthew 1:17). Matthew abridged the genealogy by omitting some names that appear in earlier records. Some speculate that the abridged arrangement was intended to aid in memorization. Genealogical abridgement has lots of biblical precedent.
The wording of Matthew 1:17 has caused some to suggest that David's name is included in both the first and second grouping of generations. Notice, "So all the generations from Abraham to David are fourteen generations, from David until the captivity in Babylon are fourteen generations..." The writer does not express his intent to reveal 42 generations from Abraham to Jesus, but rather three segments of Jewish history, each comprised of 14 generations. It is plausible that David's name being mentioned twice (v 17) indicates his inclusion in both the first and second groupings. If so, then the first begins with Abraham and ends with David, 14 generations; the second begins with David and ends with Josiah, 14 generations; and the third begins with Jeconiah and ends with Jesus, 14 generations.
In the listing of Jesus' forefathers, there is a name missing. Excluded from the list is Jehoiakim (a.k.a. Eliakim), who was Josiah's son and Jeconiah's father (1 Chronicles 3:15-16). The reason for his exclusion may be that he was a puppet king, given his rule by the Pharaoh of Egypt. The first phase of the captivity of Judah by Babylon began at the end of Jehoiakim's reign, prior to his son Jeconiah coming into power. Thus, the 3 groupings of 14 generations would include: 1. Abraham to David; 2. Solomon to Jehoiakim (he is not mentioned, but was among the first to be carried off into Babylon); 3. Jeconiah to Jesus.
There may be other possible explanations for the existence of only 41 names in the genealogy of Matthew 1, even though verse 17 speaks of three groupings of 14. Regardless, these two suffice to demonstrate that there is not a contradiction. Many commentators believe that the divisions of 14 generations is simply a literary structure by Matthew not intended to set forth a strict biological lineage. God did not arrange Israelís history so nicely that there were exactly 14 biological generations between these three crucial moments in salvation history. One suggestion is that in 1 Chronicles 1–2 there are 14 generations listed between Abraham and David and from that Matthew structured the rest of the genealogy according to the number 14.
The purpose of a genealogy is to document the proof of ancestry from the origin of the line to the person under discussion. Every individual need not be included, but only those necessary to establish descending relationship. The author may legitimately abridge a genealogy to establish a point or to make it simpler. Matthew is correct in the factual material for his purpose, which is to document the ancestry of Jesus Christ, the Messiah, from Abraham.