2012-09-26 43 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