2013-01-02 113 views
-3

我是C++的新手,我用MS Visual C++创建了一个单文档界面应用程序。 当我编译它,如下错误C2143:语法错误:缺少';'在Visual C++的'*'之前

error C2143: syntax error : missing ';' before '*' 
error C2501: 'CLine' : missing storage-class or type specifiers 
error C2501: 'GetLine' : missing storage-class or type specifiers 
error C2143: syntax error : missing ';' before '*' 
error C2501: 'CLine' : missing storage-class or type specifiers 
error C2501: 'AddLine' : missing storage-class or type specifiers 

我的文件

第十天SDIDOC.h

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

这些函数getline(都发生在所谓的第十天SDIDOC.h头文件中的一些错误)和AddLine()方法在Day10中实现此类功能SDIDOC.cpp

Day 10 SDIDOC.cpp

CLine * CDay10SDIDoc::AddLine(CPoint ptFrom, CPoint ptTo) 
{ 
//create a new CLine Object 
CLine *pLine = new CLine(ptFrom,ptTo); 

try 
{ 
    //Add the new line to the object array 
m_oaLines.Add(pLine); 

    //Mark the document as dirty(unsaved) 
    SetModifiedFlag(); 
} 
//Did we run into a memory exception ? 
catch(CMemoryException* perr) 
{ 
    //Display a message for the user,giving the bad new 
    AfxMessageBox("Out of Memory",MB_ICONSTOP|MB_OK); 

    //Did we create a line object? 
    if(pLine) 
    { 
    //Delete it 
    delete pLine; 
    pLine = NULL; 
    } 
    //delete the exception object 
perr->Delete(); 
} 
    return pLine; 
    } 

与函数getline方法

CLine * CDay10SDIDoc::GetLine(int nIndex) 
{ 
return (CLine*) m_oaLines[nIndex]; 

} 

我不明白有什么不好。 请给我一个解决方案。 谢谢...

+3

你有没有包含任何包含'CLine'的标题? – chris

+0

使用[clang](http://clang.llvm.org/)并获取更好的错误消息! –

回答

1

看来你的编译器看不到声明CLine在点,当它解析2种功能。由于这个原因,它不知道这个名字是什么,从而出错。

您可以通过解决这个包括头部CLine定义或在Day10 SDIDOC.h顶部加入只是一个向前声明和包括cpp文件头。前向声明就足够了,因为您只使用指向CLine的指针,并且不定义CLine对象,或者在头中始终使用其定义。

+1

只要在头文件中声明'Cline'就可以解决头文件的编译错误,因为它不需要查看'CLine'的细节,所以它可以接受和处理'CLine'作为*不完整类型*,但是cpp需要看到'CLine'的定义,它不能用于Incomplete类型。 –

+0

@AlokSave,好点,尽管你可以首先反驳指针的用法。 – chris

+0

@AlokSave是不是我的第二个建议?我已经添加了更多的解释,使其更清晰 –

相关问题