2012-09-26 12 views
9

Otrzymałem błąd C2065 dla zmiennych, które zadeklarowałem w pliku nagłówkowym klasy jako publiczni członkowie danych, jeden int i jeden wskaźnik do tego int. Wiersze kodu oznaczonego jako błędne są tylko wtedy, gdy używam tych zmiennych w funkcji - w klasie konstruktor wydaje się, że przechodzi dobrze."Niezadeklarowany identyfikator" jest faktycznie zadeklarowany

Używam programu Visual Studio 2010 Express napisać normalnego C++ (nie Visual C++), i tu jest wyjście dzienniku błędów kompilatora za:

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

Wreszcie here're moje bloki kodu i nagłówki:

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

NIE '#pragma once' w KPP. Tylko nagłówki. – David

+1

Tak naprawdę nie "#pragma raz" w ogóle. Użyj osłony nagłówka '# ifndef'. '#pragma once', chociaż jest szeroko wspierany, jest niestandardowe. –

Odpowiedz

12

Nie pamiętasz identyfikatora klasy dla metody destroyPointer(). Spróbuj

void BaseClassWithPointer::destroyPointer() 

zamiast

+0

To samo dotyczy metody drukowania. – Mark

+0

Wow. Czuję się głupio, lol ~ – Mareth

+0

Działa jak urok. Jak oznaczyć to jako odebrane? Po raz pierwszy zamieszczam na stronie, chociaż lubię patrzeć na odpowiedzi, które inni ludzie mieli do podobnych pytań, które miałem w przeszłości. – Mareth

4

to:

void destroyPointer() 

... 

void print() 

Powinny być

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

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

itp

1

destroyPointer function() nie jest częścią CLA ss w pliku cpp. Powinno być:

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

ale jest:

void destroyPointer() 
{ 
    delete q; 
} 

To dlatego, że nie może znaleźć q

Powiązane problemy