LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2004, 09:57 PM   #1
ashirazi
Member
 
Registered: Jul 2004
Posts: 60

Rep: Reputation: 15
cpp forward declaration error


Hi,

Could someone please tell me why we would need to use forward declaration?? Cause I fixed of my errors using this and now im getting stupid errors just about everywere. I get the following errors for all the functions in the class InitialState.

Thanks
raven



ERROR:

InitialState.cpp:10: no `void InitialState::activate(Model)' member function
declared in class `InitialState'
InitialState.cpp: In member function `void InitialState::activate(Model)':
InitialState.cpp:11: `NullOperator' undeclared (first use this function)
InitialState.cpp:11: (Each undeclared identifier is reported only once for each
function it appears in.)
InitialState.cpp: At global scope:
InitialState.cpp:16: no `void InitialState::nonZeroDigitKeyPressed(Model, int)'
member function declared in class `InitialState'


**************************************
InitialState.h


#ifndef INITIALSTATE_H
#define INITIALSTATE_H

//class InitialState;
//#include "ICalcState.h"


class InitialState : public ICalcState{
public:
InitialState();
// const static InitialState *Singleton;

// private:
// InitialState getInstance();
// const static InitialState Instance;
};

#endif



**********************************
InitialState.cpp

//class InitialState;
#include "Model.h"
//#include "ICalcState.h"
#include "InitialState.h"

InitialState::InitialState(){
// Singleton = getInstance();
}

void InitialState::activate(Model model) {
model.set_currentOperation(NullOperator.Singleton);
model.set_pendingOperation(NullOperator.Singleton);
model.set_currentValue(0);
}

void InitialState::nonZeroDigitKeyPressed(Model model, int digit) {
model.set_currentValue(model.get_currentValue() * 10 + digit);
}

void InitialState::zeroDigitKeyPressed(Model model) {
model.set_currentValue(model.get_currentValue() * 10);
}

void InitialState:lusKeyPressed(Model model) {
model.set_currentOperation(new AddOp(model.get_currentValue()));
model.set_state(LowPrecedenceOperatorCurrent.Singleton);
}

void InitialState::minusKeyPressed(Model model) {
model.set_currentOperation(new SubOp(model.get_currentValue()));
model.set_state(LowPrecedenceOperatorCurrent.Singleton);
}

void InitialState::timesKeyPressed(Model model) {
model.set_currentOperation(new MulOp(model.get_currentValue()));
model.set_state(HighPrecedenceOperatorCurrent.Singleton);
}

void InitialState::divideKeyPressed(Model model) {
model.set_currentOperation(new DivOp(model.get_currentValue()));
model.set_state(DivOperatorCurrent.Singleton);
}

void InitialState::equalsKeyPressed(Model model) {
model.set_state(AfterEqualsState.Singleton);
}

void InitialState::clearKeyPressed(Model model) {
model.set_state(InitialState.Singleton);
}
 
Old 09-16-2004, 10:03 PM   #2
ashirazi
Member
 
Registered: Jul 2004
Posts: 60

Original Poster
Rep: Reputation: 15
I forgot to mention. InitialState is extending another class ICalcState and it has all function declared there.

****************************************
ICalcState.h

#ifndef ICALCSTATE_H
#define ICALCSTATE_H

#include "CalculatorEngine.h"
#include "Modl.h"
//extern class Model;

class ICalcState {
public:
ICalcState();
void activate(Model c);
void nonZeroDigitKeyPressed(Modl mod, int digit);
void zeroDigitKeyPressed(Model mod);
void plusKeyPressed(Model mod);
void minusKeyPressed(Model mod);
void timesKeyPressed(Model mod);
void divideKeyPressed(Model mod);
void equalsKeyPressed(Model mod);
void clearKeyPressed(Model mod);
};

#endif



**************************************
ICalcState.cpp


#include "Model.h"
#include "ICalcState.h"


ICalcState::ICalcState(){

}

void ICalcState::activate(Model mod) {

}

void ICalcState::nonZeroDigitKeyPressed(Model model, int digit) {

}

void ICalcState::zeroDigitKeyPressed(Model model) {

}

void ICalcState:lusKeyPressed(Model model) {

}

void ICalcState::minusKeyPressed(Model model) {

}

void ICalcState::timesKeyPressed(Model model) {

}

void ICalcState::divideKeyPressed(Model model) {

}

void ICalcState::equalsKeyPressed(Model model) {

}

void ICalcState::clearKeyPressed(Model model) {

}
 
Old 09-17-2004, 08:01 AM   #3
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
This is a throwback to C. In order to use any function (or C++ class or method) you have to declare it before it can be used.

The purpose of a header file is to declare classes, methods, globals (if any) and functions, prior to their being used. You thus have to list classes in dependency order.

For example, if I have a class a, and a class b::a (b derrives from a), and class c has a member variable of type class b, then I would have to declare class a first, then class b, then class c.

The same goes for methods; I must include all the prototype of class a if I want to use any methods from it in class b.

A forward declaration is used when you have a cyclic dependency. If you say, for instance:
Code:
class a; or
extern static const double pi;
void quicksort(std::list<double>,int,int);
then the lexer will know what type of thing the symbols a, pi and quicksort are when you try and use them later. You can then define them properly whenever you want, which is useful if you need to reference something in the definition that also references the thing you're trying to define.

I know it's annoying from a Java point of view, where the compiler (to over-simplify) parses laboriously through many files to find what all the symbols are before even thinking about lexing them. It is like this way in C++ in order to help out the compiler, so code can be compiled faster and thus you can spend more time developing.

The error message you're getting in this particular example comes from something else. You've created a prototype (which is a forward reference) for the class InitialState, and you've told this compiler that this class has only the default methods and a public null constructor and nothing else. From this information, the compiler works out things like how much memory to assign to the object.

You've then tried to tell the compiler what the void InitialState::Activate(Model) method of the class is. The class doesn't have one of these, so it's throwing an error. (By the way, do you realise that you're passing Model by value here and not by reference as you would be if this were Java; basically, this makes a copy of the Model object passed into the method, which may or may not be what you want).
 
Old 09-17-2004, 08:12 AM   #4
ashirazi
Member
 
Registered: Jul 2004
Posts: 60

Original Poster
Rep: Reputation: 15
What do u mean when you say the class doesnt have the activate function. Cause the class InitialState is extending ICalcState which has the decleration for activate. I didnt quite catch you there. I also posted the code to ICalcState in the reply to the main post.

And about the model being poassed as value. Yeah I caught that error earlier. Thanks
 
Old 09-17-2004, 09:02 AM   #5
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
The class does indeed have an 'activate' function but that is only by virtue of the fact that InitialState isa ICalcState.

InitialState does not have an 'activate' function of its own unless you declare (and implement) it.

The hierarchy, access rights and actual declaration determine how the actual call is resolved.

If you don't know about them, you might also want to look at virtual functions.
 
Old 09-17-2004, 12:55 PM   #6
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
In Java, any method declared in a class is automatically inherited by any derived/children classes, unless it is marked as “final”. Methods are said to be implicitly virtual.

In C++, it's the other way around. A method is not derived by its children classes unless it is marked as “virtual”. Methods are said to be implicitly final.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
g++ compiler error declaration does not declare anything Basiltp Programming 4 10-11-2004 11:29 AM
cpp error ashirazi Programming 1 09-18-2004 05:49 PM
debian dpkg cpp depends on cpp error darkleaf Linux - Software 2 06-25-2004 01:47 AM
CPP error, please help... TomAL Mandriva 3 12-24-2003 09:57 AM
cpp error ANU Linux - Software 1 12-05-2003 11:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration