【问题标题】:cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty> &无法从 'std::vector<_Ty>' 转换为 'std::vector<_Ty> &
【发布时间】:2014-01-04 22:26:24
【问题描述】:

您好,我有两个问题:

  1. cannot convert from 'std::vector&lt;_Ty&gt;' to 'std::vector&lt;_Ty&gt; &amp;

    void CMapObjectPropertyPageAmbience::OnUpdateUI(CProperty* pProperty)
    {
        prt::PropertyAmbienceStringToData(pProperty, &m_propertyAmbience);
    
        std::vector<CFilename>& rSoundFileNameVector = m_propertyAmbience.AmbienceSoundVector;
        for (DWORD dwIndex = 0; dwIndex < rSoundFileNameVector.size(); ++dwIndex)
        {
            m_ctrlSoundFileList.InsertString(dwIndex, rSoundFileNameVector[dwIndex].c_str());
        }
    
        m_ctrlPlayType.SelectString(-1, m_propertyAmbience.strPlayType.c_str());
    
        SetDialogFloatText(GetSafeHwnd(), IDC_MAP_OBJECT_PROPERTY_AMBIENCE_INTERVAL, m_propertyAmbience.fPlayInterval);
        SetDialogFloatText(GetSafeHwnd(), IDC_MAP_OBJECT_PROPERTY_AMBIENCE_INTERVAL_VARIATION, m_propertyAmbience.fPlayIntervalVariation);
        SetDialogFloatText(GetSafeHwnd(), IDC_MAP_OBJECT_PROPERTY_AMBIENCE_MAX_VOLUME_AREA_PERCENTAGE, m_propertyAmbience.fMaxVolumeAreaPercentage * 100.0f);
    
        OnUpdatePropertyData(m_propertyAmbience.strName.c_str());
        OnChangePlayType();
    }
    

    错误出现在:

    std::vector<CFilename>& rSoundFileNameVector = m_propertyAmbience.AmbienceSoundVector;
    
  2. 2 个重载都不能转换所有参数类型:

    void CMapObjectPropertyPageAmbience::OnDeleteSoundFile()
    {
        DWORD dwCurSel = DWORD(m_ctrlSoundFileList.GetCurSel());
        if (dwCurSel >= m_propertyAmbience.AmbienceSoundVector.size())
            return;
    
        DeleteVectorItem < CFilename > ( & m_propertyAmbience.AmbienceSoundVector, dwCurSel);
        m_ctrlSoundFileList.DeleteString(dwCurSel);
    }
    

    DeleteVectorItem&lt;CFilename&gt;(&amp;m_propertyAmbience.AmbienceSoundVector, dwCurSel);

完全错误

1>Dialog\MapObjectPropertyPageAmbience.cpp(78): error C2440: 'initializing' : cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty> &'
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>          and
1>          [
1>              _Ty=CFilename
1>          ]
1>Dialog\MapObjectPropertyPageAmbience.cpp(205): error C2665: 'DeleteVectorItem' : none of the 2 overloads could convert all the argument types
1>          d:\dev\kraizy\novaline\srcs\client\eterpack\../eterBase/Stl.h(146): could be 'void DeleteVectorItem<CFilename>(std::vector<_Ty> *,unsigned long)'
1>          with
1>          [
1>              _Ty=CFilename
1>          ]
1>          d:\dev\kraizy\novaline\srcs\client\eterpack\../eterBase/Stl.h(191): or       'void DeleteVectorItem<CFilename>(std::vector<_Ty> *,T)'
1>          with
1>          [
1>              _Ty=CFilename,
1>              T=CFilename
1>          ]
1>          while trying to match the argument list '(std::vector<_Ty> *, DWORD)'
1>          with
1>          [
1>              _Ty=std::string
1>          ]

【问题讨论】:

  • m_propertyAmbience.AmbienceSoundVector 是什么类型?显示错误消息的准确和完整文本 - 它会告诉您编译器认为 _Ty 代表什么类型,以及编译器在寻找合适的重载时考虑和拒绝的函数。
  • 通常会有后续行来解释_Ty 是什么。查看输出窗口。
  • &m_propertyAmbience.AmbienceSoundVector 正在获取该向量的地址,删除 &
  • 这在 cmets 中看起来很可怕。您可以使用&lt;pre&gt;...&lt;/pre&gt; 将其添加到您的问题中吗?
  • 请将错误添加为对原始问题的编辑,然后删除这些 cmets。这将大大增加可读性!

标签: c++


【解决方案1】:

您正在尝试在需要vector&lt;CFilename&gt; 的地方使用vector&lt;string&gt;。这些是完全不相关的类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多