2012-09-26 15 views
9

私は、パブリックデータメンバーとしてクラスヘッダーファイルで宣言した変数のエラーC2065を取得しています。クラスのコンストラクタ内で関数内でこれらの変数を使用した場合にのみ、エラーが発生したことを示すフラグが付けられます。"宣言されていない識別子"が実際に宣言されました

私は、通常のC++(ないのVisual C++)を書き、そしてここでコンパイラのエラーログの出力だするには、Visual Studio 2010のExpressを使用しています:最後に

1>------ Build started: Project: Project 2, Configuration: Debug Win32 ------ 
1> BaseClassWithPointer.cpp 
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(27): error C2065: 'q' : undeclared identifier 
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(27): error C2541: 'delete' : cannot delete objects that are not pointers 
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(32): error C2065: 'num' : undeclared identifier 
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(33): error C2065: 'q' : undeclared identifier 
1>d:\libraries\documents\school\advanced c++\project 2\project 2\baseclasswithpointer.cpp(34): error C2065: 'q' : undeclared identifier 
1> Generating Code... 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

、私のコードブロックとヘッダをhere're:

BaseClassWithPointer.h

#pragma once 
#include <iostream> 

using namespace std; 

class BaseClassWithPointer 
{ 
public: 
    int num; 
    int *q; 
    BaseClassWithPointer(); 
    BaseClassWithPointer(int value); 
    BaseClassWithPointer(const BaseClassWithPointer& otherObject); 
    void destroyPointer(); 
    virtual void print(); 
    virtual ~BaseClassWithPointer();              //Destructor is virtual so that derived classes use their own version of the destructor. ~  (2. Inheritance - base class with pointer variables – destructors.) 
    const BaseClassWithPointer& operator= (const BaseClassWithPointer &rhs);  //Assignment operator is overloaded to avoid shallow copies of pointers. ~ (3. Inheritance  – base class with pointer variables – assignment operator overloading.) 

}; 

BaseClassWithPointer.cpp

#pragma once 
#include "BaseClassWithPointer.h" 
#include <iostream> 

using namespace std; 

BaseClassWithPointer::BaseClassWithPointer() 
{ 
    num = 0; 
    q = &num; 
} 

BaseClassWithPointer::BaseClassWithPointer(int value) 
{ 
    num = value; 
    q = &num; 
} 

BaseClassWithPointer::BaseClassWithPointer(const BaseClassWithPointer& otherObject) 
{ 
    num = otherObject.num; 
    q = &num; 
} 

void destroyPointer() 
{ 
    delete q; 
} 

void print() 
{ 
    cout << "Num: " << num << endl; 
    cout << "Value pointed to by q: " << *q << endl; 
    cout << "Address of q: " << q << endl; 
} 

BaseClassWithPointer::~BaseClassWithPointer() 
{ 
    destroyPointer(); 
} 

const BaseClassWithPointer& BaseClassWithPointer::operator=(const BaseClassWithPointer &rhs) 
{ 
    if (this != &rhs) 
    { 
     num = rhs.num; 
     q = &num; 
    } 

    return *this; 
} 
+2

は、CPPにない 'の#pragma once'を行い見つけることができない理由です。ヘッダーのみ。 – David

+1

実際、 '#pragma once 'はまったくしないでください。 '#ifndef'ヘッダガードを使用してください。 '#pragma once'は広くサポートされていますが、非標準です。 –

答えて

12

destroyPointer()メソッドのClass識別子を忘れています。

void destroyPointer() 

... 

void print() 

void BaseClassWithPointer::destroyPointer() 
{ 
.... 
} 

void BaseClassWithPointer::print() 
{ 
.... 
} 

など

+0

同じことが印刷方法に当てはまります。 – Mark

+0

うわー。私は愚かな感じ、笑〜 – Mareth

+0

魅力のように動作します。これを回答としてどのようにマークしますか?最初にサイトに投稿しましたが、他の人が私が過去に持っていたのと同様の質問をした回答を見たいのですが。 – Mareth

4

cppファイルのss。 それは次のようになります。

void BaseClassWithPointer::destroyPointer() 
{ 
    delete q; 
} 

が、次のとおりです。

void destroyPointer() 
{ 
    delete q; 
} 

それはQ

1

関数destroyPointerは()CLAの一部ではないことが必要です。これは試してみてください代わりに

void BaseClassWithPointer::destroyPointer() 

関連する問題