C++ include? probleem

Pagina: 1
Acties:

  • corani
  • Registratie: December 2000
  • Laatst online: 05-10-2017

corani

__,,,_(^_^)_,,,__

Topicstarter
Ik krijg hele rare errors tijdens het compileren van deze code.. Het zijn twee files, die elkaar includen, en ik heb het vermoeden dat het daar wel eens aan zou kunnen liggen. Maar volgens mij moet het zo toch goed zijn?

Component.h
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef __COMPONENT_H
#define __COMPONENT_H

#define __R 1000
#define __C 1001
#define __L 1002
#define __I 1003
#define __V 1004

#include "Knooppunt.h"

class Component
{
    public:
        Component(int theKind, int theNr, double theValue, int nrOfKnooppunt);
        ~Component() {}
        int getKind();
        int getNr();
        double getValue();
        void setVariable(int nrOfSteps, double stepSize);
        int getNrOfSteps();
        double getStepSize();
        bool isVariable();
        void setKnooppunt(int nr, Knooppunt *knooppunt);
    private:
        int nr;
        double value;
        int kind;
        bool variable;
        int nrOfSteps;
        double stepSize;
        Knooppunt *knooppunten[];
};
#endif

Knooppunt.h
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef __KNOOPPUNT_H
#define __KNOOPPUNT_H

#include "Component.h"

class Knooppunt
{
    public:
        Knooppunt(int theNr);
        ~Knooppunt();
        void newComponent(Component *theComp);
        int getNrComponenten();
        int getNr();
        void setVoltage(double theVoltage);
        double getVoltage();
        Component *getComponent(int compNr);


    private:
        Component *componenten[];
        
        int nr, aantalComponenten;
        double voltage;
};
#endif

En dit zijn de errors die ik krijg bij het compileren van Component:
code:
1
2
3
4
5
6
7
8
flatrix:/home/school/NWCP Project/blaat$ g++ -c Component.cpp
In file included from Component.h:10,
             from Component.cpp:1:
