473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Failed to compile in C++

Hi,

I got the following snippet of codes which gave me the following
compile errors. I would really appreciate if anyone could tell
me what I have done wrong.

Expand|Select|Wrap|Line Numbers
  1.      1  #include <iostream>
  2.      2  #include <string>
  3.      3
  4.      4  class State;
  5.      5  class StateMachineInterface
  6.      6  {
  7.      7     public:
  8.      8          virtual void gotApplication() { }
  9.      9          virtual void checkApplication() { }
  10.     10          virtual void setState(State* s) { }
  11.     11  };
  12.     12
  13.     13  // class WaitingState;
  14.     14  class ConcreteStateMachine : public StateMachineInterface
  15.     15  {
  16.     16     public:
  17.     17          ConcreteStateMachine(int n)
  18.     18          {
  19.     19             waitingState    = new WaitingState(this);
  20.     20             state           = waitingState;
  21.     21          }
  22.     22
  23.     23          void gotApplication()
  24.     24          {   cout << state->gotApplication(); }
  25.     25
  26.     26          void checkApplication()
  27.     27          {  cout << state->checkApplication();  }
  28.     28
  29.     29          virtual void setState(State* s)
  30.     30          {  state = s;  }
  31.     31
  32.     32     private:
  33.     33          State* waitingState;
  34.     34          State* state;
  35.     35  };
  36.     36
  37.     37  class State
  38.     38  {
  39.     39     public:
  40.     40          virtual string gotApplication() = 0;
  41.     41          virtual string checkApplication() =  0;
  42.     42  };
  43.     43
  44.     44  class WaitingState : public State
  45.     45  {
  46.     46     public:
  47.     47          WaitingState(StateMachineInterface* a)
  48.     48          {  automat = a; }
  49.     49
  50.     50          string gotApplication()
  51.     51          {
  52.     52            automat->setState(automat->getGotApplicationState());
  53.     53             return "Thanks for the application.";
  54.     54          }
  55.     55
  56.     56          string checkApplication()
  57.     57          {   return "You have to submit an application.";  }
  58.     58
  59.     59     private:
  60.     60          StateMachineInterface* automat;
  61.     61  };
  62.     62
  63.     63  int main()
  64.     64  {    }
  65.  
  66.  
Compile errors
==========================
/class/PATTERN-DESIGN/state% g++ example.cc
example.cc: In method `ConcreteStateMachine::ConcreteStateMachine(int)':
example.cc:19: parse error before `('
example.cc: In method `void ConcreteStateMachine::gotApplication()':
example.cc:24: no matching function for call to `State::gotApplication ()'
example.cc: In method `void ConcreteStateMachine::checkApplication()':
example.cc:27: no matching function for call to `State::checkApplication ()'
example.cc: In method `class basic_string<char,string_char_traits<char>,__defau lt_alloc_template<false,0> > WaitingState::gotApplication()':
example.cc:52: no matching function for call to `StateMachineInterface::getGotApplicationState ()'
======================================

To start off, why does it complain about lines 19 and 24?

Thanks in advance,

PL
Dec 12 '11 #1

✓ answered by weaknessforcats

You are using waitingState as a type before the compiler has seen the class definition. Therefore, waitingState is not a valid type.

Use a forward reference to waitingState before the ConcreteStateMachine class declaration:

Expand|Select|Wrap|Line Numbers
  1. class waitingState; //forward reference
  2.  
  3. class ConcreteStateMachine ...etc
This is enough info for the compier to accept a waitingState*

4 1896
johny10151981
1,059 1GB
Where did you define waitingState????
Dec 12 '11 #2
Johny10151981:
I defined waitingState as an object pointer in line 34. Are u suggesting
that waitingState as an pointer is causing the problem here?
thanks,

PL
Dec 12 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
You are using waitingState as a type before the compiler has seen the class definition. Therefore, waitingState is not a valid type.

Use a forward reference to waitingState before the ConcreteStateMachine class declaration:

Expand|Select|Wrap|Line Numbers
  1. class waitingState; //forward reference
  2.  
  3. class ConcreteStateMachine ...etc
This is enough info for the compier to accept a waitingState*
Dec 12 '11 #4
Thanks weaknessforcats, it worked after I forward-declared the waitingState class, and moved the codes around. PL
Dec 12 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: | last post by:
Some time ago I installed VC# 2003, made a small generic project, compile with the allow unsafe flag and I get the error below: "error CS1577: Assembly generation failed -- Unexpected exception...
1
by: sam++ | last post by:
Hi, I cd into examples/DLL/ and type "make", it failed to compile the example. The error is: # make make - -f DLL_Today.bor all make: don't know how to make all. Stop *** Error code 2 ...
5
by: Brice Prunier | last post by:
Here under 4 schemas i'm working with ( it may be long: sorry...) The context is the following : Resident.xsd imports Person.xsd and includes Common.xsd ( anonimous schema: no TargetNamespace )...
8
by: | last post by:
Wel, I am rebuilding the VC# 2002 project that I have deployment problems with the 2003 version, hoping this solves the problems, but now I encounter this wierd bug??? If I have the project, and...
0
by: Rich | last post by:
I get this message when running almost every time I do an incremental compile. A full rebuild makes it go away. A subsequent incremental compile causes the problem again. I've read posts...
0
by: Jérôme Le Bougeant | last post by:
Hello (and sorry for my English), I downloaded the VideoCapture module on the http://videocapture.sourceforge.net/ site. I tested it with a webcam and that functions. Now I want to...
1
by: å¼ æ²ˆé¹ | last post by:
How to compile the HelloWorld of boost.asio? Maybe this is a stupid problem , but I really don't konw how to find the right way. My compile environment is WinXP, Msys , MinGw , G++ 3.4.2,...
2
by: Bruza | last post by:
I installed MySQL 5.0.45 on Ubuntu 7.1 and download MySQL_python from Sourceforge (http://sourceforge.net/project/showfiles.php? group_id=22307). Then I untar the package and executed "python...
2
by: BruceWho | last post by:
I downloaded boost1.35.0 and built it with following command: bjam --toolset=msvc-7.1 --variant=release --threading=multi -- link=shared --with-system stage and it failed to compile, error...
2
by: akhilesh.noida | last post by:
I am trying to compile glibc-2.5 for ARM based board. But I am getting errors while configuring it. Please check and give your inputs for resolving this. configure command : $...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.