[C++] error: ‘HEAD’ was not declared in this scope

This topic is 1874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Basically, I have my STACK class that has a pointer called HEAD (protected) and when I call refer to HEAD in a child class (ENHANCED_STACK) in a virtual method, I get this error:

% make clean && make
rm -rf *o main
g++ -c main.cpp
g++ -c stack.cpp
g++ -c enhanced-stack.cpp
enhanced-stack.cpp: In member function ‘void ENHANCED_STACK::Print_Stack()’:
enhanced-stack.cpp:11: error: ‘HEAD’ was not declared in this scope
make: *** [enhanced-stack.o] Error 1

Share on other sites

stack.hpp:

http://pastebin.com/0CeqQdxf

stack.cpp:

http://pastebin.com/g8ZySsU1

enhanced-stack.hpp:

http://pastebin.com/EP9fvpdV

enhanced-stack.cpp:

http://pastebin.com/ehxVabdE

EDIT: Ok, this is really weird.  I included those links in my first post, but for some reason the forum software just didn't bother posting them.  It's as if, it ignored those links completely and went ahead with everything after the [ / code ] tag.  What gives?

Edited by ysg

Share on other sites

EDIT: Ok, this is really weird.  I included those links in my first post, but for some reason the forum software just didn't bother posting them.  It's as if, it ignored those links completely and went ahead with everything after the [ / code ] tag.  What gives?

You're not the only one. Welcome to the wonderful world of the IP.Board text/post editor! Basically, it sucks, and it will make you angry.

Anyway, I'm not going to rant here...

That error is complaining that the variable HEAD doesn't exist. And indeed it doesn't. The name of the variable is Head (look carefully at the STACK class), not HEAD (and yes, C++ is case sensitive).

Edited by Cornstalks

Share on other sites

EDIT: Ok, this is really weird.  I included those links in my first post, but for some reason the forum software just didn't bother posting them.  It's as if, it ignored those links completely and went ahead with everything after the [ / code ] tag.  What gives?

You're not the only one. Welcome to the wonderful world of the IP.Board text/post editor! Basically, it sucks, and it will make you angry.

Anyway, I'm not going to rant here...

That error is complaining that the variable HEAD doesn't exist. And indeed it doesn't. The name of the variable is Head (look carefully at the STACK class), not HEAD (and yes, C++ is case sensitive).

Ah!  Darn!  I know it's case-sensitive, it was a stupid error on my part :) .

Is there a way to go back to a different text editor?  All text would be ideal.

Share on other sites

Is there a way to go back to a different text editor?  All text would be ideal.

Nope. You're stuck in editor hell. Welcome.

But the good news is that (at least from what Michael said in that other thread) IPS will be releasing an update that should (in theory) result in a properly working editor. The bad news is that they're doing significant changes to the code base so I'm guessing we won't be seeing that for a long time...

I've pretty much stopped posting code on this site. Which means I'm spending more time in the Lounge than I should.

Edited by Cornstalks

Share on other sites

Is there a way to go back to a different text editor?  All text would be ideal.

Hit the little light switch above the text editor. It will drop you to a plaintext + bbcode mode.

Share on other sites
You should develop a consistent style. Your NODE type has lowercase members, your STACK type has members beginning with uppercase. Choose one and use it everywhere. I would also point out that most coding conventions reserve ALL_UPPERCASE for the preprocessor.

As for your code itself, I think you are using inheritance incorrectly here. The member functions in the ENHANCED_STACK could be moved to the STACK class, or made free functions. It is unusual to force client code to copy data into a special class to print it, or to do equality tests.

In particular, instead of a Copy() function you should actually provide a copy constructor for the STACK class. You also should provide an assignment operator, to fulfil the rule of three. The Clean() function duplicates the logic of the STACK destructor, by moving it into the STACK type you can remove the redundancy by having the destructor call this function.

Here is a partially tested example of how this might work.

stack.hpp
#ifndef STACK_HPP
#define STACK_HPP

class STACK
{
public:
STACK();
STACK(const STACK &stack);
~STACK();
STACK &operator=(STACK stack);

void Swap(STACK &stack);

void Clean();
void Push(int value);
void Pop(int &value);
int Is_Empty();

private:
struct NODE
{
int value;
};

};

#endif

stack.cpp
#include "stack.hpp"

#include <algorithm>

{
}

{
NODE *previous = nullptr;
{
NODE * node = new NODE;
node->value = current->value;

if(previous)
{
}
else
{
}
previous = node;
}
}

STACK::~STACK()
{
Clean();
}

STACK &STACK::operator=(STACK stack)
{
Swap(stack);
return *this;
}

void STACK::Swap(STACK &stack)
{
}

void STACK::Clean()
{
int value;
while(!Is_Empty())
{
Pop(value);
}
}

int STACK::Is_Empty()
{
}

void STACK::Push(int value)
{
NODE * temp = new NODE;
temp->value = value;
}

void STACK::Pop(int &value)
{
delete temp;
}


The other functions, instead of being part of a type, are now free functions. They could be declared in the stack header, or you can continue to use a dedicated header (such as stack_utilities.hpp). I've used your original naming scheme.

enhanced_stack.hpp
#ifndef ENHANCED_STACK_HPP
#define ENHANCED_STACK_HPP

#include "stack.hpp"

void Print_Stack(STACK stack);
bool Equal(STACK a, STACK b);

#endif

enhanced_stack.cpp
#include "enhanced_stack.hpp"

#include <iostream>
#include <iomanip>

void Print_Stack(STACK stack)
{
std::cout << "Stack: ";
if(stack.Is_Empty())
{
std::cout << "<Empty>";
}
else
{
while(!stack.Is_Empty())
{
int value;
stack.Pop(value);
std::cout << std::setw(3) << value;
}
}
std::cout << std::endl;
}

bool Equal(STACK a, STACK b)
{
while(!(a.Is_Empty() || b.Is_Empty()))
{
int valueA;
a.Pop(valueA);

int valueB;
b.Pop(valueB);

if(valueA != valueB)
{
return false;
}
}

return a.Is_Empty() && b.Is_Empty();
}


A couple of other points to note:
• The copy constructor is complex, but it avoids a lot of unnecessary runtime work that your older Copy() did.
• I avoid including headers until they are need. Note how the standard headers are only included in the source file(s) that depend on them
• By providing the copy constructor, we can safely treat the STACK objects by value. Thus, writing Print_Stack() and Equal() as taking their arguments by value becomes the simplest and most natural way to implement these functions. You could also provide iterator functionality to avoid the need to make copies.
• The Swap() function was added to simplify adding an assignment operator. It isn't strictly necessary, but can be handy if you want to provide the same trick for a class that wants to make use of the STACK class while minimising runtime cost.

Share on other sites

Is there a way to go back to a different text editor?  All text would be ideal.

Hit the little light switch above the text editor. It will drop you to a plaintext + bbcode mode.

I've had problems in both plaintext mode and in WYSIWYG mode. Just a note that if you use plaintext mode, it may not solve your problems and it's still a good idea to copy (to save) your post before posting it just in case it gets destroyed.

Share on other sites

Is there a way to go back to a different text editor?  All text would be ideal.

Hit the little light switch above the text editor. It will drop you to a plaintext + bbcode mode.

Hmm... ok. That's a little better. Hopefully it doesn't cut off the rest of my posts.

Share on other sites

You should develop a consistent style. Your NODE type has lowercase members, your STACK type has members beginning with uppercase. Choose one and use it everywhere. I would also point out that most coding conventions reserve ALL_UPPERCASE for the preprocessor.

As for your code itself, I think you are using inheritance incorrectly here. The member functions in the ENHANCED_STACK could be moved to the STACK class, or made free functions. It is unusual to force client code to copy data into a special class to print it, or to do equality tests.

In particular, instead of a Copy() function you should actually provide a copy constructor for the STACK class. You also should provide an assignment operator, to fulfil the rule of three. The Clean() function duplicates the logic of the STACK destructor, by moving it into the STACK type you can remove the redundancy by having the destructor call this function.

Here is a partially tested example of how this might work.

stack.hpp

#ifndef STACK_HPP
#define STACK_HPP

class STACK
{
public:
STACK();
STACK(const STACK &stack);
~STACK();
STACK &operator=(STACK stack);

void Swap(STACK &stack);

void Clean();
void Push(int value);
void Pop(int &value);
int Is_Empty();

private:
struct NODE
{
int value;
};

};

#endif

stack.cpp
#include "stack.hpp"

#include <algorithm>

{
}

{
NODE *previous = nullptr;
{
NODE * node = new NODE;
node->value = current->value;

if(previous)
{
}
else
{
}
previous = node;
}
}

STACK::~STACK()
{
Clean();
}

STACK &STACK::operator=(STACK stack)
{
Swap(stack);
return *this;
}

void STACK::Swap(STACK &stack)
{
}

void STACK::Clean()
{
int value;
while(!Is_Empty())
{
Pop(value);
}
}

int STACK::Is_Empty()
{
}

void STACK::Push(int value)
{
NODE * temp = new NODE;
temp->value = value;
}

void STACK::Pop(int &value)
{
delete temp;
}


The other functions, instead of being part of a type, are now free functions. They could be declared in the stack header, or you can continue to use a dedicated header (such as stack_utilities.hpp). I've used your original naming scheme.

enhanced_stack.hpp
#ifndef ENHANCED_STACK_HPP
#define ENHANCED_STACK_HPP

#include "stack.hpp"

void Print_Stack(STACK stack);
bool Equal(STACK a, STACK b);

#endif

enhanced_stack.cpp
#include "enhanced_stack.hpp"

#include <iostream>
#include <iomanip>

void Print_Stack(STACK stack)
{
std::cout << "Stack: ";
if(stack.Is_Empty())
{
std::cout << "<Empty>";
}
else
{
while(!stack.Is_Empty())
{
int value;
stack.Pop(value);
std::cout << std::setw(3) << value;
}
}
std::cout << std::endl;
}

bool Equal(STACK a, STACK b)
{
while(!(a.Is_Empty() || b.Is_Empty()))
{
int valueA;
a.Pop(valueA);

int valueB;
b.Pop(valueB);

if(valueA != valueB)
{
return false;
}
}

return a.Is_Empty() && b.Is_Empty();
}


A couple of other points to note:
• The copy constructor is complex, but it avoids a lot of unnecessary runtime work that your older Copy() did.
• I avoid including headers until they are need. Note how the standard headers are only included in the source file(s) that depend on them
• By providing the copy constructor, we can safely treat the STACK objects by value. Thus, writing Print_Stack() and Equal() as taking their arguments by value becomes the simplest and most natural way to implement these functions. You could also provide iterator functionality to avoid the need to make copies.
• The Swap() function was added to simplify adding an assignment operator. It isn't strictly necessary, but can be handy if you want to provide the same trick for a class that wants to make use of the STACK class while minimising runtime cost.

Hi, thanks for giving me such a detailed response. I agree with about 70% of what you've written .

The code that you're viewing is what I got from this book:
http://www.amazon.com/Object-Oriented-Neural-Networks-Joey-Rogers/dp/0125931158

I've done C++ coding before, but haven't done it seriously for about 2 years (I'm a little rusty at the moment). I'm going through this C++ book/code to learn more about artificial neural networks (more specifically, how to make one and get a good understanding of the nuts and bolts explanation of how an ANN works).

• 16
• 9
• 13
• 41
• 15