Undefined reference to class::function

Hello! I've been working on a small project in my spare time, and I've run into a problem that I can't seem to figure out. It's a small game that uses a 2-dimensional vector of a "Tile" class. I created another class called "Mapmaker" that will populate the 2-dimensional vector based on input specified in a text file. I'm currently using g++ on cygwin.

The mapmaker header looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef MAPMAKER_H
#define MAPMAKER_H

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include "tile.h"

using namespace std;

class Mapmaker
{
public:
	Mapmaker();

	//Other
	vector<vector<Tile> > loadMap(int chnumber, int lvlnumber);
private:
	ifstream mapfile;
};

#endif 


And the relevant part of the cpp file is this:

1
2
3
4
5
6
7
8
9
vector<vector<Tile> > nextMap(xSize, vector<Tile>(ySize, Tile()));
for(int x = 0; x < xSize; x++)
{
	for(int y = 0; y < ySize; y++)
	{				
		int nextTile = mapfile.get();
		nextMap[x][y] = Tile(x,y,0,nextTile);
	}
}


I compile them both into mapmaker.o with the following command in a Makefile:

1
2
3
4
5
CC=g++
CFLAGS=-c
...
mapmaker.o: mapmaker.h mapmaker.cpp
	$(CC) mapmaker.h mapmaker.cpp $(CFLAGS)


I'm sure the code has some other problems, but I can work those out. However, when I try to use the command to make mapmaker.o, I get the following compiler errors:

mapmaker.o:mapmaker.cpp:(.text+0x2ef): Undefined reference to 'Tile::Tile()'
mapmaker.o:mapmaker.cpp:(.text+0x432): Undefined reference to 'Tile::Tile(int,int,int,int)'

Both of these are specified in the tile.h file, which I've included in mapmaker.h, so mapmaker.cpp should know what the functions are. I've tried including the tile.h and tile.cpp file in the compliation command, but it doesn't fix the problem. I can't think of what I'm missing here.

Thank you for your time!
You shouldn't get an undefined reference until you are linking the executable. What does that look like in your Makefile?
Where's the Tile code? Why isn't it in the build?
Last edited on
Sorry, I forgot to include the tile code! Here's the tile.h file.

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
#ifndef TILE_H
#define TILE_H

using namespace std;

class Tile
{
public:
	//Constructor
	Tile();
	Tile(int gXpos, int gYpos, int gHeight, int gTertype);

	//Selectors
	int getXpos();
	int getYpos();
	int getHeight();
	int getTertype();

	//Mutators
	void setX(int nextX);
	void setY(int nextY);
	void setHeight(int nextHeight);
	void setType(int nextType);

private:
	int xpos;
	int ypos;
	int height;
	int tertype;
};

#endif 


The Tile.cpp file is somewhat longer. I trimmed the selector and mutator functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Default constructor
Tile::Tile()
{
}

//NONDEFAULT constructor
Tile::Tile(int gXpos, int gYpos, int gHeight, int gTertype)
{
	xpos = gXpos;
	ypos = gYpos;
	height = gHeight;
	tertype = gTertype;
}
...
//Selector, mutator functions. 


I'm not linking the executable when it tries to make mapmaker.o, but the entire makefile looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CC=g++
CFLAGS=-c

all: game.exe

game: character.o scenario.o tile.o mapmaker.o main.cpp
	$(CC) -o game.exe character.o scenario.o mapmaker.o main.cpp

character.o: character.h character.cpp
	$(CC) character.h character.cpp $(CFLAGS)

scenario.o: scenario.h scenario.cpp
	$(CC) scenario.h scenario.cpp $(CFLAGS)

tile.o: tile.h tile.cpp
	$(CC) tile.h tile.cpp $(CFLAGS)

mapmaker.o: mapmaker.h mapmaker.cpp
	$(CC) mapmaker.h mapmaker.cpp $(CFLAGS)
You makefile: line 7 is missing tile.o.
Good catch! I totally didn't notice that. That seems to have fixed the problem. Thank you very much!
Topic archived. No new replies allowed.