This question already has an answer here:
error: no matching function for call to ‘Point::Point()
(1 answer)
Closed 7 months ago.
I developed two modules with separate implementation and interfaces.
These are the ones:
This is file Move.h:
#pragma once
#include "utils.h"
class Move {
private:
int x;
int y;
public:
Move(int x_inp, int y_inp);
char getX();
int getY();
};
And this is Move.cpp:
#include "Move.h"
Move::Move(int x_inp, int y_inp) {
int size = 10
this->x = x_inp;
this->y = y_inp;
};
int Move::getX() {
return this->x;
};
int Move::getY() {
return this->y;
}
This is the file Node.h:
#pragma once
#include "Move.h"
#include <vector>
class Node {
private:
Move move;
Node* parent;
std::vector <Node*> children;
public:
Node(Move inp_move, Node* parent_inp);
double value(const float EXPLORE_CONST);
void add_children(std::vector<Node*>);
};
#include "Node.cpp"
Node::Node(Move inp_move, Node* parent_inp) { // this is where compilation error rises.
this->move = inp_move;
this->parent = parent_inp;
}
void Node::add_children(std::vector<Node*> list_of_children) {
for (Node* item : list_of_children) {
this->children.push_back(item);
}
}
It always gives me the error that no default constructor exists for class "Move". I am really stuck and try to figure out the solution but didn't find the answer.
Can you guys help me please.
Thanks
you should try literally adding a default constructor to your "Move" class.
public:
Move() {}
//default constructor
Move(int x_inp, int y_inp);
char getX();
int getY();
};
´´´´
You need use member initializer lists:
Node::Node(Move inp_move, Node* parent_inp)
: move{inp_move}
, parent{parent_inp}
{
}
Or provide default constructor for Move as suggested in comment. IMO use of member initializer lists is better choice.
Related
I am trying to implement a Polynomial structure using a linked list of Terms (the linked list is implemented separately).
When I run my main program, I get a (Thread 1: EXC_BAD_ACCESS code=2) error on the line
coeff = x; in the definition my setCoeff function.
I tried commenting out that specific function call, but it gives me the same error for the setX() and setY() functions.
I think I have my files and functions set up properly, I cannot figure out why it is not letting me use these functions.
Please help !
In order, I have included: Polynomial.h, Polynomial.cpp, and main.cpp.
#ifndef __Polynomial__Polynomial__
#define __Polynomial__Polynomial__
#include <stdio.h>
class Term {
private:
int coeff;
int deg_x;
int deg_y;
public:
Term();
int getCoeff();
int getX();
int getY();
void setX(int);
void setY(int);
void setCoeff(int);
};
#endif /* defined(__Polynomial__Polynomial__) */
___________________________
#include "Polynomial.h"
Term::Term() {
coeff = NULL;
deg_x = NULL;
deg_y = NULL;
}
int Term::getCoeff(){
return coeff;
}
int Term::getX() {
return deg_x;
}
int Term::getY() {
return deg_y;
}
void Term::setX(int x){
deg_x = x;
}
void Term::setY(int x){
deg_y = x;
}
void Term::setCoeff(int x){
coeff = x;
}
__________________________
#include <iostream>
#include <fstream>
#include "Polynomial.h"
int main() {
Term* t1;
t1->setCoeff(4);
t1->setX(3);
t1->setY(6);
}
You never create an object. You have Term* t1, which is an uninitialized pointer to random memory, then you try to use it with t1->setCoeff(4) which is trying to use an object that was never created. That's definitely gonna go wrong.
Do this instead..
auto t1 = std::make_unique<Term>();
Or if you don't need it to be a pointer, you can create a simple stack variable and access it with '.' operator like this ...
Term t1;
t1.setCoeff(4);
t1.setX(3);
t1.setY(6);
Recently I have been working on remaking my remake of the classic snake game, this time employing a deque instead of a linked list for the snake's tail. I construct a tail segment and try to emplace it at the front of the deque and get a strange error.
no matching function for call to 'std::deque<SnakePart>::emplace_front(<brace-enclosed initializer list>)'|
Since I am still quite new to C++ I am clueless as to why this error is occurring. It would be appreciated if someone could help me solve this conundrum.
SnakePart.cpp
http://pastebin.com/verR9bpn
Snake.hhttp://pastebin.com/XUyNAVKK
Minimal Complete & Verifiable Example:
#include <deque>
struct Test {
Test(int x, int y) : x(x), y(y){}
int x;
int y;
};
int main () {
int x = 4, y = 5;
std::deque<Test> tester;
tester.emplace_front({x, y});
return 0;
}
SnakePart.cpp
#include "Snake.h"
#include <deque>
void SnakePart::advance(int x, int y, bool loose_tail = true) {
parts.emplace_front({x, y});
if(loose_tail)
parts.pop_back();
}
Snake.h
#ifndef SNAKE_H_INCLUDED
#define SNAKE_H_INCLUDED
#include <deque>
class SnakeHead {
private:
int posX, posY;
// Snake head functions
void input();
void movement();
};
class SnakePart {
private:
std::deque<SnakePart> parts;
void advance(int x, int y, bool loose_tail);
};
#endif // SNAKE_H_INCLUDED
The emplace_front function takes its arguments and passes to a suitable constructor in the contained object. The Test class doesn't have a constructor which takes a single initializer-list argument. It have a constructor taking two int arguments which means you could use
tester.emplace_front(x, y);
It also have a copy-constructor which means you could do
tester.emplace_front(Test{x, y});
Remove curly braces from emplace_front function params. Semantic of emplace* functions means that arguments passed as like in constructor of container type T. Passing it with {} makes argument type is std::initializer_list.
Hi which data structures will be the best and the easier to implement tree with two leaf type for example leaf, which contains int and leaf, which contains pointer to function. I need this to genetic programming.
I do this on my own, but i am afraid this is bad idea.
node.h
#ifndef NODE_H
#define NODE_H
#include <iostream>
#include <stdio.h>
typedef int(* func) (int, int);
using namespace std;
class node
{
private:
node * left, * right, *parent;
int i;
int type; //0 - term, 1 - func
public:
node(node* parent,node* left,node* right, int i, int type);
~node();
void changeLeft(node* left);
void changeRight(node* right);
void changeParent(node* parent);
node* getLeft();
node* getRight();
node* getParent();
int getI();
virtual func getFunction(){return 0;}
virtual int getTerminal(){return 1;}
void show();
};
#endif // NODE_H
tree.h
#ifndef TREE_H
#define TREE_H
#include <vector>
#include <cstdio>
#include <iostream>
#include "node.h"
#include "nodefunc.h"
#include "nodeterm.h"
#include "functionset.h"
#include "terminalset.h"
using namespace std;
enum typeInit
{
GROW_INIT,
FULL_INIT
};
enum typeNode
{
TERMINAL_NODE,
FUNCTION_NODE
};
class tree
{
public:
vector <node*> nodes;
int depth;
int counterNodes;
private:
node* root;
public:
tree(int depth);
~tree();
public:
void initialize(typeInit type);
void show();
node* getRoot();
int run();
private:
void initializeGrow(functionSet functions, terminalSet terminals, node* subroot, int depth);
int initializeFull(functionSet functions, terminalSet terminals, node* subroot, int depth);
int showSubtree(node* subtree, int depth);
int runSubtree(node* subtree, int depth);
};
#endif // TREE_H
You can further develop your idea using a tagged union:
enum typeNode {TERMINAL_NODE, FUNCTION_NODE};
class node
{
union
{
int i;
func f;
};
typeNode type;
// ...
};
Only one of the types can be in use at any one time and the type field explicitly indicates which one is in use.
Along this way you could also use boost::variant in place of the anonymous union.
A different approach is using a symbol base class:
class symbol
{
public:
virtual int eval(int [] = nullptr) = 0;
// ...
};
class terminal : public symbol
{
public:
virtual int eval(int []) override;
{
return i;
}
// ...
private:
int i;
};
class function1 : public symbol
{
public:
virtual int eval(int params[]) override;
{
return f(params[0], params[1]);
}
// ...
private:
func f;
}
and the node class is something like:
class node
{
node *left, *right, *parent;
symbol *s;
// ...
};
Since the function class is stateless, you need to pass some parameters to the eval function. This is an annoyance but helps to keep simple other parts of the genetic program (e.g. building the tree, tree recombination...).
Use three-address code: https://en.wikipedia.org/wiki/Three-address_code.
This is much more easier to work with.
Have a look to the implementation of a linear genetic programming variant called Multi-Expression programming which uses it: http://www.mepx.org
So I am trying to forward declare a class in my C++ project and then create it in main.
So I have player_obj.cpp which contains the class, classes.h which forward declares the class, and main.cpp which uses it.
classes.h
#ifndef CLASSES_H
#define CLASSES_H
class player_class
{
public:
int x;
int y;
char sprite;
int xprevious;
int yprevious;
private:
bool active;
public:
void update_xy();
player_class(int _x, int _y, char _sprite);
void step();
void destroy();
};
#endif
main.cpp
#include <iostream>
#include "classes.h"
using namespace std;
int main()
{
player_class player_obj (5,5,'#');
cout << player_obj.x << ", " << player_obj.y << endl;
return 0;
}
and player_obj.cpp
#include <iostream>
#include <Windows.h>
using namespace std;
class player_class
{
public:
//Coordinates
int x;
int y;
//Sprite
char sprite;
//Previous coordinates
int xprevious;
int yprevious;
//Not everyone can set the activity
private:
//Active
bool active;
//Update xprevious and yprevious - Called by the step event
void update_xy()
{
xprevious = x;
yprevious = y;
}
//All functions public
public:
//Create event/Constructer
player_class(int _x, int _y, char _sprite)
{
//Set default variables
x = _x;
y = _y;
sprite = _sprite;
xprevious = x;
yprevious = y;
active = true;
}
//Step event
void step()
{
//Update old xprevious and yprevious
update_xy();
//Do other stuff here
}
//Drestroy event
void destroy()
{
active = false;
}
};
I thought that would work out all right but when I compile and run it I get:
main.cpp:(.text+0x2c): undefined reference to`player_class::player_class(int, int, char)'
I've done some research, but I can't seem to fix this issue.
I greatly appreciate any help!
Well you're sort of close, what you have in your header is indeed a class declaration (not a forward declaration mind you).
The problem is you never defined it. What you have in player_obj.cpp is an abomination of class redefinition, but you already have your class declared. Just include the header file and define the functions one by one and you're done!
#include "classes.h"
player_class::player_class(int _x, int _y, char _sprite)
{
//Set default variables
x = _x;
y = _y;
sprite = _sprite;
xprevious = x;
yprevious = y;
active = true;
}
// and so on
If you're serious about learning modern C++ though, a few notes:
#pragma once is the modern way of guarding header files. Don't use those #ifdef..#endif constructs.
generally speaking, don't name anything starting with underscores. Especially not parameters visible as part of your public contract.
you have class initializers for a reason, use them! You don't need half a screen of copy pasting variables in your constructors.
You dont want a forward declaration. You want a declaration. It is a classical case of declaring a class in a header file and defining its functions in a cpp file. Then including the header where-ever you want to use your class
You only need forward declarations when you want to use a pointer to that class as a parameter to a function or a member variable somewhere but the definition of that class is not available yet.
Note that when you forward declare a class, you cannot use this class's member variables or functions in that header
-regards
Gautam
This question already has answers here:
What are access specifiers? Should I inherit with private, protected or public?
(2 answers)
Closed 9 years ago.
How do you guys find the value of s.x from this code
I am beginner of c++ and dont know how to solve it
Please help thanks
// StarterLab.c : C Program to convert to C++
//
//#include "stdafx.h" // required for Visual Studio
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//#include "MemTracker.h"
#pragma warning (disable:4996)
using namespace std;
struct variable
{
friend void showCalculation(variable a);
private:
int x;
int y;
int sum;
public:
void Calculate(int x,int y);
};
void showCalculation(variable a)
{
printf("%d",a.sum);
};
void variable:: Calculate (int x,int y)
{
sum = x + y;
};
int main ()
{
variable s;
s.Calculate(7, 6);
showCalculation(s);
printf("%d",s.x);
}
How do you guys find the value of s.x from this code
I am beginner of c++ and dont know how to solve it
Please help thanks
The variable x is private, so you cannot access it directly. You could add a member function to get it:
int variable::GetX() {
return x;
}
printf("%d", s.GetX());
You can not access s.x because x is a private member. You have two options.
Create a getter:
int variable::X() { return x; }
or make it public:
public:
int x;
int y;
int sum;
Note that using getters/setters is the appropriate way of doing this.