简体   繁体   中英

Why does this call to sprintf_s() work, and how can I make this work on my computer?

I'm an inexperienced student and this was part of a posted solution to our C++ programming assignment. But it does not compile on my computer and I need help understanding why. The first error message I encountered was at sprintf_s() saying "identifier 'sprintf_s' is undeclared", which I think is because I am running linux and sprintf_s() is not a standard lib func. I have thus tried replacing it with snprintf() and the original error message disappeared (but please let me know if that was not right, my change is commented out above the original line).

After changing it, I then received the error message "argument of type 'const char *' is incompatible with parameter of type 'size_t'" and " 'float' incompatible with parameter of type 'const char *' ". I understand that the parameters for both sprintf_s() and snprintf() are (char *str, size_t size, const char *format, ...) so I would think that error makes sense since size was missing from the arguments being passed. But if there must be an argument for size, then would it be correct to set it as 128? And why would this work without that argument on my teacher's Windows machine for sprintf_s(), but not on Linux for snprintf()? Also, he is using Visual Studio, I am using Visual Studio Code.

The entire function is shown below. If you know of another reason why I might be experiencing problems compiling what should be a working solution, please let me know!

bool myNode::isAccessible()
{
    return isAccessible(x, y);
}
myNode::myNode(const float location[3])
{
    x = (location[0] > 0.0) ? (int)floor(location[0] / SCALE + 0.5f) : (int)ceil(location[0] / SCALE - 0.5f);
    y = (location[1] > 0.0) ? (int)floor(location[1] / SCALE + 0.5f) : (int)ceil(location[1] / SCALE - 0.5f);
    if (isAccessible(x, y)) return;
    int originalX = x, originalY = y;
    for (int a = -1; a <= 1; a++)
        for (int b = -1; b <= 1; b++) {
            if (a == 0 && b == 0) continue;
            x = originalX + a;
            y = originalY + b;
            if (isAccessible(x, y)) return;
        }
    char buffer[128];
    //snprintf(buffer, "***AStarNode: could not find any isAccessible node for (%f, %f, %f)***", location[0], location[1], location[2]);
    sprintf_s(buffer, "***AStarNode: could not find any isAccessible node for (%f, %f, %f)***", location[0], location[1], location[2]);
    controlPanel->addMessage(buffer);
}

Error "argument of type 'const char *' is incompatible with parameter of type 'size_t'" and the following errors come from missing buffer size argument to snprintf .

The correct call to snprintf is:

snprintf(buffer, sizeof buffer, "***AStarNode: could not find any isAccessible node for (%f, %f, %f)***", location[0], location[1], location[2]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM