我试图在 .cpp 文件中实现
setColor()
的定义,该定义之前已在 Sphere 类的 .h 文件中正确编译,但现在我收到下一个链接器错误:
Starting build...
cmd /c chcp 65001>nul && g++.exe -std=c++17 -g -I include -L lib src\* -o bin\helloworld.exe -lfreeglut -lopengl32 -lglu32
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\sdelgado\AppData\Local\Temp\ccIOwL6S.o: in function `main':
C:\msys64\home\sdelgado\helloworld/src/main.cpp:55:(.text+0x1b3): undefined reference to `Sphere::setColor(unsigned char, unsigned char, unsigned char)'
collect2.exe: error: ld returned 1 exit status
球体.h
class Sphere
{
public:
float radio;
float x;
float y;
unsigned char red;
unsigned char green;
unsigned char blue;
void setColor( unsigned char r, unsigned char g, unsigned char b);
};
球体.cpp
#include "sphere.h"
void Sphere::setColor(unsigned char r,unsigned char g, unsigned char b)
{
red=r;
green=g;
blue=b;
}
main.cpp
#include "sphere.h"
Sphere sphere;
main()
{
[...]
sphere.setColor(200,0,0);
}
是否有必要添加一些我跳过的其他内容?
有必要包含
src
文件夹中的每个 .h 和 .cpp 文件。