Взял пример из учебника С++ изнутри (Вайнер), но при попытке скомпилировать программу, возникают ошибки (gcc компилятор)
Код
C:\Users\D36B~1\AppData\Local\Temp\ccBeoZYs.o:main.cpp:(.text+0x25): undefined reference to `Ocean::initalizer()'
C:\Users\D36B~1\AppData\Local\Temp\ccBeoZYs.o:main.cpp:(.text+0x30): undefined reference to `Ocean::run()'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\D36B~1\AppData\Local\Temp\ccBeoZYs.o: bad reloc address 0x20 in section `.eh_fram
e'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
Помогите исправить, пожалуйста)
Cell.h
Кликните здесь для просмотра всего текста
C++ |
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
| //Класс Cell
#include "Cell.h"
#include <stream.h>
#include <iostream>
using namespace std;
//методы поиска соседей
Cell* Cell::getCellat(Coordinate aCoord)
{
return Ocean1->cells[aCoord.getY()][aCoord.getX()];
}
void Cell::assignCellAt(Coordinate aCoord, Cell* aCell)
{
Ocean1->cells[aCoord.getY()][aCoord.getX()]=aCell;
}
Cell* Cell::getneighborWidthImage(char anImage)
{
Cell* neighbors[4];
unsigned count=0;
if (north()->getImage()==anImage) neighbors [count++]=north();
if (south()->getImage()==anImage) neighbors [count++]=south();
if (east()->getImage()==anImage) neighbors [count++]=east();
if (west()->getImage()==anImage) neighbors [count++]=west();
if (count==0)
return this;
else
return neighbors[Ocean1->random.nextIntBetween(0,count-1)];
Coordinate Cell::getEmptyNeighborCoord(void)
{
return getneighborWidthImage (DefaultImage)->getOffset();
}
Coordinate Cell::getPreyNeighborCoord (void)
{
return getNeighborWidthImage(DefaultPreyImage)->getOffset();
}
Cell* Cell::north(void)
{
unsigned yvalue;
yvalue=(offset.getY()+1)%Ocean1->numrows;
return Ocean1->cells[yvalue][offset.getX()];
}
Cell* Cell:east(void)
{
unsigned xvalue;
xvalue=(offset.getX()+1)%Ocean1->numCols;
return Ocean1->cells[offset.getY()][xvalue];
}
Cell* Cell:west(void)
{
unsigned xvalue;
xvalue=(offset.getX()>0)?(offset.getX()-1):(Ocean1->numCols-1);;
return Ocean1->cells[offset.getY()][xvalue];
}
//методы обработки и отображения
Cell* Cell::reproduce(Coordinate anOffset)
{
Cell* temp= new Cell(anOffset);
return temp;
}
void Cell::display(void)
{
cout<<form("%c",Image);
} |
|
Cell.h
Кликните здесь для просмотра всего текста
C++ |
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
35
36
37
38
39
40
41
42
43
44
| // Cell.h
#ifndef CellDefined
#define CellDefined
#include "Ocean.h"
class Cell
{
friend class Ocean;
protected:
static Ocean *Ocean1;
Coordinate offset;
char Image; //изображение в виде символа
//Метод поиска соседей
Cell* getCellAt (Coodinate aCoord);
void assignCellAt (Coordinate aCoord, Cell* aCell);
Cell* getNeighborWidthImage (char anImage);
Coordinate getEmptyNeighborCoord (void);
Coordinate getPreyNeighborCoord(void);
Cell* north(void);
Cell* south(void);
Cell* east(void);
Cell* west(void);
//методы обработки и отображения
virtual Cell* reproduce (Coordinate anOffset);
public:
Cell(Coordinate& aCoord): offset( aCoord) {image=DefaultImage;}//!!!!!!!!
Cell (void){}
virtual ~Cell(void){}
//Методы доступа
Coordinate& getOffset(void) {return offset;}
void setOffset(Coordinate& anOffset) {offset = anOffset;}
char getImage(void){return image;}
//Методы обработки изображения
void display(void);
virtual void process (void){} //do nothing
};
#endif |
|
Constants.h
Кликните здесь для просмотра всего текста
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| #define MaxRows 25
#define MaxCols 70
#define DefaultNumObstacles 75
#define DefaultNumPredators 20
#define DefaultNumPrey 150
#define DefaultNumiteratios 1000
#define DefaultImage ' '
#define DefaultPreyImage 'f'
#define DefaultPredImage 'S'
#define ObstacleImage '#'
#define TimeToFeed 6
#define TimeToReproduce 6 |
|
Coordinate.h
Кликните здесь для просмотра всего текста
C++ |
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
35
36
37
38
39
40
41
42
43
| // Класс Coordinate.h - координаты
#ifndef CoordinateDefined
#define CoordinateDefined
class Coordinate
{
private:
unsigned x;
unsigned y;
public:
Coordinate (unsigned aX, unsigned aY): x(aX),y(aY) {}
Coordinate (void) {x=0; y=0;}
Coordinate (Coordinate& aCoord)
{
x=aCoord.x;
y=aCoord.y;
}
~Coordinate (void){}
//методы доступа
unsigned getX(void){return x;}
unsigned getY(void){return y;}
void setX (unsigned aX){x=aX;}
void setY (unsigned aY){y=aY;}
//методы присвоения и сравнения
void operator = (Coordinate& aCoord)
{
x=aCoord.x;
y=aCoord.y;
}
int operator == (Coordinate& c)
{
return(x==c.x&&y==c.y);
}
int operator != (Coordinate& c)
{
return(x!=c.x||y!=c.y);
}
};
#endif |
|
Obstacle.h
Кликните здесь для просмотра всего текста
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| //Obstacle.h
//Унаследованный от Cell
#ifndef ObstacleDefined
#define ObstacleDefined
#include "Predator.h"
class Obstacle: public Cell
{
public:
Obstacle (Coordinate& aCoord): Cell(aCoord)
{
image=ObstacleImage;
}
virtual ~Obstacle(void){}
};
#endif |
|
main.cpp
Кликните здесь для просмотра всего текста
C++ |
1
2
3
4
5
6
7
| #include "Ocean.h"
main()
{
Ocean* myOcean=new Ocean;
myOcean->Ocean::initalizer();
myOcean->Ocean::run();
} |
|
Ocean.cpp
Кликните здесь для просмотра всего текста
C++ |
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
| //Класс ocean
#include <stream.h>
#include "Obstacle.h"
#include "Cell.h"
#include "Constants.h"
#include "Predator.h"
#include "Prey.h"
#include "Random.h"
#include <iostream>
using namespace std;
//методы инициализации
void Ocean::initalizer(void)
{
random.initialize();
numRows=MaxRows;
numCols=MaxCols;
size=numRows*numCols;
numObstacles = DefaultNumObstacles;
numPredators = DefaultNumPredators;
numPrey= DefaultNumPrey;
initCells();
}
void Ocean::initCells(void)
{
addEmptyCells();
cout<< "\n\n Enter number of obstacles(default=75):";
cout.flush();
cin>> numObstacles;
if (numObstacles >=size) numObstacles=size;
cout <<"\n Number obstacles accepted="<<numObstacles;
cout.flush();
cout<<"\n\n Enter number of predators(default=20): ";
cout.flush();
cin >>numPredators;
if (numPredators>=(size - numObstacles))
numPredators=size-NumObstacles;
cout<<"\n Number predators accepted =" <<numPredators;
cout.flush();
cout<<"\n\n Enter number of prey(default=150):";
cout.flush();
cin>>numPrey;
if (numPrey >=(size - numObstacles-numPredators))
numPrey=size - numObstacles-numPredators;
cout <<"\n Number prey accepted="<<numPrey<<"\n\n";
cout.flush();
addObstacles();
addPredators();
addPrey;
displayStats(-1);
displayCells();
displayBorder();
Cell::Ocean1=this;//прикрепляем океан ко всем ячейкам
}
void Ocean::addEmptyCells(void)
{
for (unsigned row=0;row<numRows;row++)
for(unsigned col= 0; col<numCols;Coll++)
cells[row][col]=new Cell(Coordinate(col,row));
}
void Ocean::addObstacles(void)
{
Coordinate empty;
for (unsigned count=0; count<numObstacles;count++)
{
empty=getEmptyCellCoord();
cells[empty.getY()][empty.getX()]= new Obstacle(empty);
}
}
void Ocean::addPredators(void)
{
Coordinate empty;
for (unsigned count=0;count<numPredators;count++)
{
empty=getEmptyCellCoord();
cells[empty.getY()][empty.getX()]= new Predator(empty);
}
}
void Ocean::addPrey(void)
{
Coordinate empty;
for (unsigned count =0;count<numPrey;count++)
{
empty=getEmptyCellCoord();
cells[empty.getY()][empty.getX()]= new Prey(empty);
}
}
Coordinate Ocean::getEmptyCellCoord(void)
{
unsigned x,y;
Coordinate empty;
do
{
x=random.nextIntBetween(0,numCols-1);
y=random.NextIntBetween(0,numRows-1);
}
while (cells[y][x]->getImage()!=DefaultImage);
empty=cells[y][x]->getOffset();
delete cells[y][x];
return empty;
}
//методы отображения
void Ocean::displayBorder(void)
{
for(unsigned col =0;col<numCols;col++)
cout<<"*";
cout<<"\n";// горизонтальная граница
}
void Ocean::displayCells(void)
{
for(unsigned row=0;row<numrows;row++)
{
for(unsigned col=0;col<numCols;col++)
cells[row][col]->display();
cout<<"\n";
}
}
void Ocean::displayStats(int iteration)
{
cout<<"\n\n";
cout<<"Iteration number:"<<++iteration;
cout<<"Obstacles:"<<numObstacles;
cout<<"Predators:"<<numPredators;
cout<<"Prey:"<<numPrey<<"\n";
cout.flush();
displayBorder();
}
//метод запуска процессу моделирования
void Ocean::run(void)
{
unsigned numIterations=DefaultNumIterations;
cout<<"\n\nEnter number of iterations (default max =1000):";
cout.flush();
cin>>numIterations;
if (numIterations >1000) numIterations=1000;
cout<<"\nNumber iterations="<<numIterations<<"\nbegin run...\n";
cout.flush();
for(unsigned iteration =0;iteration<numIterations;iteration++)
{
if (numPredators>0 && numPrey>0)
{
for (unsigned row=0;row<numRows;row++)
for (unsigned col=0;col<numCols;col++)
cells[row]col]->process();
displatStats(iteration);
displayCells();
displayBorder();
cout.flush();
}
}
cout<<"\n\nend of Simulation\n";
cout.flush();
} |
|
Ocean.h
Кликните здесь для просмотра всего текста
C++ |
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
| // ocean.h
#ifndef OceanDefined
#define OceanDefined
#include "Constants.h"
#include "Random.h"
#include "Coordinate.h"
class Cell;
class Ocean
{
friend class Cell;
private:
unsigned NumRows;
unsigned numCols;
unsigned size; //numRows*numCols
unsigned numPrey;
unsigned numPredators;
unsigned numObstacles;
Random random;
Cell* cells[MaxRows][MaxCols];
//методы инициализации
void initCells (void);
void addEmptyCells(void);
void addObstacles (void);
void addPredators (void);
void addPrey(void);
Coordinate getEmptyCellCoord(void);
//методы отображения
void displayBorder(void);
void displayCells(void);
void displayStats( int iteration);
public:
// Нет конструкторов, потому что его объекты - статич. переменная
//методы доступа
unsigned getNumPrey(void) {return numPrey;}
unsigned getNumPredators (void) {return numPredators;}
void setNumPrey (unsigned aNumber) {numPrey = aNumber;}
void setNumPredators (unsigned aNumber) {numPredators=aNumber;}
//метод инициализации
void initalizer (void);
//метод запуска процесса моделирования
void run (void);
};
#endif |
|
Predator.cpp
Кликните здесь для просмотра всего текста
C++ |
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
| //Класс Predator
#include "Predator.h"
//методы обработки и отображения
void Predator::process(void)
{
Coordinate toCoord;
if (--timeToFeed<=0) //хищник умирает
{
assignCellAt(offset, new Cell(offset));
Ocean1->setNumPredators(Ocean1->getNumPredators()-1);
delete this;
}
else //съедает соседнюю добычу (если возможно)
{
toCoord=getPreyNeighborCoord();
if (toCoord!=offset)
{
Ocean1->setNumPrey(Ocean->getNumPrey()-1);
timeToFeed=TimeToFeed;
moveFrom(offset,toCoord);
}
else
Prey::process();//перемещается в пустую ячейку (если возможно)
}
}
Cell* Predator::reproduce(Coordinate anOffset)
{
Predator* temp=new Predator(anOffset);
Ocean1->setNumPredators(Ocean1->getNumPredators());
return (Cell*)temp;
} |
|
Predator.h
Кликните здесь для просмотра всего текста
C++ |
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
| // Predator.h
// производный от Cell
#ifndef PredatorDefined
#define PredatorDefined
#include "Prey.h"
class Predator: public Prey
{
private:
//Методы обработки и отображения
virtual Cell* reproduce(Coordinate anOffset);
protected:
unsigned timeToFeed;
public:
Predator (Coordinate aCoord): Prey(aCoord)
{
timeToFeed=TimeToFeed;
Image= DefaultPredImage;
}
virtual ~Predator (void){}
//Методы обработки и отображения
virtual void process(void);
};
#endif |
|
Prey.cpp
Кликните здесь для просмотра всего текста
C++ |
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
| //Класс Prey
#include "Prey.h"
//методы обработки и отображения
void Prey::moveFrom( Coordinate from, Coordinate to)
{
Cell* toCell;
--timetoReproduce;
if(to!=from)
{
toCell=getCellAt(to);
delete toCell;
setOffset(to);
assignCellAt(to,this);
if(timetoReproduce<=0)
{
timeToReproduce=TimeToReproduce;
assignCellAt(from,reproduce(from));
}
else
assignCellAt(from,new Cell(from));
}
}
Cell* Prey::reproduce(Coordinate anOffset)
{
Prey* temp= new Pray(anOffset);
Ocean1->setNumPrey(Ocean1->getNumPrey()+1);
return (Cell*)remp;
} |
|
Prey.h
Кликните здесь для просмотра всего текста
C++ |
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
| // Prey.h
#ifndef PreyDefined
#define PreyDefined
#include "Cell.h"
class Prey: public Cell
{
protected:
int timeToReproduce;
//Методы обработки и отображения
void moveFrom (Coordinate from, Coordinate to);
virtual Cell* reproduce (Coordinate anOffset);
public:
Prey( Coordinate& aCoord):Cell(aCoord)
{
timeToReproduce=TimeToReproduce;
Image=DefaultPreyImage;
}
virtual ~Prey(void){}
//Методы обработки и отображения
virtual void process(void)
{
Coordinate toCoord;
toCoord=getEmptyNeighborCoord();
moveFrom (offset, toCoord);
}
};
#endif |
|
Random.cpp
Кликните здесь для просмотра всего текста
C++ |
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
35
| //Класс Random
#include "Random.h"
#difine MAX 32767
//методы доступа
float Random::randReal(void)
{
int c;
static first=1;
//Этот код выполняется один раз
if (first)
{
seed1*=2;
seed2*=2;
if (seed1>MAX)seed1=MAX
if (seed2>MAX)seed2=MAX
//возбуждение генератора
for (int index=1;index<=30;index++)
randReal();
}
unsigned Random::nextIntBetween(int low, int high)
{
float r,t;
int c;
r=(float)high-(float)low+1.0;
t=r*randReal();
c=(int)t;
return (low+c);
} |
|
Random.h
Кликните здесь для просмотра всего текста
C++ |
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
| // случайны числа из заданного промежутка или равномерно распределенные
// действительные числа их промежутка 0,0 и 1,0
#ifndef RandomDefined
#define RandomDefined
class Random
{
private:
int seed1,seed2;
public:
//методы инициализации
void initialize(void)
{
seed1=3797;
seed2=2117;
}
void init (int s1, int s2)
{
seed1=s1;
seed2=s2;
}
//методы доступа
float randReal(void);
unsigned nextIntBetween (int low, int high);
};
#endif |
|