Knooppunt.h:11: `Component' was not declared in this scope
Knooppunt.h:11: `theComp' was not declared in this scope
Knooppunt.h:11: variable or field `newComponent' declared void
Knooppunt.h:16: syntax error before `*'
Knooppunt.h:20: syntax error before `*'

En bij het compileren van Knooppunt:
code:
1
2
3
4
5
6
flatrix:/home/school/NWCP Project/blaat$ g++ -c Knooppunt.cpp
In file included from Knooppunt.h:4,
             from Knooppunt.cpp:1:
Component.h:24: type specifier omitted for parameter
Component.h:24: parse error before `*'
Component.h:32: syntax error before `*'

Laat me nou toch eens met rust man!
Iedereen die in telekinese gelooft, steek a.u.b. mijn hand op


  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Waarom "crossinclude" je die files :?
[edit]
Of hoort dat zo? :)

  • Flipmo2K
  • Registratie: April 2000
  • Niet online
moet het ook niet zijn #include <component.h>? Zo heb ik 't tenminste geleerd...

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op donderdag 28 februari 2002 13:56 schreef Flipmo2K het volgende:
moet het ook niet zijn #include <component.h>? Zo heb ik 't tenminste geleerd...
Nee, die <> is alleen voor als de file ergens in een "standaard" include dir staat.

  • krietjur
  • Registratie: Februari 2001
  • Laatst online: 21:29

krietjur

Where am I?

Op donderdag 28 februari 2002 13:56 schreef Flipmo2K het volgende:
moet het ook niet zijn #include <component.h>? Zo heb ik 't tenminste geleerd...
Volgens mij klopt dat #include "component.h" wel hoor, zo doe ik 't in elk geval ook altijd ;)
Probleem zal inderdaad wel zitten in wat ACM al zegt, het crossincluden..

  • corani
  • Registratie: December 2000
  • Laatst online: 05-10-2017

corani

__,,,_(^_^)_,,,__

Topicstarter
Nou, in knooppunt heb ik component nodig, en in component heb ik knooppunt nodig..

Laat me nou toch eens met rust man!
Iedereen die in telekinese gelooft, steek a.u.b. mijn hand op


  • Toiletman
  • Registratie: Februari 2000
  • Laatst online: 13-09 14:36
jaja, ik ben ook een n00b, maar dit probleem had ik dus ook... als je search had gebruikt had je het waarschijnlijk wel gevonden, maar het komt d'r op neer dat je je classes eerst ff moet definieren...

dus naast de file includen, ook gewoon class knooppunt;
in die component header zetten, en andersom ook
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
//********************Player Declaration*****************
#ifndef _PLAYER
#define _PLAYER
class Area;                     
#include "Area.h"

class Player
{
    Area* itsVillage;
...
};

#endif   //_PLAYER

Only dead fish go with the flow


  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Op donderdag 28 februari 2002 14:08 schreef corani het volgende:
Nou, in knooppunt heb ik component nodig, en in component heb ik knooppunt nodig..
Werkt het niet zo:
ipv in component.h de knooppunt.h en vice versa includen:

component.cpp:
#include component.h
#include knooppunt.h

en in knooppunt.cpp hetzelfde?
[edit]
Of gewoon 1 header maken met veel van je classes erin?

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 11-09 08:26

.oisyn

Moderator Devschuur®

Demotivational Speaker

Op donderdag 28 februari 2002 14:10 schreef Toiletman het volgende:
jaja, ik ben ook een n00b, maar dit probleem had ik dus ook... als je search had gebruikt had je het waarschijnlijk wel gevonden, maar het komt d'r op neer dat je je classes eerst ff moet definieren...

dus naast de file includen, ook gewoon class knooppunt;
in die component header zetten, en andersom ook
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
//********************Player Declaration*****************
#ifndef _PLAYER
#define _PLAYER
class Area;                     
#include "Area.h"

class Player
{
    Area* itsVillage;
...
};

#endif   //_PLAYER
het idee is goed, je doet het alleen verkeerd :)

Wat je moet doen, is gewoon helemaal niet cross-includen, en gewoon van te voren aangeven dat Knooppunt en Component classes zijn voordat je ze gebruikt

in Component.h
code:
1
2
3
4
5
6
7
8
9
10
11
#ifndef COMPONENT_H
#define COMPONENT_H

class Knooppunt;

class Component
{
    ...
};

#endif

en in Knooppunt.h:
code:
1
2
3
4
5
6
7
8
9
10
11
#ifndef KNOOPPUNT_H
#define KNOOPPUNT_H

class Component;

class Knooppunt
{
    ...
};

#endif

Let wel dat dit alleen werkt als je enkel referenties naar die klasse gebruikt (pointers en parameters voor functies dus), en niet een instantie van de klasse zelf als member, want dan moet de compiler wel de definitie van die klasse weten.

Maw, als je dit hebt:
code:
1
2
3
4
class ClassA
{
    ClassB b;
};

dan zul je wel eerst ClassB moeten definieren voordat ClassA gedefinieerd wordt

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


  • corani
  • Registratie: December 2000
  • Laatst online: 05-10-2017

corani

__,,,_(^_^)_,,,__

Topicstarter
Hieperdepiep hoera! Het werkt!! :) Bedankt OiSyN en de rest!

de declaratie in de header zelf erbij zetten werkte!
Op donderdag 28 februari 2002 14:24 schreef OiSyN het volgende:

in Component.h
code:
1
2
3
4
5
6
7
8
9
10
11
#ifndef COMPONENT_H
#define COMPONENT_H

class Knooppunt;

class Component
{
    ...
};

#endif

en in Knooppunt.h:
code:
1
2
3
4
5
6
7
8
9
10
11
#ifndef KNOOPPUNT_H
#define KNOOPPUNT_H

class Component;

class Knooppunt
{
    ...
};

#endif

Laat me nou toch eens met rust man!
Iedereen die in telekinese gelooft, steek a.u.b. mijn hand op

Pagina: 1