0

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);
}
Fatcow808
  • 11
  • 2
  • `addMessage` expects a `char*`? I would use C++ streams rather than C-io. – 463035818_is_not_an_ai Apr 20 '20 at 09:32
  • `sprintf_s()` is not a C++ function. It's from an optional part of of C11, that no compiler/libc vendors outside of Microsoft bothered to implement (And MS's version doesn't even fully follow the standard). It shouldn't be used. – Shawn Apr 20 '20 at 09:52
  • Does this answer your question? [How to properly replace sprintf\_s by sprintf in C++03?](https://stackoverflow.com/questions/54238576/how-to-properly-replace-sprintf-s-by-sprintf-in-c03) – Botje Apr 20 '20 at 09:58
  • @idclev463035818 unfortunately, this is a working solution that will only serve as the base project for me to start on my current assignment, so I'm reluctant to improve it and just need it to compile. – Fatcow808 Apr 21 '20 at 05:39
  • @Shawn So you are saying its not good practice right? I will keep that in mind. It was my teacher who decided to use sprintf_s() in their solution. – Fatcow808 Apr 21 '20 at 05:43
  • @Botje no, I am not trying to replace it with sprintf() nor vsnprintf() – Fatcow808 Apr 21 '20 at 05:46
  • Your title says "how do I get it to compile", and the answer is "by providing an implementation of `sprintf_s`, either as a function or a macro that calls snprintf." – Botje Apr 21 '20 at 06:14
  • Yeah, not good practice. MSVC will tell you to use their `_s` functions over the usual ones unless you `#define` a particular symbol. It's just them trying to lock you into their product. – Shawn Apr 21 '20 at 06:45
  • no this isnt a "working solution". Even if this is code your teacher gave you should talk to him and convince him to fix it. – 463035818_is_not_an_ai Apr 21 '20 at 08:00

1 Answers1

2

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]);
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • Thank you for the clarification! The examples I looked at had manually set the number of bytes and was less than max buffer size, so I didn't even think about using sizeof(). Do you happen to know why my teachers solution works for him despite missing buffer size argument? Does it depend on the compilers or IDE used? – Fatcow808 Apr 21 '20 at 05:31
  • @Fatcow808 Your teacher probably used insecure `sprintf` rather than `snprintf`. – Maxim Egorushkin Apr 21 '20 at 07:49