클래스 헤더 파일에서 공용 데이터 멤버, 하나의 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 = #
}
BaseClassWithPointer::BaseClassWithPointer(int value)
{
num = value;
q = #
}
BaseClassWithPointer::BaseClassWithPointer(const BaseClassWithPointer& otherObject)
{
num = otherObject.num;
q = #
}
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 = #
}
return *this;
}
은 CPP에없는'의 #pragma once' 수행 Q를 찾을 수없는 이유입니다. 헤더 만. – David
사실'#pragma once '를 전혀 사용하지 마십시오. '# ifndef' 헤더 가드를 사용하십시오. '#pragma once'는 광범위하게 지원되지만 비표준입니다. –