9

クラスヘッダーファイルでパブリックデータメンバーとして宣言した変数に対してエラーC2065が発生し、1つのintとそのintへの1つのポインターが発生します。エラーとしてフラグが立てられているコード行は、これらの変数を関数で使用する場合にのみ発生します。クラスのコンストラクター内では、問題なく処理されているように見えます。

Visual Studio 2010Expressを使用して通常のC++(Visual C ++ではない)を記述しています。コンパイラのエラーログの出力は次のとおりです。

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 ==========

最後に、これが私のコードブロックとヘッダーです:

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;
}
4

3 に答える 3

12

destroyPointer()メソッドのクラス識別子を忘れました。試す

void BaseClassWithPointer::destroyPointer()

代わりは

于 2012-09-26T19:50:23.470 に答える
4

これ:

void destroyPointer()

...

void print()

する必要があります

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

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

于 2012-09-26T19:51:16.827 に答える
1

関数destroyPointer()は、cppファイルのクラスの一部ではありません。そのはず:

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

しかし:

void destroyPointer()
{
  delete q;
}

これがqが見つからない理由です

于 2012-09-26T19:51:38.363 に答える