My (mostly technical) blog

error C2143: syntax error : missing ‘;’ before ‘*’

Posted on: December 30, 2006

Ever since I started programming with C++, I have been pulling my hair out trying to decrypt what the compiler errors mean.

Coming from the Java camp, I just hate how C++ compilers work and how stupid they are! After spending 3 hours trying to know why this piece of code won’t compile, I found out the answer after searching for a LONG time.

The code:

#pragma once
#include “Edge.h”
#include “IDGenerator.h”
class Face {
public:
Edge *edge;
int id;
Face(){
id = IDGenerator::getFaceID();
}

void setEdge(Edge *e) {
edge = e;
}
int getID() {
return id;
}

Edge *getEdge() {
return edge;
}
};

The compile error I was getting was:
error C2143: syntax error : missing ';' before '*'

it was choking at the “Edge *edge” line. To cut a long story short, if you are going to use a pointer to another class, don’t include the file as above, just use forward declaration by adding:

class Edge;

at the top of

class Face {

so that the final version looks like:

#pragma once
#include “IDGenerator.h”

class Edge;

class Face {
public:
Edge *edge;
int id;
Face(){
id = IDGenerator::getFaceID();
}

void setEdge(Edge *e) {
edge = e;
}
int getID() {
return id;
}

Edge *getEdge() {
return edge;
}
};

for a better explaination:
http://www-subatech.in2p3.fr/~photons/subatech/soft/carnac/CPP-INC-1.shtml

Advertisements

29 Responses to "error C2143: syntax error : missing ‘;’ before ‘*’"

I owe you not only my thanks, but my sanity as well….. I’ve been dealing with this same error since yesterday and coul’d really find how to solve it…

Thanks for the advice!!!

Regards
Shadow99x

You are welcome πŸ™‚

very interesting, but I don’t agree with you
Idetrorce

Can you elaborate?

Repeate Shadow99x responce + my health.

Solved and Fnx.

ya C++ compiler is infuriating, i had the same problem as here, but it was solved by including a needed namespace

Holy Crap, Thanks!

I spent hours trying to figure this out.

hey thanks you help me alot!!

Regards,
p

Thanks!! if i ever see you im buying you a coffee 😐

Thank you SO incredibly much! I’m having to do a group project for class ON MY OWN, so it’s really frustrating to spend hours on one error 😦

You are all welcome πŸ™‚
@Emily, I do understand your frustration..hehe

Oh man, I should’ve googled this a day and a half ago. Thanks a million!

I had a struct with a class pointer inside, but the problem was exactly the same.

Unbelievable how unhelpful compiler messages can be sometimes.

HUGE Thanks for this! I’ve been trying to figure this out for hours now.

I was being stubborn and not googling for the error message, convinced that “no one would have written anything applying to my specific situation”. But low and behold, you have! Thank you.

I had a follow issue — where I needed to include the header file inside my .cpp file. Hard to explain, but anyone having further issues, look here:

http://www.eventhelix.com/realtimemantra/headerfileincludepatterns.htm

Thanks a lot for this….I come from java as well and HATE c++ to the core..but gotta do it cos my project requires it 😦

This was very very helpful!!

The compiler is expecting you to provide the definitions..just include the header file where this pointer is defined or use the forward declaration as suggested by

I figured out why it doesn’t work in visual studio c++. Here is the correct code I used to fix my error C2143:

#include “Wave.h” /* This header file has the CWave class in it */

CWave *wave1;

int main()
{
wave1.Load(_T(“C:…\Music.wav”));
wave1.Play();
Sleep(100000);
}

The compilers for C++ language are twenty years in the making and they are very good at compiling C++ into native byte-code., so don’t display your hatred towards the precious developers of these massive pieces of art.

Additionally, your diagnosis for the error isn’t exactly correct. You say this will not compile:

#pragma once
#include “Edge.h”
#include “IDGenerator.h”

class Face { // some Java bling for ya
public:
Edge *edge;
int id;

Face() {
id = IDGenerator::getFaceID();
}

void setEdge(Edge *e) {
edge = e;
}

int getID() {
return id;
}

Edge *getEdge() {
return edge;
}
};

Because of the ‘Edge *edge;’ line you receive build error:

C2143: syntax error : missing ‘;’ before ‘*’

Was this the only error?

Because, ‘technically’, if ‘Edge.h’ contains the ‘Edge’ class, then by the time the compiler reaches the line:

‘Edge* edge;’

the ‘Edge’ class SHOULD (right?) have already been declared, so the C2143 error the only makes sense if its following another error (revealed below).

Your solution was to forward declare the Edge class using:

class Edge;

And you said it worked, but since you used forward declaration, the above code will definitely not compile, because the assignment operator ‘=’, for ‘Edge’ = ‘Edge’ will not exist. And you use such an operation within the ‘void setEdge’ member function.

My solution to your trouble lies within the Edge.h file. Somewhere in this file will be an error preventing the ‘Edge’ class from being declared. Thus, after following few more related errors, you will eventually end up with the C2143.

Additionally, your ‘Face’ class will now not be declared, which may create similar errors elsewhere.

Some advice from an advent self-taught developer to one who probably got interested in college, READ EVERY ERROR MESSAGE BEFORE CONCLUDING THERE IS A PROBLEM.

Happy Hacking

Ah yes.

I do take one thing back, the setEdge function was fine, Edge* = Edge* does exist., but your problem was definitely not the C2143 error.

..my apologies.

Thanks a lot man. This error has been driving me crazy for the last 1 and half days

Many thanks for that! You just saved me another few hours of drilling through my code……Thanks again!!!

add #include β€œEdge.h” to end of stdafx.h

help plz ……
i have the same error and i dont know what to do …..
when I compile my code …

// Day10Doc.h : interface of the CDay10Doc class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_DAY10DOC_H__A12642CF_7E96_4273_B3B5_FA94CD497BEC__INCLUDED_)
#define AFX_DAY10DOC_H__A12642CF_7E96_4273_B3B5_FA94CD497BEC__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CDay10Doc : public CDocument
{
protected: // create from serialization only
CDay10Doc();
DECLARE_DYNCREATE(CDay10Doc)

// Attributes
public:

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDay10Doc)
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL

// Implementation
public:
int GetLineCount();
CLine* AddLine(CPoint ptFrom, CPoint ptTo);
CObArray m_oaLines;
virtual ~CDay10Doc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
//{{AFX_MSG(CDay10Doc)
// NOTE – the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_DAY10DOC_H__A12642CF_7E96_4273_B3B5_FA94CD497BEC__INCLUDED_)

i get the error : missing “;” before “*” for this line
CLine* AddLine(CPoint ptFrom, CPoint ptTo);

and there are other errors which are :
——————–Configuration: Day10 – Win32 Debug——————–
Compiling…
Day10.cpp
d:\vc++6\msdev98\myprojects\day10\day10doc.h(36) : error C2143: syntax error : missing ‘;’ before ‘*’
d:\vc++6\msdev98\myprojects\day10\day10doc.h(36) : error C2501: ‘CLine’ : missing storage-class or type specifiers
d:\vc++6\msdev98\myprojects\day10\day10doc.h(36) : error C2501: ‘AddLine’ : missing storage-class or type specifiers
Day10View.cpp
d:\vc++6\msdev98\myprojects\day10\day10doc.h(36) : error C2143: syntax error : missing ‘;’ before ‘*’
d:\vc++6\msdev98\myprojects\day10\day10doc.h(36) : error C2501: ‘CLine’ : missing storage-class or type specifiers
d:\vc++6\msdev98\myprojects\day10\day10doc.h(36) : error C2501: ‘AddLine’ : missing storage-class or type specifiers
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(71) : error C2065: ‘CLine’ : undeclared identifier
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(71) : error C2065: ‘lptLine’ : undeclared identifier
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(71) : warning C4552: ‘*’ : operator has no effect; expected operator with side-effect
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(76) : error C2039: ‘GetLine’ : is not a member of ‘CDay10Doc’
d:\vc++6\msdev98\myprojects\day10\day10doc.h(13) : see declaration of ‘CDay10Doc’
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(78) : error C2227: left of ‘->Draw’ must point to class/struct/union
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(138) : error C2084: function ‘void __thiscall CDay10View::OnLButtonDown(unsigned int,class CPoint)’ already has a body
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(186) : error C2065: ‘pLine’ : undeclared identifier
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(186) : error C2440: ‘=’ : cannot convert from ‘int *’ to ‘int’
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(188) : error C2227: left of ‘->Draw’ must point to class/struct/union
Generating Code…
Error executing cl.exe.

Day10.exe – 14 error(s), 1 warning(s)
kindly help and reply here or on my email:
alshloulsaddam@yhoo.com
this tok me more than 1 week

guyzzz i did it like this

// Day10Doc.h : interface of the CDay10Doc class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_DAY10DOC_H__A12642CF_7E96_4273_B3B5_FA94CD497BEC__INCLUDED_)
#define AFX_DAY10DOC_H__A12642CF_7E96_4273_B3B5_FA94CD497BEC__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CLine;
class CDay10Doc : public CDocument
{
protected: // create from serialization only
CDay10Doc();
DECLARE_DYNCREATE(CDay10Doc)

// Attributes
public:

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDay10Doc)
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);
//}}AFX_VIRTUAL

// Implementation
public:
int GetLineCount();
CLine* AddLine(CPoint ptFrom, CPoint ptTo);
CObArray m_oaLines;
virtual ~CDay10Doc();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
//{{AFX_MSG(CDay10Doc)
// NOTE – the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_DAY10DOC_H__A12642CF_7E96_4273_B3B5_FA94CD497BEC__INCLUDED_)

and that works good, thanx alot but after compiling i got another errors not in Day10.cpp file but in Day10View.cpp file
for Day10View the code is:

// Day10View.cpp : implementation of the CDay10View class
//

#include “stdafx.h”
#include “Day10.h”

#include “Day10Doc.h”
#include “Day10View.h”

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDay10View

IMPLEMENT_DYNCREATE(CDay10View, CView)

BEGIN_MESSAGE_MAP(CDay10View, CView)
//{{AFX_MSG_MAP(CDay10View)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDay10View construction/destruction

CDay10View::CDay10View()
{
// TODO: add construction code here

}

CDay10View::~CDay10View()
{
}

BOOL CDay10View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs

return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDay10View drawing

void CDay10View::OnDraw(CDC* pDC)
{
CDay10Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
///////////////////////
// MY CODE STARTS HERE
///////////////////////
// Get the number of lines in the document
int liCount = pDoc->GetLineCount();
// Are there any lines in the document?
if (liCount)
{
int liPos;
CLine*lptLine;
// Loop through the lines in the document
for (liPos = 0; liPos GetLine(liPos);
// Draw the line
lptLine->Draw(pDC);
}
}
///////////////////////
// MY CODE ENDS HERE
///////////////////////
}

