0

So I have written a program in the Code Editor window and when checking it, it all makes sense. There are no errors (the red squigly line) and the warnings/advice that are there I understand.

However then I go to build the code and run it, it will not run because it finds errors. 29 of them at the time of writing. It shows them in the Output Window. The worst part is that the Code Editor itself does not display the corresponding errors. The Output Window will say at line 13 is an error, but I'll go there and the Code Editor says it's all fine and there is no error. So which one is correct?

A few examples: I have a vector class of

vector<Point>points;

and the Editor says it's fine, however the Output windows is saying "error: missing ';' before '<'" so on paper it wants me to fix it to

vector;<Point>points;

But that is clearly wrong and then the Editor also highlights it as wrong. Other errors also don't make sense. I have a function called

string toString(){}

And the Output windows will again, say "error: missing '('" however there isn't the function is complete with no missing bits.

How do I get the Code Editor and Output Window to work together and display errors that are actually there, because right now they're both at conflict with each other.

Artyu
  • 19
  • 1
  • 2
  • 7
  • 2
    Please show a [mre]. Check if you got all includes, and don't forget the `std::` or `using namespace std;` (but note that the latter [is considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). Also the compiler is usually right and intellisense might have some hickups. Lastly check the very first error you find, often missing braces or semicolons causes errors further down the document. You also might want to compile your code in another compiler and hope for a more precise error message. – Lukas-T Jul 30 '20 at 16:01
  • Intellisense is not always correct. – drescherjm Jul 30 '20 at 16:51
  • `error: missing ';' before '<'"` probably means you forgot to include vector or perhaps you put `#include ` above `#include "pch.h"` and don't know that every line above `#include "pch.h"` is silently ignored by the compiler because this is how precompiled headers work on Visual Studio. – drescherjm Jul 30 '20 at 16:53
  • What does "pch.h" stand for? I have #include below my headers in the appropriate .h files. – Artyu Jul 30 '20 at 17:22
  • If you don't know you must have disabled Precompiled Headers in your Visual Studio project settings. Usually when you create a new console project in Visual Studio the IDE creates one with precompiled headers enabled. Related to pch.h on Visual Studio: [https://stackoverflow.com/questions/54121917/what-is-pch-h-and-why-is-it-needed-to-be-included-as-the-first-header-file](https://stackoverflow.com/questions/54121917/what-is-pch-h-and-why-is-it-needed-to-be-included-as-the-first-header-file) – drescherjm Jul 30 '20 at 17:39
  • ***How do I get the Code Editor and Output Window to work together and display errors that are actually there, because right now they're both at conflict with each other.*** There are 2 different compilers at work. The `red squiggles` are generated by one partial compiler that is built for speed but not always accuracy. The other compiler actually builds your code. – drescherjm Jul 30 '20 at 18:20
  • Ok, I added std::vectorpoints; to the whole line of code and about half of the errors went away. I guess C++ didn't know what vectors were. Didn't know they were referenced by the STD? – Artyu Jul 30 '20 at 20:04
  • ***Didn't know they were referenced by the STD?*** Yes vector is in the std namespace. [https://en.cppreference.com/w/cpp/container/vector](https://en.cppreference.com/w/cpp/container/vector) – drescherjm Jul 30 '20 at 20:32
  • @drescherjm VS disables precompiled headers by default for console apps now. – eesiraed Jul 31 '20 at 00:23
  • I admit I have used CMake for so long, I don't often create a project in the IDE directly. – drescherjm Jul 31 '20 at 02:06
  • 1
    _" I guess C++ didn't know what vectors were"_ - no, namespaces don't get added automatically (that would defy the concept of namespaces). You have to be explicit about namespaces as Barnets answer suggests. – Lukas-T Jul 31 '20 at 04:48

1 Answers1

2
  1. About There are no errors (the red squigly line), I suggest that you could check if your VS setting Tools->Options->Text Editor->C/C++->Advanced->Disable IntelliSense is False.

  2. All C++ standard library types and functions are declared in the std namespace or namespaces nested inside std. So, if you use functions like vector, you could add std::vector. I suggest that you could use std::function rather than using namespace std; Also, you could refer to Microsoft Docs about namespace.

Barrnet Chou
  • 1,738
  • 1
  • 4
  • 7