使用std set时遇到一些问题

时间:2011-09-14 23:07:44

标签: c++ visual-c++

所有,我创建了以下代码:

struct Styles{
wstring styleVal;
wstring styleName;
};
set<Styles> vtrStyles1;//filled in somehow
set<Styles> vtrStyles2;//filled in somehow

set<Styles> vtrStyles3(vtrStyles1);
vtrStyles3.insert(vtrStyles2.begin(), vtrStyles2.end());
unCommon = vtrStyles1.size() + vtrStyles2.size() - vtrStyles3.size();

我在stdafx.h中包含了set,​​并使用了namespace std;但是我得到了以下错误:

1>XMLDOMFromVCDlg.cpp(5377): error C2065: 'set' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5377): error C2275: 'Styles' : illegal use of this type as an expression
1>          XMLDOMFromVCDlg.cpp(5344) : see declaration of 'Styles'
1>XMLDOMFromVCDlg.cpp(5377): error C2065: 'vtrStyles1' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5378): error C2065: 'set' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5378): error C2275: 'Styles' : illegal use of this type as an expression
1>          XMLDOMFromVCDlg.cpp(5344) : see declaration of 'Styles'
1>XMLDOMFromVCDlg.cpp(5378): error C2065: 'vtrStyles2' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5497): error C2065: 'vtrStyles1' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5497): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5507): error C2065: 'vtrStyles1' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5507): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5517): error C2065: 'vtrStyles1' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5517): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5534): error C2065: 'vtrStyles1' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5534): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5545): error C2065: 'vtrStyles1' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5545): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5555): error C2065: 'vtrStyles1' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5555): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5565): error C2065: 'vtrStyles1' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5565): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5606): error C2065: 'vtrStyles2' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5606): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5616): error C2065: 'vtrStyles2' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5616): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5626): error C2065: 'vtrStyles2' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5626): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5643): error C2065: 'vtrStyles2' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5643): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5654): error C2065: 'vtrStyles2' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5654): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5664): error C2065: 'vtrStyles2' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5664): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5674): error C2065: 'vtrStyles2' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5674): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5704): error C2065: 'set' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5704): error C2275: 'Styles' : illegal use of this type as an expression
1>          XMLDOMFromVCDlg.cpp(5344) : see declaration of 'Styles'
1>XMLDOMFromVCDlg.cpp(5704): error C2065: 'vtrStyles1' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5704): error C3861: 'vtrStyles3': identifier not found
1>XMLDOMFromVCDlg.cpp(5705): error C2065: 'vtrStyles3' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5705): error C2228: left of '.insert' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5705): error C2065: 'vtrStyles2' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5705): error C2228: left of '.begin' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5705): error C2065: 'vtrStyles2' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5705): error C2228: left of '.end' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5706): error C2065: 'vtrStyles1' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5706): error C2228: left of '.size' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5706): error C2065: 'vtrStyles2' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5706): error C2228: left of '.size' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5706): error C2065: 'vtrStyles3' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5706): error C2228: left of '.size' must have class/struct/union
1>          type is ''unknown-type''
1>XMLDOMFromVCDlg.cpp(5707): error C2065: 'vtrStyles3' : undeclared identifier
1>XMLDOMFromVCDlg.cpp(5707): error C2228: left of '.size' must have class/struct/union
1>          type is ''unknown-type''

所以最近怎么样? thx提前。

欢呼声

岱岳

3 个答案:

答案 0 :(得分:1)

有些东西你没告诉我们。如果为operator<结构定义Styles(见下文),则使用Visual Studio 2008编译以下程序(使用已发布代码的部分进行复制粘贴)。

#include <set>
#include <string>
using namespace std;

struct Styles {
    wstring styleVal;
    wstring styleName;
};

int main ( int, char ** )
{
    set<Styles> vtrStyles1;
    set<Styles> vtrStyles2;
    set<Styles> vtrStyles3(vtrStyles1);
    vtrStyles3.insert(vtrStyles2.begin(), vtrStyles2.end());
    const size_t unCommon =
        vtrStyles1.size() + vtrStyles2.size() - vtrStyles3.size();
}

以下定义对我来说似乎没问题,但它可能不是您的想法。在main()之前插入此内容。

bool operator< ( const Styles& lhs, const Styles& rhs )
{
    return (lhs.styleName < rhs.styleName);
}

如果您可以编译此程序,那么您的问题就在其他地方。

答案 1 :(得分:0)

假设您已将stdafx.h编辑为#include <set>并添加了using namespace std,您是否按Ctrl + F7只编译XMLDOMFromVCDlg.cpp?如果是这样,您需要首先重新编译stdafx.cpp以重新生成预编译的头文件。

答案 2 :(得分:0)

您需要使用std :: set或使用std :: set或使用namespace std。 另外,请确保

#include <set> 

在stdafx.h或代码中。