I am trying to simulate a certain game as an exercise (dont ask me which, you will know if you know the game), however, I just learnt objects and class on the internet and the whole thing is still so confusing to me. The following is a section of my code that is giving me an error.
The error I have is:
C:\Users\N\Desktop\Untitled1.cpp In constructor 'ekop::Moveset::Moveset()':
56 9 C:\Users\N\Desktop\Untitled1.cpp [Error] no matching function for call to 'Move::Move()'
56 9 C:\Users\N\Desktop\Untitled1.cpp [Note] candidates are:
14 3 C:\Users\N\Desktop\Untitled1.cpp [Note] Move::Move(std::string, int, int, int)
14 3 C:\Users\N\Desktop\Untitled1.cpp [Note] candidate expects 4 arguments, 0 provided
11 7 C:\Users\N\Desktop\Untitled1.cpp [Note] Move::Move(const Move&)
11 7 C:\Users\N\Desktop\Untitled1.cpp [Note] candidate expects 1 argument, 0 provided
56 9 C:\Users\N\Desktop\Untitled1.cpp [Error] no matching function for call to 'Move::Move()'
56 9 C:\Users\N\Desktop\Untitled1.cpp [Note] candidates are:
14 3 C:\Users\N\Desktop\Untitled1.cpp [Note] Move::Move(std::string, int, int, int)
14 3 C:\Users\N\Desktop\Untitled1.cpp [Note] candidate expects 4 arguments, 0 provided
11 7 C:\Users\N\Desktop\Untitled1.cpp [Note] Move::Move(const Move&)
11 7 C:\Users\N\Desktop\Untitled1.cpp [Note] candidate expects 1 argument, 0 provided
56 9 C:\Users\N\Desktop\Untitled1.cpp [Error] no matching function for call to 'Move::Move()'
56 9 C:\Users\N\Desktop\Untitled1.cpp [Note] candidates are:
14 3 C:\Users\N\Desktop\Untitled1.cpp [Note] Move::Move(std::string, int, int, int)
14 3 C:\Users\N\Desktop\Untitled1.cpp [Note] candidate expects 4 arguments, 0 provided
11 7 C:\Users\N\Desktop\Untitled1.cpp [Note] Move::Move(const Move&)
11 7 C:\Users\N\Desktop\Untitled1.cpp [Note] candidate expects 1 argument, 0 provided
56 9 C:\Users\N\Desktop\Untitled1.cpp [Error] no matching function for call to 'Move::Move()'
56 9 C:\Users\N\Desktop\Untitled1.cpp [Note] candidates are:
14 3 C:\Users\N\Desktop\Untitled1.cpp [Note] Move::Move(std::string, int, int, int)
14 3 C:\Users\N\Desktop\Untitled1.cpp [Note] candidate expects 4 arguments, 0 provided
11 7 C:\Users\N\Desktop\Untitled1.cpp [Note] Move::Move(const Move&)
11 7 C:\Users\N\Desktop\Untitled1.cpp [Note] candidate expects 1 argument, 0 provided
C:\Users\N\Desktop\Untitled1.cpp In constructor 'ekop::ekop()':
52 12 C:\Users\N\Desktop\Untitled1.cpp [Note] synthesized method 'ekop::Moveset::Moveset()' first required here
I don't know what went wrong, the code is below.
Don't tell me not to use using namespace std;, I know I am not supposed to do that but I am not going to fix that in this program.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;
class Move
{
public:
Move(string a, int b, int c, int d)
{
name = a;
type_num = b;
power = c;
accuracy = d;
}
void setName (string a)
{
name = a;
}
void setType_num(int a)
{
type_num = a;
}
void setPower(int a)
{
power = a;
}
void setAccuracy(int a)
{
accuracy = a;
}
private:
string name, type;
int type_num, power, accuracy;
};
class ekop
{
public:
ekop()
{
Moveset moveset;
}
private:
class Moveset
{
public:
//constructor
//retrievors
private:
Move slot1, slot2, slot3, slot4;
};
};
int main()
{
//lines of code
return EXIT_SUCCESS;
}
edit 1: I had this constructor for the class Moveset, but since it didnt affect the error when I took it out, so I didnt include that in the post just now.
Moveset()
{
slot1.setName("MOVE 1");
slot1.setType_num(rand()%18);
slot1.setPower(10*(rand%15+1));
slot1.setAccuracy(5*(rand%11+10));
//repeat for the rest of the slots
}
edit 2: Now if I do this instead:
//No change before this
class Moveset
{
public:
//constructor
//retrievors
private:
Move slot1("MOVE 1", rand() % 18, 10*(rand % 15 + 1), 5 * (rand % 11 + 10)),
slot2("MOVE 2", rand() % 18, 10*(rand % 15 + 1), 5 * (rand % 11 + 10)),
slot3("MOVE 3", rand() % 18, 10*(rand % 15 + 1), 5 * (rand % 11 + 10)),
slot4("MOVE 4", rand() % 18, 10*(rand % 15 + 1), 5 * (rand % 11 + 10));
};
};
//no change after this
I get this error instead:
62 16 C:\Users\N\Desktop\Untitled1.cpp [Error] expected identifier before string constant
62 16 C:\Users\N\Desktop\Untitled1.cpp [Error] expected ',' or '...' before string constant
63 16 C:\Users\N\Desktop\Untitled1.cpp [Error] expected identifier before string constant
63 16 C:\Users\N\Desktop\Untitled1.cpp [Error] expected ',' or '...' before string constant
64 13 C:\Users\N\Desktop\Untitled1.cpp [Error] expected identifier before string constant
64 13 C:\Users\N\Desktop\Untitled1.cpp [Error] expected ',' or '...' before string constant
65 13 C:\Users\N\Desktop\Untitled1.cpp [Error] expected identifier before string constant
65 13 C:\Users\N\Desktop\Untitled1.cpp [Error] expected ',' or '...' before string constant
A MoveSet contains four default-initialized Move instances, but Move has no default constructor – because you have defined a constructor, which suppresses automatic generation of a default constructor.
Since the class needs a user-defined default constructor (built in type data members will otherwise have arbitrary values), define one.
One way to define it is to provide default values for all arguments of the existing constructor, but I'd define a separate default constructor.
In passing, the existing code
Move(string a, int b, int c, int d)
{
name = a;
type_num = b;
power = c;
accuracy = d;
}
… which uses assignments to initialize members, has a problem as a general way to do this. Namely, that members of class type that can't be default-initialized, can't be initialized this way. You'd experience the same problem as above, namely error messages about lacking a default constructor.
A solution is to use a member initializer list, like this:
Move( string a, int b, int c, int d )
: name( a ), type_num( b ), power( c ), accuracy( d )
{}
There are further possible refinements, such as move for efficiency or const for clarity (unfortunately one has to choose between them), but the important thing is to just generally adopt this convention of using member initializer lists, which works much more generally than assignments to members.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With