Using the latest versions of Visual Studio 2019 and Visual Studio. In both I am trying to use this header-only class to display a vector:
#pragma once
#include <cstring>
#include <iterator>
#include <vector>
// Header-only helper class for using strings
class stringHelper
{
public:
// Constructor / destructor
stringHelper() {}
~stringHelper() {}
template <class T>
void PrintVector(std::vector<T> input, const char cSeparator = ' ', bool bNewLine = false)
{
// Output an array (std::vector) to the console eg. {1,11,21} => 1 11 21.
// Example usage: stringHelper::PrintVector<int>(vecOfInts);
if (input.size() > 0)
{
for (std::vector<T>::iterator it = input.begin(); it != input.end();)
{
std::cout << *it;
if (++it != input.end())
std::cout << cSeparator;
}
if (bNewLine)
std::cout << std::endl;
}
}
};
Visual Studio Code has been configured to use the cl.exe
compiler from Visual Studio 2019 as follows:
{
// Use MSVC cl.exe (eg. Visual Studio 2019) on Windows
"type": "shell",
"label": "cl.exe: Build active file (DEBUG)",
"command": "cl.exe",
"args": [
"/std:c++17",
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
}
}
A simple test program might be:
#include <vector>
#include "stringHelper.h"
int main()
{
// Create and print out a vector
stringHelper helper;
std::vector<int> vec = {1, 2, 17};
helper.PrintVector<int>(vec); // Expecting: 1 2 17
return 0;
}
In Visual Studio Code, the program compiles and produces the correct result. The Visual Studio project is configured to use C++17 (I tried C++14 as well). When I compile in Visual Studio 2019 Professional, multiple C2760
compile errors are generated:
Error C2760 syntax error: unexpected token 'identifier', expected ';'
When I select this line in the Error List
, the token it
is highlighted.
Change the internal loop in PrintVector
to the following, which is subtly different and not what I need. Now everything compiles and works fine in both Visual Studio Code and 2019:
for (auto a : input)
{
std::cout << a << cSeparator;
}
The Microsoft help on C2760 does not appear to apply. Is this a compiler or VS 2019 bug? Why is this failing in Visual Studio 2019 but not Visual Studio Code?
question from:
https://stackoverflow.com/questions/65647489/compilation-error-c2760-in-visual-studio-2019-but-not-visual-studio-code