LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   desperate help, CPP ERROR (https://www.linuxquestions.org/questions/programming-9/desperate-help-cpp-error-231198/)

ashirazi 09-16-2004 12:41 AM

desperate help, CPP ERROR
 
Hey,

Im gettin this real weird error while compiling this file name ICalcState.cpp. The functions in this class require the class object Medl to be passed. But when I compile, I get an error saying,

ICalcState.h:12: `Modl' was not declared in this scope

for every locations where Modl is used, even after I included the Model header file. I rechecked the header name, and all other syntax. Im using gcc to compile and im also using a makefile. The Modl class compiles just fine, but this thing started after I created the ICalcState.


ICalcState is the only file throwing the error, and its located in the same dir as the rest of the classes.


Thanks a ton
raven




The files are as follows:

*********************************
Modl.h

#ifndef MODL_H
#define MODL_H

#include "NullOperation.h"
#include "NullCalcInterface.h"
#include "CalculatorEngine.h"
#include "ICalcState.h"

class Modl : public NullCalcInterface{

public:
NullOperation _pendingOperation;
NullOperation _currentOperation;
int _currentValue;
CalculatorEngine *_gui;
ICalcState _state;

// public:
Modl (CalculatorEngine *c);
int get_currentValue();
void set_currentValue(int value);
NullOperation get_currentOperation();
void set_currentOperation(NullOperation operation);
NullOperation get_pendingOperation();
void set_pendingOperation(NullOperation operation);
void set_error();
void set_state(NullCalcInterface s);
void clearKeyPressed();
void nonZeroDigitKeyPressed(int digit);
void zeroKeyPressed();
void plusKeyPressed();
void minusKeyPressed();
void timesKeyPressed();
void divideKeyPressed();
void equalsKeyPressed();
};

class someC {
public:
someC();
};

#endif



*********************************
Modl.cpp

#include "NullCalcInterface.h"
#include "Operations.h"
#include "NullOperation.h"
#include "CalculatorEngine.h"
//#include "ICalcState.h"
#include "Modl.h"

Modl::Modl(CalculatorEngine *c) {
_gui = c;
set_state(InitialState->Singleton);
}

int Modl::get_currentValue() {
return _currentValue;
}

void Modl::set_currentValue(int value){
_currentValue = value;
_gui->setDisplay(_currentValue);
}

NullOperation Modl::get_currentOperation() {
return _currentOperation;
}

void Modl::set_currentOperation(NullOperation operation) {
_currentOperation = operation;
}

NullOperation Modl::get_pendingOperation() {
return _pendingOperation;
}

void Modl::set_pendingOperation(NullOperation operation) {
_pendingOperation = operation;
}

void Modl::set_error(){
_gui->set_error();
}

void Model::set_state(NullCalcInterface s){
_state = s;
_state.activate(this);
}

void Modl::clearKeyPressed() {
_state.clearKeyPressed(this);
}

void Modl::nonZeroDigitKeyPressed(int digit){
_state.nonZeroDigitKeyPressed(this, digit);
}

void Modl::zeroKeyPressed() {
_state.zeroDigitKeyPressed(this);
}

void Modl::plusKeyPressed() {
_state.plusKeyPressed(this);
}

void Modl::minusKeyPressed() {
_state.minusKeyPressed(this);
}

void Modl::timesKeyPressed() {
_state.timesKeyPressed(this);
}

void Modl::divideKeyPressed() {
_state.divideKeyPressed(this);
}

void Modl::equalsKeyPressed() {
_state.equalsKeyPressed(this);
}

someC::someC(){
}


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

#ifndef ICALCSTATE_H
#define ICALCSTATE_H

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

class ICalcState {
public:
ICalcState();
void activate(Modl 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 "Modl.h"
#include "ICalcState.h"


ICalcState::ICalcState(){

}

void ICalcState::activate(Modl l) {

}

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

}

void ICalcState::zeroDigitKeyPressed(Model model) {

}

void ICalcState::plusKeyPressed(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) {

}




********************************
Makefile

CONFIG=/projects/CSE250/C++/include/MakeConfiguration
include $(CONFIG)

# These variables determine where variable files are located.
Bin = .
oDir = .
sDir = .

#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# This variable determines the name of the executable to be produced.
PROG= $(Bin)/Calculator

EXOBJS= $(oDir)/CalculatorEngine.o $(oDir)/Addition.o $(oDir)/State.o $(oDir)/Subtraction.o $(oDir)/Multiplication.o $(oDir)/Division.o $(oDir)/Operations.o $(oDir)/NullOperation.o $(oDir)/Modl.o $(oDir)/NullCalcInterface.o $(oDir)/ICalcState.o

CLEANEXTS= $(PROG) $(EXOBJS)

#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

.PHONY: default all objs clean cleanobj cleanall

default: all

all: $(PROG)

objs: $(EXOBJS)

# Note that this rule has no dependencies listed; it still requires two lines!
# See the explanation of rule structure below.
clean:
-rm -f $(CLEANEXTS); rm -f *~ \#*

#
# EXPLANATION OF HOW TO MAKE THE ZIP FILE TO SUBMIT
#
# This will clean and then make then make a submission-ready zip file.
# Note that it is assumed that the current directory is named 'PP1'.
# To create the zip file to submit, type 'gmake submitZip' in the
# current directory (which should be named PP1).
submitZip: clean
( cd ..; rm PP1.zip ; zip -r PP1.zip PP1 )

#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

$(PROG): $(EXOBJS)
$(CXX) -o $@ $(EXOBJS) $(OBJS) $(LDFLAGS)

$(oDir)/CalculatorEngine.o: $(sDir)/CalculatorEngine.cpp $(sDir)/CalculatorEngine.h
$(CXX) -c $(CFLAGS) -o $@ $<

$(oDir)/State.o: $(sDir)/State.cpp $(sDir)/State.h
$(CXX) -c $(CFLAGS) -o $@ $<

$(oDir)/Addition.o: $(sDir)/Addition.cpp $(sDir)/Addition.h
$(CXX) -c $(CFLAGS) -o $@ $<

$(oDir)/Subtraction.o: $(sDir)/Subtraction.cpp $(sDir)/Subtraction.h
$(CXX) -c $(CFLAGS) -o $@ $<

$(oDir)/Multiplication.o: $(sDir)/Multiplication.cpp $(sDir)/Multiplication.h
$(CXX) -c $(CFLAGS) -o $@ $<

$(oDir)/Division.o: $(sDir)/Division.cpp $(oDir)/Division.h
$(CXX) -c $(CFLAGS) -o $@ $<

$(oDir)/Operations.o: $(sDir)/Operations.cpp $(oDir)/Operations.h
$(CXX) -c $(CFLAGS) -o $@ $<

$(oDir)/NullOperation.o: $(sDir)/NullOperation.cpp $(oDir)/NullOperation.h
$(CXX) -c $(CFLAGS) -o $@ $<

$(oDir)/Modl.o: $(sDir)/Modl.cpp $(oDir)/Modl.h
$(CXX) -c $(CFLAGS) -o $@ $<

$(oDir)/NullCalcInterface.o: $(sDir)/NullCalcInterface.cpp $(oDir)/NullCalcInterface.h
$(CXX) -c $(CFLAGS) -o $@ $<

$(oDir)/ICalcState.o: $(sDir)/ICalcState.cpp $(oDir)/ICalcState.h
$(CXX) -c $(CFLAGS) -o $@ $<

rjlee 09-16-2004 05:49 AM

Re: desperate help, CPP ERROR
 
ICalcState.h is being included by Modl.h before the definition of the modl class.
You can fix the problem by adding the line
Code:

class Modl;
just before the line
Code:

#include "ICalcState.h"
in Modl.h

This will create a forward declaration of the class so that the compiler knows what Modl actually is before it tries to use it.

ashirazi 09-16-2004 02:37 PM

Thanks for the reply, but im gettin a somewhat similar error with another class now. Model used here is the same as the Modl class that we refered to earlier.

The compilation error is as follows:



InitialState.h:11: invalid use of undefined type `class InitialState'
InitialState.h:8: forward declaration of `class InitialState'
InitialState.h:11: ISO C++ forbids in-class initialization of non-const static
member `Singleton'
Model.cpp: In constructor `Model::Model(CalculatorEngine*)':
Model.cpp:10: parse error before `.' token



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

#ifndef INITIALSTATE_H
#define INITIALSTATE_H

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


class InitialState : public ICalcState{
public:
InitialState();
static InitialState Singleton = new InitialState();

};

#endif

dakensta 09-16-2004 05:50 PM

static members go into the implementation (cpp file) like this
Code:

int class_name::member_name = 100;
Also you appear to be assigning a pointer to an object!

And at the risk of jumping the gun ...

with singletons (I notice the name of your member) here is an example :

Code:

// .hpp header file

class singleton
{
public:
  static singleton* get_instance() ;

private:
  // could be protected ...
  singleton() { /* do construction stuff */ }

};

// .cpp implementation file

singleton* singleton::get_instance()
{
    static singleton instance;  // one object will be created on the first call of this function
    return &instance;
}

Just typed this from memory so you might want to check it ...

And this isn't the only way to create a singleton but the important point is that the constructor is private otherwise people can create more singletons and it will quickly become a multiton

HTH

rjlee 09-17-2004 03:36 AM

Quote:

Originally posted by ashirazi
InitialState.h

#ifndef INITIALSTATE_H
#define INITIALSTATE_H

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


class InitialState : public ICalcState{
public:
InitialState();
static InitialState Singleton = new InitialState();

};

#endif
I'd just like to add that this is a typical mistake for someone coming from a language like Java or Perl (using Object-Orientation). In Java, there are three types of variable or member:

A value, such as int x; SomeClass y; You access members with the dot operator, and it's allocated on the stack (which is fast and efficient, but goes out of scope after you exit the appropriate block of code).

A reference, such as int & xref(x); SomeClass & yref(y); A reference will always contain the same value as the object to which it refernces, and must not go out of scope before the referenced variable does. This is similar to the assignment operator in Java (for classes); after you change x, xref will be different and vice. versa. The difference being that a reference cannot be null.

A pointer, such as int * px; SomeClass * py; A pointer is simply a regular variable that holds the address of another variable. You can use it like a reference with the * (dereference) operator (*px or *py) or access members in the structure being pointed to with the -> operator.

The new operator always returns a pointer to the allocated variable, so pointers are often used to hold dynamic variables in C++. A variable allocated with new() has scope until you free it with delete(), even if the pointer goes out of scope. On some operating systems (like Windows XP), failing to call delete(pointer) on a variable at all will cause a memory leak.


All times are GMT -5. The time now is 06:52 AM.