Error in C++ list::sort

Error in C++ list::sort

本文关键字:sort list C++ in Error      更新时间:2023-10-16

得到一堆错误,它们非常神秘

以下是所有相关代码:

// 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:usersspanieldocumentsvisual studio 2010projectslistlistmain.cpp(12): error C2380: type(s) preceding 'texture' (constructor with return type, or illegal redefinition of current class-name?)
1>c:usersspanieldocumentsvisual studio 2010projectslistlistmain.cpp(18): error C2274: 'function-style cast' : illegal as right side of '.' operator
1>c:usersspanieldocumentsvisual studio 2010projectslistlistmain.cpp(18): error C2274: 'function-style cast' : illegal as right side of '.' operator
1>c:usersspanieldocumentsvisual studio 2010projectslistlistmain.cpp(35): error C2274: 'function-style cast' : illegal as right side of '.' operator
1>c:usersspanieldocumentsvisual studio 2010projectslistlistmain.cpp(39): error C2274: 'function-style cast' : illegal as right side of '.' operator
1>c:usersspanieldocumentsvisual studio 2010projectslistlistmain.cpp(43): error C2274: 'function-style cast' : illegal as right side of '.' operator
1>c:usersspanieldocumentsvisual studio 2010projectslistlistmain.cpp(51): error C2273: 'function-style cast' : illegal as right side of '->' operator
1>c:usersspanieldocumentsvisual studio 2010projectslistlistmain.cpp(59): error C2273: 'function-style cast' : illegal as right side of '->' operator

您有一个与struct同名的struct成员texture

显然VS 2010不喜欢这样,尽管GCC 4.1.2很好。

恐怕你不能这样做:

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