开发者

Error in C++ list::sort

Getting a bunch of errors and they are very cryptic.

Here's all the relevant code:

// 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,开发者_JAVA技巧 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;
}

I am just testing out the STL sort function but I get the errors:

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


You have a struct member texture with the same name as the struct itself.

Apparently VS 2010 doesn't like this, although GCC 4.1.2 is fine. I'm not sure which is correct...


I'm afraid you cannot do like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