0

So, I'm trying to sort a vector of a custom object myStruct according to num1 in the structure. I have to following code:

struct myStruct{

    int num1;
    std::vector<int> vecStruct;
    int num2;

    myStruct(int n1, std::vector<int> j, int n2) : num1(n1), vecStruct(j), num2(n2) {}

    bool operator < (const myStruct& s) const
    {
        return (num1 < s.num1);
    }
};

and then I use this to sort:

sort(myVector.begin(), myVector.end());

where

std::vector<myStruct> myVector;

I've tried to follow these instructions, but I keep getting a a compiling error:

No matching constructor for initialisation of 'myStruct'

I'm using Xcode 4.6.2 on MacOSX Mountain Lion - not that it matters, but Xcode is what's giving me this error

Community
  • 1
  • 1
FriedRike
  • 145
  • 2
  • 4
  • 20

2 Answers2

3

In the code you've presented above, your constructor name doesn't match the name of the struct (myStruct vs student). I'd start by fixing that.

Ben K.
  • 81
  • 1
  • 4
2

When I tested your code, but changing the name of your struct to student (given that is what it's referred to inside the struct), I received no compilation errors.

Maybe that's the issue?

dlanod
  • 8,664
  • 8
  • 54
  • 96
  • I changed that, I still get the same compiling error... I don't understand how it works on yours but not on mine – FriedRike May 04 '13 at 15:30
  • May I ask, how are you compiling it? I try to build it with Xcode and it doesn't work. Neither does it with `g++` – FriedRike May 04 '13 at 15:50
  • VS Express 2012. Unfortunately I don't have Xcode to test it with, but I don't see anything in the code that looks controversial to not be cross-compilable. When you corrected the syntax did the error just change to `"No matching constructor for initialisation of 'student'"`? – dlanod May 04 '13 at 22:09
  • Ignore the bit about `student` above, I just noticed you'd changed it all to `myStruct`. Can you include the code you're using to populate the vector? I was getting a similar error when playing around because I briefly used the (non-existent) default constructor to populate the vector. – dlanod May 04 '13 at 22:21