2011-04-29 54 views
0

得到一堆錯誤,它們很神祕。C++列表錯誤:: sort

這裏的所有相關代碼:

// list::sort 
#include <iostream> 
#include <list> 
#include <string> 
#include <cctype> 
#include "main.h" 

using namespace std; 

struct texture 
{ 
    int texture; 
    int ref; 
}; 

bool compare_nocase (texture first, texture second) 
{ 
    if(first.texture < second.texture) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

int main() 
{ 
    list<texture> mylist; 
    list<texture>::iterator it; 

    texture moose; 
    moose.ref = 3; 
    moose.texture = 6; 

    texture moose2; 
    moose2.ref = 1; 
    moose2.texture = 3; 

    texture moose3; 
    moose3.ref = 2; 
    moose3.texture = 14; 

    mylist.push_back (moose); 
    mylist.push_back (moose2); 
    mylist.push_back (moose3); 

    cout << "before sort mylist contains:"; 
    for (it=mylist.begin(); it!=mylist.end(); ++it) 
     cout << it->texture << endl; 
    cout << endl; 


    mylist.sort(compare_nocase); 

    cout << "after sort mylist contains:"; 
    for (it=mylist.begin(); it!=mylist.end(); ++it) 
    cout << it->texture << endl; 
    cout << endl; 

    return 0; 
} 

我只是測試了STL的排序功能,但我得到的錯誤:

c:\users\spaniel\documents\visual studio 2010\projects\list\list\main.cpp(12): error C2380: type(s) preceding 'texture' (constructor with return type, or illegal redefinition of current class-name?) 
1>c:\users\spaniel\documents\visual studio 2010\projects\list\list\main.cpp(18): error C2274: 'function-style cast' : illegal as right side of '.' operator 
1>c:\users\spaniel\documents\visual studio 2010\projects\list\list\main.cpp(18): error C2274: 'function-style cast' : illegal as right side of '.' operator 
1>c:\users\spaniel\documents\visual studio 2010\projects\list\list\main.cpp(35): error C2274: 'function-style cast' : illegal as right side of '.' operator 
1>c:\users\spaniel\documents\visual studio 2010\projects\list\list\main.cpp(39): error C2274: 'function-style cast' : illegal as right side of '.' operator 
1>c:\users\spaniel\documents\visual studio 2010\projects\list\list\main.cpp(43): error C2274: 'function-style cast' : illegal as right side of '.' operator 
1>c:\users\spaniel\documents\visual studio 2010\projects\list\list\main.cpp(51): error C2273: 'function-style cast' : illegal as right side of '->' operator 
1>c:\users\spaniel\documents\visual studio 2010\projects\list\list\main.cpp(59): error C2273: 'function-style cast' : illegal as right side of '->' operator 

回答

6

你有一個struct成員texture具有相同的名稱爲struct本身。

顯然VS 2010不喜歡這個,雖然GCC 4.1.2很好。我不知道哪個是正確的?

+1

上帝我是個白癡。謝謝! – Juiceyboxers 2011-04-29 07:43:15

+0

是的,海灣合作委員會確實沒問題,我沒有看到一個理由,這是錯的:? – 2011-04-29 07:46:46

+0

它有一個很好的理由是錯誤的。它引用struct中成員函數中的'texture'高度模糊和混淆。如果無論類型'texture'是否定義'operator()',這都是一個特別的問題。那麼你甚至不能告訴你是否試圖在'texture'成員上構造一個新的'texture'或使用函數調用操作符。 – Omnifarious 2011-04-29 07:59:58

0

恐怕你不能這樣做:

struct texture 
{ 
     v name is conflicted with struct name 
    int texture; 
    int ref; 
};