identifier is undefined warning, for functions that already exist in #include

3

1

I have a c++ source code that compiles normally in Visual Studio 2017, but strangely, VS 2017 shows in red underlined the names of some functions that I created and left in a #include, accusing as undefined identifier.

Well, how it's "undefined" if the functions are there, and the program compiles without errors?

enter image description here

What am I missing?

Rogério Dec

Posted 2018-05-30T15:19:36.900

Reputation: 316

Do you happen to have the declaration of the method appearing after that particular call? I assume you have tried to close and reopen the project? – Ramhound – 2018-05-30T15:33:58.227

@Ramhound I wonder if "functions" actually means functions here. Pre-processor macros could also be the case and those can easily confuse the syntax-checker. – Tonny – 2018-05-30T15:42:44.257

@Tonny - I would also agree. It is not clear if the "identifier "explode" is undefined is a warning or an error in this case. If it was a syntax error then the code should be compiled. Without the entire file or the actual output of the compilation, I don't believe we can actually say the reason this error is being generated.of this error message. However, based on my nearly 2 decades worth of using Visual Studio to compile C++ code, I am going to guess there is a missing bracket somewhere int he code. – Ramhound – 2018-05-30T15:51:17.277

Answers

2

The syntax checker in the IDE is not 100% reliable. They are getting better and better, but 100% accuracy is not achievable, especially not for languages like C and C++ that allow pre-processor macros and the like.

Certain combinations of #include stuff, combined with name-spaces and macro-expansions can throw the syntax checker of, even though the code is actually correct.

An error (or just a warning) somewhere in the code or an include may also create spurious syntax-warnings further down in the source-file.

This is not only an issue with Visual Studio. It can happen in any IDE or editor that does syntax highlighting/checking. I have seen it happen in XCode, Eclipse and CodeBlocks too, just to name a few examples.

In general: If you encounter something like this, it is usually an indication that the source-code that defines the declaration (the include that defines explode() in your case) is possibly written in an unclear, ambiguous or even wrong way. Even though it compiles for your particular use of explode() in the source it may not do so for all possible use-cases.

You should consider re-working that code so it doesn't show the red line anymore. It will be confusing to another programmer that needs to work on the code later. And that other programmer can be you, if you need to re-visit the code a few months later and have forgotten all about this.

Tonny

Posted 2018-05-30T15:19:36.900

Reputation: 19 919

1

It's definitely a bug in VS 2017. To test, I removed the include function and put it in the body of the source code: hence the red line disappears. And most impressive: after reactivating the function back to include, and deleting it from the source code (ie everything exactly as it was before), VS no longer showed the red line, either for this function or for the other functions which before appeared with the same red line ...

And just out of curiosity, here's the function:

vector<string> explode(string linha, string delim) {
    vector<string> parte;
    size_t pos = 0,pos2 = 0;
    do {
        pos2 = linha.find(delim, pos);
        parte.push_back(linha.substr(pos, pos2-pos));
        pos = pos2+1;
    } while(pos2 != string::npos);
    return parte;
}

I do not think the problem is with my code ...

Rogério Dec

Posted 2018-05-30T15:19:36.900

Reputation: 316