Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

c++ - Compilation error C2760 in Visual Studio 2019 but not Visual Studio Code

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This problem was reported to Microsoft who responded (in under 48 hours!) with an explanation of the underlying issue. This is not a compiler bug, but relates to type deduction when using templates. Read the full discussion here.

One of the comments provided a useful link to this StackOverflow answer with similar details.

Executive summary

  • std::vector<T>::iterator is a dependent name which creates ambiguity issues
  • Resolve by specifying that std::vector<T>::iterator is a type or use auto (code examples below)
  • Issue is detected by cl.exe when using the /permissive- option. This is enabled for Visual Studio 2019 but not (by default) for Visual Studio Code or the Visual Studio 2019 Developer Command Prompt.

To specify as a type:

typename std::vector<T>::iterator it = input.begin();
for (; it != input.end();)

To use auto:

for (auto it = input.begin(); it != input.end();)

Both solutions have been tested and resolve the compile problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...