/////////////////////////////////////////////////////////////////////////////
// CDay10View printing

BOOL CDay10View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}

void CDay10View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}

void CDay10View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing

}

/////////////////////////////////////////////////////////////////////////////
// CDay10View diagnostics

#ifdef _DEBUG
void CDay10View::AssertValid() const
{
CView::AssertValid();
}

void CDay10View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}

CDay10Doc* CDay10View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDay10Doc)));
return (CDay10Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDay10View message handlers

void CDay10View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

CView::OnLButtonDown(nFlags, point);
}

void CDay10View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
///////////////////////
// MY CODE STARTS HERE
///////////////////////
// Capture the mouse, so no other application can
// grab it if the mouse leaves the window area
SetCapture();
// Save the point
m_ptPrevPos = point;
///////////////////////
// MY CODE ENDS HERE
///////////////////////
CView::OnLButtonDown(nFlags, point);
}

void CDay10View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
///////////////////////
// MY CODE STARTS HERE
///////////////////////
// Have we captured the mouse?
if (GetCapture() == this)
// If so, release it so other applications can
// have it
ReleaseCapture();
///////////////////////
// MY CODE ENDS HERE
///////////////////////
CView::OnLButtonUp(nFlags, point);
}

void CDay10View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
///////////////////////
// MY CODE STARTS HERE
///////////////////////
// Check to see if the left mouse button is down
if ((nFlags & MK_LBUTTON) == MK_LBUTTON)
{
// Have we captured the mouse?
if (GetCapture() == this)
{
// Get the Device Context
CClientDC dc(this);
// Add the line to the document
CLine *pLine = GetDocument()->AddLine(m_ptPrevPos, point);
// Draw the current stretch of line
pLine->Draw(&dc);
// Save the current point as the previous point
m_ptPrevPos = point;
}
}
///////////////////////
// MY CODE ENDS HERE
///////////////////////
CView::OnMouseMove(nFlags, point);
}

