c++ - 已声明但未定义的静态方法错误 C++

标签 c++

我在类中有一个静态方法,在文件 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/

相关文章:

c++ - 指针问题 "cannot instantiate abstract class"

c++ - 使用一个 '\n' 打印

c++ - 将函数名称从 C++ Dll 导出到 Delphi

c++ - 在我的资源类中将通过 loadFromFile() 加载字体的函数更改为 loadFromMemory()

c++ - SFML平台碰撞

c++ - Tilemap 碰撞 SFML C++

c++ - 在检查 while 循环时不匹配 'operator!='

c++ - 如何在C++中使用无锁循环缓冲区实现零拷贝tcp

c++ - 移动构造函数提示 "non-class type"

c++ - 将 boost::bind 与包含 boost::mutex 的类一起使用