未找到标识符convertToHSL

Identifier not found convertToHSL

本文关键字:convertToHSL 标识符      更新时间:2023-10-16

当我在主方法中调用convertToHSL(c1)时,我得到一个未找到的错误标识符。我不明白我的代码的问题是什么。请帮助。我的代码如下:

#include "stdafx.h"
#include "q3.h"
#include <cmath>
#include <iostream>
#include <math.h>
using namespace std; 
int main(int argc, char* argv[])
{
    Color c1(1,1,1);
    HSL h=convertToHSL(c1);
    getchar();
    getchar();
    return 0;
}
Color::Color(){}
Color::Color(float r,float g,float b){
    this->r=r;
    this->g=g;
    this->b=b;
}
Color::~Color(void){}
Color Color::operator+(Color c) {
    return Color(r*c.r,g*c.g,b*c.b);
}
Color Color::operator*(float s) {
    return Color(s*r,s*g,s*b);
}
HSL::HSL() {}
HSL::HSL(float h,float s,float l) {
    this->h=h;
    this->s=s;
    this->l=l;
}
HSL::~HSL(void){}


HSL convertToHSL(Color const& c) {
    return HSL(0,0,0);

如果没有声明convertToHSL,则在main()上是未知的:将main()替换为所有其他函数下面的文件末尾

在调用convertToHSL时(在main中),编译器根本不知道它的存在,因为它还没有"看到"它(它还没有被声明)。

因此,为了能够从main调用这个函数,要么将convertHSL的定义移到main之上,要么至少在main之上预先声明它(不定义它)。或者,如果也要从其他文件中使用它,则将其声明放入头文件中(其定义可能放入单独的源文件中,或者使用inline说明符直接放入头文件中)。