Xcode使用未声明的标识符' func'

时间:2017-03-10 07:16:58

标签: c++ xcode macos

我一直收到同样的错误

  

使用未声明的标识符' GetTextSize'

这是我的代码:

Vector2D nameSize = GetTextSize(string, testfont); // This is where I get the error
Vector2D GetTextSize(const char* text, HFONT font)
{
    std::wstring wc = StringToWstring(text);

    int x_res, y_res;
    GetTextSize(font, wc.c_str(), x_res, y_res); 
    // ^^^ This is a different func with the same name
    return Vector2D(x_res, y_res);
}

1 个答案:

答案 0 :(得分:0)

您应该在使用之前定义/声明GetTextSize()

换句话说,确保GetTextSize()的定义/声明在调用之前。

这有两个解决方案:

// Define the function first
Vector2D GetTextSize(const char* text, HFONT font)
{
    std::wstring wc = StringToWstring(text);

    int x_res, y_res;
    GetTextSize(font, wc.c_str(), x_res, y_res); 
    // ^^^ This is a different func with the same name
    return Vector2D(x_res, y_res);
}

// Then you can use it
Vector2D nameSize = GetTextSize(string, testfont);

或者:

// Declare the function first
Vector2D GetTextSize(const char* text, HFONT font);

// Then you can use it
Vector2D nameSize = GetTextSize(string, testfont); // This is where I get the error

// Defination of the function
Vector2D GetTextSize(const char* text, HFONT font)
{
    std::wstring wc = StringToWstring(text);

    int x_res, y_res;
    GetTextSize(font, wc.c_str(), x_res, y_res); 
    // ^^^ This is a different func with the same name
    return Vector2D(x_res, y_res);
}