我試圖編譯使用繼承一個簡單的程序,我遇到了這一點:未定義的引用與繼承,生成文件的問題?
g++ -g -Wall -c q1Test.cpp
g++ -g -Wall -c figure.cpp
g++ -g -Wall -c rectangle.cpp
g++ -g -Wall -c triangle.cpp
g++ -g -Wall -o shapes q1Test.o figure.o rectangle.o triangle.o
q1Test.o: In function `main':
/home/aryner/school/fall2013/csc340/hw9/Q1/q1Test.cpp:11: undefined reference to `Figure::center()'
/home/aryner/school/fall2013/csc340/hw9/Q1/q1Test.cpp:16: undefined reference to `Figure::center()'
collect2: ld returned 1 exit status
make: *** [shapes] Error 1
搜索這個錯誤使我相信,我有我的makefile文件有問題,但我完全失去了作爲什麼問題可能是。我將我的makefile與我在網上找到的一些作比較,我不明白爲什麼我的工作不應該。這裏是我使用Makefile:
CC = g++
CFLAGS = -g -Wall
default: shapes
shapes: q1Test.o figure.o rectangle.o triangle.o
$(CC) $(CFLAGS) -o shapes q1Test.o figure.o rectangle.o triangle.o
figure.o: figure.cpp figure.h
$(CC) $(CFLAGS) -c figure.cpp
rectangle.o: rectangle.cpp rectangle.h
$(CC) $(CFLAGS) -c rectangle.cpp
triangle.o: triangle.cpp triangle.h
$(CC) $(CFLAGS) -c triangle.cpp
q1Test.o: q1Test.cpp figure.h rectangle.h triangle.h
$(CC) $(CFLAGS) -c q1Test.cpp
clean:
$(RM) shapes *.o *~
我不想發佈的源文件,因爲這是一個家庭作業,但是我會給出一個簡要的說明。圖是矩形和三角形的超類。矩形和三角形應該是圖中的繼承中心()。 q1Test是一個測試程序,用於測試三角形和矩形中的所有函數,center是唯一經過測試的函數,它是從figure繼承的,並且不會超載。
只是爲了快速檢查,嘗試在'shapes:'規則中的'figure.o'之後移動'q1Test.o'。 – slugonamission
另外,完整性檢查(儘管違反了這個可能會導致另一個警告......),'center()'*確實在'Figure'中有一個實現嗎? – slugonamission