编译在类中定义方法的单个文件时,来自 C++ 中 xtree 的 VS2019 C2675 和 C2100 错误

Posted

技术标签:

【中文标题】编译在类中定义方法的单个文件时,来自 C++ 中 xtree 的 VS2019 C2675 和 C2100 错误【英文标题】:VS2019 C2675 and C2100 error from xtree in c++ when compiling a single file that defines a method in a class 【发布时间】:2020-04-15 07:50:45 【问题描述】:

当我尝试编译 cppTableMaker.cpp 时收到一些错误消息,但它指向一个不是我创建的库。

1>------ Build started: Project: cppTableMaker, Configuration: Debug Win32 ------
1>cppTableMaker.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.24.28314\include\xtree(1373,13): error C2675: unary '++': '_Iter' does not define this operator or a conversion to a type acceptable to the predefined operator
1>        with
1>        [
1>            _Iter=std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>
1>        ]
1>G:\My Drive\coding\C++\cppTableMaker\cppTableMaker.cpp(31): message : see reference to function template instantiation 'void std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::insert<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>>(_Iter,_Iter)' being compiled
1>        with
1>        [
1>            _Kty=std::wstring,
1>            _Ty=std::vector<std::wstring,std::allocator<std::wstring>>,
1>            _Pr=std::less<std::wstring>,
1>            _Alloc=std::allocator<std::pair<const std::wstring,std::vector<std::wstring,std::allocator<std::wstring>>>>,
1>            _Iter=std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>
1>        ]
1>G:\My Drive\coding\C++\cppTableMaker\cppTableMaker.cpp(31): message : see reference to function template instantiation 'void std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::insert<std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>>(_Iter,_Iter)' being compiled
1>        with
1>        [
1>            _Kty=std::wstring,
1>            _Ty=std::vector<std::wstring,std::allocator<std::wstring>>,
1>            _Pr=std::less<std::wstring>,
1>            _Alloc=std::allocator<std::pair<const std::wstring,std::vector<std::wstring,std::allocator<std::wstring>>>>,
1>            _Iter=std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>
1>        ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.24.28314\include\xtree(1374,36): error C2100: illegal indirection
1>G:\My Drive\coding\C++\cppTableMaker\cppTableMaker.cpp(183,1): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>INTERNAL COMPILER ERROR in 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.24.28314\bin\HostX86\x86\CL.exe'
1>    Please choose the Technical Support command on the Visual C++
1>    Help menu, or open the Technical Support help file for more information
1>Done building project "cppTableMaker.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

好像来自:

#include <string>
#include <vector>
#include <map>
#include "cppTableMaker.h"
#include "common.cpp"
#include <locale>
#include <codecvt>
#include <sstream>
//bunch of functions and methods, and the table::modernTable::column class. (there's no main())
std::vector<int> table::modernTable::ins(std::vector<std::wstring> values)

    this->row ++;
    auto local_data = this->data;
    std::vector<int> local_colMaxLen = this->colMaxLen;
    int loopCount 0 ;
    for (auto const& [key, val] : local_data)
    
        local_data.insert(key, std::wstring(values.at(loopCount)));  //there's some info pointing to the first column of this line.
        local_colMaxLen.at(loopCount) = max(local_colMaxLen.at(loopCount), max(values.at(loopCount).length(), key.length()));
        loopCount ++;
    
    this->colMaxLen = local_colMaxLen;
    this->data = local_data;
    return local_colMaxLen;

包含头文件cppTableMaker.h:

#pragma once
#include <string>
#include <map>
#include <vector>
#ifndef cppTableMaker_h
#define cppTableMaker_h

namespace table 
    class modernTable 
        public:
        //variables
        std::map<std::wstring, std::vector<std::wstring>> data;
        int row 0 ;
        std::vector<int> colMaxLen;
        //init
        modernTable(std::map<std::wstring, std::vector<std::wstring>> data_INPUT, int row_INPUT, std::vector<int> colMaxLen_INPUT);    //constructer
        // ~modernTable();   //destructer
        //methods
        std::vector<int> ins(std::vector<std::wstring>);  //this function might be the cause
        std::wstring get();
        std::vector<std::wstring> rm(int rowNum);

        class column;
        auto col(std::wstring name);

        void mv(int index);
        void mvto(int index);
    ;
    //other classes

#endif

common.cpp 没有什么特别之处。它只包含一个max() 函数。

那么我的脚本有什么问题?这是什么意思?我将所有++ 更改为+1,问题仍然存在。错误发生在标准库中是没有意义的。我可以摆脱它吗?如何以及为什么?

【问题讨论】:

数据值的类型是std::vector&lt;std::wstring&gt;,但是你正在尝试插入std::wstring 谢谢!这解决了问题!结案! 【参考方案1】:

map::insert 需要迭代器和值或 std::pair。

如果您将问题行替换为配对,您的代码将被编译

    //local_data.insert(key, std::wstring(values.at(loopCount)));  //there's some info pointing to the first column of this line.
    std::pair<std::wstring, std::vector<std::wstring>> value = std::make_pair(key, std::vector<std::wstring>values.at(loopCount));
    local_data.insert(value);  //there's some info pointing to the first column of this line.

但我认为你想要这样的东西:

    val.push_back(values.at(loopCount));  //there's some info pointing to the first column of this line.

对于 do push into val,您应该对 for-variable 进行非常量引用

//for (auto const& [key, val] : local_data)
for (auto & [key, val] : local_data)

顺便说一句,如果你将 max 参数的类型从 size_t 转换为 int,你可以使用 std::max:

local_colMaxLen.at(loopCount) = std::max(local_colMaxLen.at(loopCount), int(std::max(values.at(loopCount).length(), key.length())));

使用std::max 的其他方式是将counts 的类型从int 更改为size_t

【讨论】:

以上是关于编译在类中定义方法的单个文件时,来自 C++ 中 xtree 的 VS2019 C2675 和 C2100 错误的主要内容,如果未能解决你的问题,请参考以下文章

C++中如何定义变量

C++要点总结

C++ main 可以在类中并调用派生类吗? [复制]

类与对象的认识

在类中定义数组的方法和字段

c++中的内联函数inline