我在类中有一个静态方法,在文件 Convert.h
中如下所示class Convert
{
public :
static string convertIntToStr(unsigned int integer);
};
在Convert.cpp
string
Convert::convertIntToStr(unsigned int integer)
{
ostringstream ostr;
ostr << integer;
return ostr.str();
}
我在另一个 .cpp 文件的其他一些类方法中使用它作为 Convert::convertIntToStr
,但是我得到链接错误,它说 undefined reference Convert::convertIntToStr(unsigned int )
。你能告诉我哪里出了问题吗?
最佳答案
对于多个cpp 文件,您必须将已编译的目标文件链接到可执行文件。在像 eclipse CDT 或 Visual stdio 这样的 IDE 中,它已经为您完成了。
自己编译链接,以gcc为例,编写Makefile
:
CC=g++
CPPFLAGS=-fPIC -Wall -g -O2
all:executable
executable: convert.o other.o
$(CC) $(CPPFLAGS) -o $@ $^
convert.o: convert.cpp
$(RC) $^
other.o: other.cpp
$(CC) -o $@ -c $^
.PHONY:clean
clean:
rm *.o executable
关于c++ - 已声明但未定义的静态方法错误 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17264151/