Foward referencing broken?

J

John Watson

I can find no error w/ the code below but g++ complains that:
22: error: multiple types in one declaration

22 class Group;
23
24 class Cell{
25 public:
26 uint val;
27 Group *group;
28 Cell();
29 Cell(uint inval, Group *g);
30 };
31
32 class Group{
33 uint val;
34 vector<Cell *> members;
35 public:
36 Group(uint in); //: val(in) {}
37 void swap(Group *newGroup);
38 void add(Cell *inCell); //{ members.push_back(inCell);}
39 uint value(); // {return val;}
40 };

I need to forward declare class Group for class Cell to have a pointer to
that class. Does anyone know how to deal with two classes referencing
eachother?
Thanks,
John
 
K

Keith Davies

John Watson said:
I can find no error w/ the code below but g++ complains that:
22: error: multiple types in one declaration

22 class Group;
23
24 class Cell{
25 public:
26 uint val;
27 Group *group;
28 Cell();
29 Cell(uint inval, Group *g);
30 };
31
32 class Group{
33 uint val;
34 vector<Cell *> members;
35 public:
36 Group(uint in); //: val(in) {}
37 void swap(Group *newGroup);
38 void add(Cell *inCell); //{ members.push_back(inCell);}
39 uint value(); // {return val;}
40 };

I need to forward declare class Group for class Cell to have a pointer to
that class. Does anyone know how to deal with two classes referencing
eachother?

What happens before line 22? What I see here all looks okay,
declaration-wise.


Keith
 
J

John Watson

I edited the class names to be ACell and AGroup just in case they were
clashing with the include files. Again, the error occurs on line 20 w/
G++ spitting: Conc.cpp:20: error: multiple types in one declaration .

01 #include <iostream>
02 #include <pthread.h>
03 #include <semaphore.h>
04 #include <sstream>
05 #include <iomanip>
06 #include <fstream>
07 #include <sys/time.h>
08 #include <vector>
09 #define SHARED 1
10 typedef unsigned int uint;
11 using namespace std;
12
13 struct Task{
14 uint row1, row2;
15 Task *nxt;
16 Task(){}
17 Task(uint in1, uint in2):row1(in1), row2(in2){}
18 }
19
20 class AGroup;
21
22 class ACell{
23 public:
24 uint val;
25 AGroup *group;
26 ACell();
27 ACell(uint inval, AGroup *g);
28 };
29
30 class AGroup{
31 uint val;
32 vector<ACell *> members;
33 public:
34 AGroup(uint in); //: val(in) {}
35 void swap(AGroup *newAGroup);
36 void add(ACell *inACell); //{ members.push_back(inACell);}
37 uint value(); // {return val;}
38 };
39
40 ACell::ACell(){ group = NULL;}
41 ACell::ACell(uint inval, AGroup *g): val(inval), group(g){}
42
43 AGroup::AGroup(uint in): val(in) {}
44 void AGroup::swap(AGroup *newAGroup){
45 //iterate through all members and change their AGroup to newAGroup
46 size_t size = members.size();
47 vector<ACell *>::iterator firstElement;
48 for(int i = 0; i < size; i++){
49 firstElement = members.begin();
50 (*firstElement)->group = newAGroup;
51 newAGroup->add(*firstElement);
52 members.erase(firstElement);
53 }
54 }
55 void AGroup::add(ACell *inACell){members.push_back(inACell);}
56 uint AGroup::value() {return val;}


What gives?
-John Watson
 
G

Gianni Mariani

Keith said:
What happens before line 22? What I see here all looks okay,
declaration-wise.

exactly ... there seems to be a missing ";" at the end of a previous
definition or somthing like that.
 
I

Ian Collins

John said:
12
13 struct Task{
14 uint row1, row2;
15 Task *nxt;
16 Task(){}
17 Task(uint in1, uint in2):row1(in1), row2(in2){}
18 }
19
There's your problem, missing semicolon.
 
K

Keith Davies

John Watson said:
I edited the class names to be ACell and AGroup just in case they were
clashing with the include files. Again, the error occurs on line 20 w/
G++ spitting: Conc.cpp:20: error: multiple types in one declaration .

13 struct Task{
14 uint row1, row2;
15 Task *nxt;
16 Task(){}
17 Task(uint in1, uint in2):row1(in1), row2(in2){}
18 }

// ^^ notice a lack of semicolon here
19
20 class AGroup;

The parser sees:

struct Task{
uint row1, row2;
Task *nxt;
Task(){}
Task(uint in1, uint ins):row1(in1), row2(int2){}
} class AGroup;

This confuses the poor thing.

Well, not really *confuses* it. It knows it's wrong. I just doesn't
tell you what caused it.

As a general rule, if you get an error message like this -- any error
message that shows up where you're doing declarations, really -- check
for missing semicolons and mismatched braces ({}) before it. I expect
you'll most often find a missing semi or brace just before it.

At least that's what usually bites me. And it can give a *lot* of error
messages as a result, too.


Keith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top