-2

私は C++ が初めてで、MS Visual C++ でシングル ドキュメント インターフェイス アプリケーションを作成しました。コンパイルすると、Day10 SDIDOC.h というヘッダファイルに以下のようなエラーが発生しました。

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

私のファイルは

Day10 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() および AddLine() メソッドは、Day10 SDIDOC.cpp で次のように実装されます。

Day10 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];

}

何が悪いのか理解できません。その解決策を教えてください。ありがとうございました...

4

1 に答える 1

1

コンパイラは、2 つの関数を解析する時点での宣言を認識できないようです。CLineこのため、その名前が何であるかがわからず、エラーが発生します。

の定義にヘッダーを含めるか、先頭に前方宣言CLineを追加して cpp ファイルにヘッダーを含めることで、これを解決できます。オブジェクトへのポインターのみを使用し、オブジェクトを定義したり、ヘッダーでその定義を使用したりしないため、前方宣言で十分です。Day10 SDIDOC.hCLineCLine

于 2013-01-02T04:53:54.597 に答える