函数定义在 .h 文件中编译,但不在 .cpp 文件中,使用 C++17 和 VS 代码

问题描述 投票:0回答:1

我试图在 .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); 
}

是否有必要添加一些我跳过的其他内容?

c++ visual-studio-code c++17 code-structure
1个回答
0
投票

有必要包含

src
文件夹中的每个 .h 和 .cpp 文件。

© www.soinside.com 2019 - 2024. All rights reserved.