and the errors i got are:

——————-Configuration: Day10 – Win32 Debug——————–
Compiling…
Day10.cpp
Day10View.cpp
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(76) : error C2039: ‘GetLine’ : is not a member of ‘CDay10Doc’
d:\vc++6\msdev98\myprojects\day10\day10doc.h(13) : see declaration of ‘CDay10Doc’
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(78) : error C2027: use of undefined type ‘CLine’
d:\vc++6\msdev98\myprojects\day10\day10doc.h(12) : see declaration of ‘CLine’
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(78) : error C2227: left of ‘->Draw’ must point to class/struct/union
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(138) : error C2084: function ‘void __thiscall CDay10View::OnLButtonDown(unsigned int,class CPoint)’ already has a body
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(188) : error C2027: use of undefined type ‘CLine’
d:\vc++6\msdev98\myprojects\day10\day10doc.h(12) : see declaration of ‘CLine’
D:\VC++6\MSDev98\MyProjects\Day10\Day10View.cpp(188) : error C2227: left of ‘->Draw’ must point to class/struct/union
Generating Code…
Compiling…
Day10Doc.cpp
Generating Code…
Error executing cl.exe.

Day10.exe – 6 error(s), 0 warning(s)

Another way that this problem can jump up and ‘bite’ you is to have trash in an include file. I spent three days trying to figure out why it was breaking on the line:
static char THIS_FILE[] = __FILE__;
This was in the header of most of the files in a large complex project.
I finally found the trash (some extra characters that were accidentally entered) in a major include file. Removing the trash fixed the problem but it was not obvious what was going on.
Bottom line, be very careful when typing or using keyboard shortcuts. If you get trash in an include file and don’t notice it, it will be hell to pay trying to find the problem.

For latest news you have to pay a visit the web
and on the web I found this web site as a finest web site for newest updates.

Today, I went to the beachfront with my children.

I found a sea shell and gave it to my 4 year old daughter
and said “You can hear the ocean if you put this to your ear.” She
put the shell to her ear and screamed. There was a hermit
crab inside and it pinched her ear. She never wants to go back!
LoL I know this is entirely off topic but I had to tell someone!

Oh my goodness! Incredible article dude! Thank
you so much, However I am encountering issues with your
RSS. I don’t know the reason why I can’t join it. Is there
anybody having identical RSS issues? Anyone that knows the answer will you kindly respond?
Thanks!!

Patrick’s Day Gluten-Free Dishes 5 Non-Pinterest Websites for St.

Another cool feature is the fact that user can set the device to clean at specific times as needed.
So it’s the ideal time for marketers towards gear up and adopt
that the raging trend to benefit the absolute most out of it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Ahmed Sabbour's Facebook profile
December 2006
S M T W T F S
    Apr »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Tweets

RSS StackOverflow

Recently bookmarked

%d bloggers like this: