Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 ysg   Members   -  Reputation: 192

Like
0Likes
Like

Posted 06 April 2013 - 08:59 PM

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


Sponsor:

#2 ysg   Members   -  Reputation: 192

Like
0Likes
Like

Posted 06 April 2013 - 09:02 PM

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, 06 April 2013 - 09:04 PM.


#3 Cornstalks   Crossbones+   -  Reputation: 6991

Like
3Likes
Like

Posted 06 April 2013 - 09:14 PM

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, 06 April 2013 - 09:15 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#4 ysg   Members   -  Reputation: 192

Like
0Likes
Like

Posted 06 April 2013 - 09:41 PM

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.



#5 Cornstalks   Crossbones+   -  Reputation: 6991

Like
1Likes
Like

Posted 06 April 2013 - 09:57 PM

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, 06 April 2013 - 09:57 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#6 swiftcoder   Senior Moderators   -  Reputation: 10446

Like
1Likes
Like

Posted 07 April 2013 - 01:29 AM

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.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#7 rip-off   Moderators   -  Reputation: 8764

Like
2Likes
Like

Posted 07 April 2013 - 07:16 AM

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;
		NODE * link;
	};
 
	NODE * Head;
};

#endif
stack.cpp
#include "stack.hpp"

#include <algorithm>

STACK::STACK() : Head(nullptr)
{
}

STACK::STACK(const STACK &stack) : Head(nullptr)
{
	NODE *previous = nullptr;
	for(NODE *current = stack.Head ; current ; current = current->link)
	{
		NODE * node = new NODE;
		node->value = current->value;
		node->link = nullptr;
		
		if(previous)
		{
			previous->link = node;
		}
		else
		{
			Head = node;
		}
		previous = node;
    }
}

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

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

void STACK::Swap(STACK &stack)
{
	std::swap(Head, stack.Head);
}

void STACK::Clean()
{
	int value;
	while(!Is_Empty())
	{
		Pop(value);
	}
}
 
int STACK::Is_Empty()
{
	return !Head;
}
 
void STACK::Push(int value)
{
	NODE * temp = new NODE;
	temp->value = value;
	temp->link = Head;
	Head = temp;
}
 
void STACK::Pop(int &value)
{
	NODE * temp = Head;
	value = Head->value;
	Head = Head->link;
	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.


#8 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 07 April 2013 - 08:42 AM

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.


[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#9 ysg   Members   -  Reputation: 192

Like
0Likes
Like

Posted 07 April 2013 - 12:19 PM


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.

#10 ysg   Members   -  Reputation: 192

Like
0Likes
Like

Posted 07 April 2013 - 12:33 PM

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;
		NODE * link;
	};
 
	NODE * Head;
};

#endif
stack.cpp
#include "stack.hpp"

#include <algorithm>

STACK::STACK() : Head(nullptr)
{
}

STACK::STACK(const STACK &stack) : Head(nullptr)
{
	NODE *previous = nullptr;
	for(NODE *current = stack.Head ; current ; current = current->link)
	{
		NODE * node = new NODE;
		node->value = current->value;
		node->link = nullptr;
		
		if(previous)
		{
			previous->link = node;
		}
		else
		{
			Head = node;
		}
		previous = node;
    }
}

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

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

void STACK::Swap(STACK &stack)
{
	std::swap(Head, stack.Head);
}

void STACK::Clean()
{
	int value;
	while(!Is_Empty())
	{
		Pop(value);
	}
}
 
int STACK::Is_Empty()
{
	return !Head;
}
 
void STACK::Push(int value)
{
	NODE * temp = new NODE;
	temp->value = value;
	temp->link = Head;
	Head = temp;
}
 
void STACK::Pop(int &value)
{
	NODE * temp = Head;
	value = Head->value;
	Head = Head->link;
	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 smile.png .

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).




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS