"undeclared identifier" is actually declared
Asked Answered
M

3

9

I've been getting error C2065s for variables that I've declared in the class header file as public data members, one int and one pointer to that int. The lines of code being flagged as erroneous are only when I use these variables in a function - within the class' constructor, they appear to get through okay.

I'm using Visual Studio 2010 Express to write normal C++ (not Visual C++), and here's the output of the compiler's error log:

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

Finally, here're my code blocks and headers:

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;
}
Melissamelisse answered 26/9, 2012 at 19:48 Comment(2)
Don't #pragma once in a cpp. Headers only.Winou
In fact, don't #pragma once at all. Use a #ifndef header guard. #pragma once, while widely supported, is nonstandard.Havildar
T
12

You forgot the Class identifier for your destroyPointer() method. Try

void BaseClassWithPointer::destroyPointer()

instead

Tele answered 26/9, 2012 at 19:50 Comment(2)
the same applies for the print method.Tele
Works like a charm. How do I mark this as answered? First time posting on the site, though I like to look at answers other people got to similar questions I've had in the past.Melissamelisse
V
4

This:

void destroyPointer()

...

void print()

Should be

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

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

etc.

Ventriloquize answered 26/9, 2012 at 19:51 Comment(0)
H
1

The function destroyPointer() is not part of the class in the cpp file. It should be:

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

but is:

void destroyPointer()
{
  delete q;
}

This is why it cannot find q

Heterogeneity answered 26/9, 2012 at 19:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.