未定义引用继承,makefile问题?

时间:2013-12-09 18:43:12

标签: c++ inheritance makefile

我正在尝试编译一个使用继承的简单程序,我遇到了这个:

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 *~

我不想发布源文件,因为这是一个家庭作业,但我会简要介绍一下。图是矩形和三角形的超类。矩形和三角形应该从图中继承center()。 q1Test是一个测试程序,它测试三角形和矩形中的所有函数,center是唯一一个从图中继承并且没有重载的测试函数。

1 个答案:

答案 0 :(得分:1)

两种最有可能的情况是您忘记在源文件中定义Figure::center(),或者您不小心忘记将其限定为Figure::,因此您将获得全局center个功能不在课堂上的。