2012-09-26 42 views
9

我已经收到错误C2065s的变量,我已经在类头文件中声明为公共数据成员,一个int和一个指向该int的指针。被标记为错误的代码行只有当我在函数中使用这些变量时 - 在类的构造函数中,它们看起来好像没有问题。“未声明的标识符”实际上是声明的

我使用Visual Studio 2010 Express来写普通的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; 
} 
+2

不要'的#pragma once'以CPP。只有头部。 – David

+1

事实上,不要'#pragma once'。使用'#ifndef'头文件。 '#pragma Once'虽然得到广泛支持,但是并不标准。 –

回答

12

您忘记了您的destroyPointer()方法的类标识符。 尝试

void BaseClassWithPointer::destroyPointer() 

代替

+0

这同样适用于打印方法。 – Mark

+0

哇。我感到愚蠢,哈哈〜 – Mareth

+0

工程就像一个魅力。我如何将此标记为已回答?第一次在网站上发布,虽然我喜欢看答案,但其他人也遇到过我曾遇到的类似问题。 – Mareth

4

此:

void destroyPointer() 

... 

void print() 

应该是

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

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

1

函数destroyPointer()不是CLA的一部分ss在cpp文件中。 它应该是:

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

但:

void destroyPointer() 
{ 
    delete q; 
} 

这就是为什么它不能找出q