2012-09-26 5 views
9

클래스 헤더 파일에서 공용 데이터 멤버, 하나의 int 및 해당 int에 대한 포인터로 선언 한 변수에 대해 C2065 오류가 발생했습니다. 오류로 표시된 코드 행은 함수에서 이러한 변수를 사용할 때만 - 클래스 생성자 내에서 정상적으로 처리되는 것처럼 보입니다. 바로 여기에 내 코드 블록과 헤더, 마지막으로"선언되지 않은 식별자"가 실제로 선언되었습니다.

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

:

여기 ++ 정상적인 C를 작성 (하지 비주얼 C++를) 비주얼 스튜디오 2010 익스프레스를 사용하고있어 컴파일러의 오류 로그의 출력입니다 :

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' 수행 Q를 찾을 수없는 이유입니다. 헤더 만. – David

+1

사실'#pragma once '를 전혀 사용하지 마십시오. '# ifndef' 헤더 가드를 사용하십시오. '#pragma once'는 광범위하게 지원되지만 비표준입니다. –

답변

12

destroyPointer() 메소드의 클래스 식별자를 잊어 버렸습니다.

void destroyPointer() 

... 

void print() 

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

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

+0

인쇄 방법에도 동일하게 적용됩니다. – Mark

+0

와우. 멍청한 느낌, 롤 ~ – Mareth

+0

매력 작품. 어떻게 이것을 답변으로 표시합니까? 사이트에 처음 게시하는 경우, 다른 사람들이 내가 과거에했던 비슷한 질문에 대한 답변을보고 싶지만. – Mareth

4

spp는 cpp 파일에 있습니다. 그것은해야한다 :

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

하지만입니다 :

void destroyPointer() 
{ 
    delete q; 
} 

이 그것을

1

함수 destroyPointer()가 CIA 요원의 일부가되어야한다 이것은 시도 대신

void BaseClassWithPointer::destroyPointer()