Undefined reference error: id returned 1 exit status

When I try to compile my code by typing 'make' I get the error:

1
2
3
4
5
Marathon.cc:(.text+0x3e1): undefined reference to `View::validPos(Position&)'
Marathon.cc:(.text+0x46b): undefined reference to `View::update(Position&, Position&, char)'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'p1' failed
make: *** [p1] Error 1


I've double checked my capitals, spelling, and I included the View.h header file in Marathon.h. I don't believe there is anything wrong with my makefile. I was wondering if anyone could point out why I keep getting this error?

Marathon.cc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
using namespace std;
#include <string>
#include "Marathon.h"


void Marathon::run(){
	view.print();
	
	int i;
	for(i = 0; i < MAX_RUNNERS; ++i){
		Position pos;
		participants.get(i)->update(pos);
		if(view.validPos(pos)){
			Position oldPos = participants.get(i)->getCurrPos();
			participants.get(i)->setCurrPos(pos);
			view.update(oldPos, pos, participants.get(i)->getAvatar());
		}
	}
}


Marathon.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef MARATHON_H
#define MARATHON_H

#include "View.h"
#include "Position.h"
#include "Array.h"

class Marathon{
	public:
		Marathon();
		void run();
	private:
		View view;
		Array participants;
};

#endif


View.cc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
#include <string>

#include "View.h"


void update(Position& oldPos, Position& newPos, char avatar){
	oldPos.setRow(' ');
	oldPos.setColumn(' ');
	
	newPos.setRow(avatar);
	newPos.setColumn(avatar);
}

bool validPos(Position& pos){
	if((pos.getColumn() <= MAX_COLUMNS) && (pos.getRow() <= MAX_ROWS)){
		return true;
	}
	return false;
}


Makefile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
p1:	main.o View.o Marathon.o
	g++ -o a3 main.o View.o Marathon.o

main.o:	main.cc Marathon.h
	g++ -c main.cc

View.o:	View.cc View.h Position.h
	g++ -c View.cc
	
Marathon.o:	Marathon.cc Marathon.h View.h Position.h Array.h 
	g++ -c Marathon.cc
	

clean:
	rm -f *.o p1


Just a guess (I didn't try making an equivalent setup myself), but since Marathon.o depends on View.o, put Marathon.o before View.o in your command.
Didn't seem to fix it :(
Oh, I get it now.

You aren't putting ClassName:: before the member function in the View.cc implementation file.
So it's defining update and validPos as standalone functions instead of as class members.

And the compiler doesn't catch this because you aren't using any member variables or member functions within either of these functions (so they could just be non-class functions, but that's up to you).
Last edited on
Topic archived. No new replies allowed.