I am receiving error C2512: 'LoadingWorldlet' : no appropriate default constructor available, when I try to compile this file. There is no explicit constructor, so I can not think of a reason for me to be recieving this error.
struct Worldlet {
int x, z;
glm::mat4 worldletMatrix;
std::vector<Voxel> blocks;
};
struct LoadingWorldlet {
int x, z;
std::future<Worldlet> &result;
};
class World {
public:
World();
~World();
void Init();
void InitRenderable();
void UpdateWorldletList(int x, int z);
void Render(Shader* worldShader, Camera *mainPov);
static Worldlet LoadWorldlet(int x, int z, std::ifstream &file);
private:
std::vector<Worldlet> worldlets;
std::vector<LoadingWorldlet> loadingWorldlets;
std::vector<std::string> loadingTitles;
std::vector<int> toRemove;
Renderable cube;
std::string worldName, prefix;
static const float CUBE_SIZE;
static const int LOADLIMIT = 1;
int GetLoadRadius(int r = 0) {
static int i = r;
return i;
}
};
This is the only function in which LoadingWorldlet is used:
void World::UpdateWorldletList(int x, int z) {
static int previousX, previousZ;
for(int index: toRemove) {
worldlets.erase(worldlets.begin() + index);
}
toRemove.clear();
int loaded = 0;
std::vector<int> clearList;
for(auto &loading: loadingWorldlets) {
if(loaded >= LOADLIMIT) break;
worldlets.push_back(loading.result.get());
clearList.push_back(loaded);
loaded++;
}
for(int i: clearList)
loadingWorldlets.erase(loadingWorldlets.begin()+i);
if(previousX != x && previousZ != z) {
int i = 0;
for(auto worldlet: worldlets) {
if(pow(worldlet.x - x, 2) + pow(worldlet.z - z, 2) > GetLoadRadius()) {
toRemove.push_back(i);
}
i++;
}
for(int recX = -GetLoadRadius(); recX < GetLoadRadius(); recX++) {
for(int recZ = -GetLoadRadius(); recZ < GetLoadRadius(); recZ++) {
bool cont = false;
for(auto worldlet: worldlets) {
if (worldlet.x == recX && worldlet.z == recZ) {
cont = true;
break;
}
}
for(auto loading: loadingWorldlets) {
if (loading.x == recX && loading.z == recZ) {
cont = true;
break;
}
}
if(cont || pow(recX - x, 2) + pow(recZ - z, 2) > GetLoadRadius())
continue;
std::ifstream file("./worlds/" + worldName + "/" + prefix + std::to_string(recX) + "X" + std::to_string(recZ) + "Z.json");
if (!file)
continue;
LoadingWorldlet loading;
loading.x = recX;
loading.z = recZ;
loading.result = std::async(LoadWorldlet, recX, recZ, file);
loadingWorldlets.push_back(loading);
}
}
}
}
I have tried adding a default constructor, but then I receive an error about a missing = operator. Doesn't the compiler automatically add in these things? How can I fix the error? If it is important, I am using Visual Studio 2013 preview.
Looking over the code, you'll need to a way to instantiate your reference to std::future<Worldlet> &result;
Typically, this is done through a constructor.
struct LoadingWorldlet
{
LoadingWorldlet( std::future<Worldlet> & inWorldlet ):
result( inWorldlet ) {}
int x, z;
std::future<Worldlet> &result;
};
Otherwise, you could simply not make the data member a reference ( this assumes that other data members don't also have mandatory constructors):
std::future<Worldlet> result;
Related
So it seems like C++ doesn't like it when it has to deal with several conversions at once (or more probably I don't know how to do it).
Here are my constructors (each T are different templates, one per class):
Number();
Number(long long int x);
Fraction(T num = T(0), T denom = T(1)) : m_num(num), m_denom(denom) { format(); }
Polynom(T x = T(0)) : m_size(1) { m_vect.push_back(T(x)); }
Matrix(int const& m, int const& n = 0, T const& x = T(0)) : m_m(m), m_n(n) { /*blablabla*/ }
And I am aksing for that :
Matrix<Polynom<Fraction<Number> > > M(5, 5);
I expected that it could be converted step by step as I have written T(...) everywhere, but it doesn't. How to solve that problem?
EDIT:
template <typename T>
class Matrix {
public:
Matrix(int const& m, int const& n = 0, T const& x = T(0)) : m_m(m), m_n(n) {
if (m_n == 0)
m_n = m_m;
T X(0);
m_grid = new T*[m_m];
for (int i = 0; i < m_m; i++) {
m_grid[i] = new T[m_n];
for (int j = 0; j < m_n; j++) {
X = new T(x);
m_grid[i][j] = X;
}
}
}
/* ... */
private:
T** m_grid;
int m_m, m_n;
}
template <typename T>
class Polynom {
public:
//Polynom() : m_size(0) {}
Polynom(T x = T(0)) : m_size(1) { m_vect.push_back(T(x)); }
/* ... */
private:
std::vector<T> m_vect;
int m_size;
}
template <typename T>
class Fraction {
public:
Fraction(T num = T(0), T denom = T(1)) : m_num(num), m_denom(denom) { format(); }
/* ... */
private:
T m_num;
T m_denom;
}
class Number {
public:
Number(long long int x){
if(x == 0)
m_digits.push_back(0);
while (x != 0) {
m_digits.push_back(x % 10);
x /= 10;
}
m_neg = (x < 0);
/* ... */
private:
std::vector<int> m_digits;
bool m_neg;
}
You need to have a Matrix constructor that can take the types (it could be that all it does is forwarding those arguments, but it has to take them).
template<typename T>
struct Matrix
{
template<typename ... Args>
Matrix(Args && ... args)
: mem(std::forward<Args>(args)...)
{ }
T mem;
};
I am trying to solve a problem dealing with 2d vectors array.FunClass gets two parameters, number of sets and the lines per set.
The problem is whenever update_mytable function is being called a segmentation fault error pops up.During a quick search on stackoverflow I read that in most cases the issue for the seg-fault is because we are trying to access to a non-existed vector.But I don't think what I have the same issue here.
using std::vector;
typedef vector<mytable_entry>::iterator mytable_iter;
struct mytable_entry
{
int x_entry;
int y_entry;
};
mytable_entry new_mytable_entry(int new_ip, int new_target) {
mytable_entry new_entry;
new_entry.ip_entry = new_ip;
new_entry.target_entry = new_target;
return new_entry;
}
class FunClass : public BaseClass
{
public:
FunClass(unsigned mytable_sets, unsigned mytable_lines_per_set)
: table_sets(mytable_sets), table_assoc(mytable_lines_per_set)
{
mytable_table = vector< vector<mytable_entry> > (table_sets,vector< mytable_entry> (table_assoc));
}
~FunClass() {
/* Vectors will be decontructed automatially out of scope */
}
virtual bool predict(int x, int y) {
/* If mytable_entry exist return true, else false */
unsigned mytable_set_index = x % table_sets;
if (find_mytable_entry(mytable_table[mytable_set_index])) {
return true;
} else {
return false;
}
}
virtual void update(bool val1, bool val2, int x, int y) {
unsigned mytable_set_index = x % table_sets;
if (val1 == val2){
update_mytable_table(mytable_table[mytable_set_index],ip,target_entry,table_assoc);
}
void update_mytable(vector<mytable_entry> & mytable_set, int x, int y,unsigned table_assoc){
mytable_entry new_entry = new_mytable_entry(ip,target);
mytable_set.push_back(new_entry);
if (mytable_set.size() > table_assoc){
mytable_set.erase(mytable_set.begin());
}
}
bool find_mytable_entry(vector<mytable_entry> & mytable_set, int x) {
mytable_iter iter;
for (iter = mytable_set.begin(); iter != mytable_set.end(); iter++){
if ((*iter).ip_entry == ip) {
mytable_entry tmp_mytable_entry = (*iter);
mytable_set.erase(iter);
mytable_set.push_back(tmp_mytable_entry);
return true;
}
}
return false;
}
private:
vector< vector<mytable_entry> > mytable_table;
unsigned table_sets, table_assoc;
};
If I remove the mytable_set.push_back(new_entry); from update_mytable everything goes fine then.
I have been reading for a while, but today I can't figure someting out and find a solution.
How to return a function pointer from a function table as parameter? All similair solutions don't work for this one and end up not compiling.
I have tried a lot of methods but the compiler always returns with errors like:
function returning function is not allowed solution (when using typedef void (*func)();)
As NO parameters have to be passed into the final routine it should be possible.
My simplified example:
void PrintOne(void) { printf("One")};
void PrintTwo(void) { printf("Two")};
struct ScanListStruct
{
int Value;
void (*Routine)(void);
}
const ScanListStruct DoList[] =
{
{1, PrintOne},
{2, PrintTwo}
}
bool GetRoutine(void *Ptr, int Nr)
{
for (int x =0; x<=1; x++)
{
if (DoList[x].Value = Nr)
{
Ptr = DoList[(x)].Routine;
//((*DoList[(x)].Routine)()); // Original Working and executing version!
return true;
}
}
return false;
}
void main(void)
{
int y = 1;
void (*RoutineInMain)(); // Define
if (GetRoutine( RoutineInMain, y) == true) // get the address
{
RoutineInMain(); // Execute the function
}
}
There a few things wrong with the code;
Syntax errors (missing ; etc.)
main must return int
GetRoutine should accept the function pointer by reference, not just a void* pointer to anything
if condition should contain an equality test, not an assignment
As follows, works as expected;
void PrintOne(void) { printf("One"); };
void PrintTwo(void) { printf("Two"); };
struct ScanListStruct
{
int Value;
void (*Routine)(void);
};
const ScanListStruct DoList[] =
{
{1, &PrintOne},
{2, &PrintTwo}
};
bool GetRoutine(void (*&Ptr)(), int Nr)
{
for (int x =0; x<=1; x++)
{
if (DoList[x].Value == Nr)
{
Ptr = *DoList[(x)].Routine;
//((*DoList[(x)].Routine)()); // Original Working and executing version!
return true;
}
}
return false;
}
int main(void)
{
int y = 1;
void (*RoutineInMain)(); // Define
if (GetRoutine( RoutineInMain, y) == true) // get the address
{
RoutineInMain(); // Execute the function
}
}
Prints One.
You have lots of errors in your code. Like here you put the comas at the wrong place:
void PrintOne(void) { printf("One")};
void PrintTwo(void) { printf("Two")};
It should be
void PrintOne(void) { printf("One");}
void PrintTwo(void) { printf("Two");}
And here you are using the wrong operator, = instead of ==.
if (DoList[x].Value = Nr)
When the argument Ptr is a pointer, and that is passed by value, so the value assigned in the function will not be available when the function returns.
This is how your code should be:
void PrintOne(void) { printf("One"); }
void PrintTwo(void) { printf("Two"); }
typedef void(*prototype)();
struct ScanListStruct
{
int Value;
prototype Routine;
};
const ScanListStruct DoList[] =
{
{ 1, PrintOne },
{ 2, PrintTwo }
};
bool GetRoutine(prototype &Ptr, int Nr)
{
for (int x = 0; x <= 1; x++)
{
if (DoList[x].Value == Nr)
{
Ptr = DoList[(x)].Routine;
return true;
}
}
return false;
}
int main()
{
int y = 1;
prototype RoutineInMain; // Define
if (GetRoutine(RoutineInMain, y) == true) // get the address
{
RoutineInMain(); // Execute the function
}
return 0;
}
I tried to write a simple game and I accidently got errors
"No matching function for call to Monster::Monster()"
and
"No matching function for call to Player::Player()".
Both were for the Game class constructor. Class Game is not related to classes Monster or Player in any way. Here are the codes.
main.cpp
#include <game.h>
#include <iostream>
using namespace std;
int main() {
Game game = Game( );
while(!game.hasFinished( )) {
game.round( );
}
}
game.h
#ifndef GAME
#define GAME
#include <entity.h>
class Game {
private:
bool finished = false;
char area[15][15];
char defaultSign = '*';
int size = 15;
Monster monsters[4];
Player player;
int score = 0;
void cls();
void fill();
void display();
void playerMove();
public:
Game();
void round();
int getScore();
bool hasFinished();
};
#endif
game.cpp
#include <game.h>
#include <iostream>
#include <conio.h>
Game::Game() {
player = Player(7, 7);
monsters[0] = Monster(0, 0);
monsters[1] = Monster(size - 1, size - 1);
monsters[2] = Monster(size - 1, 0);
monsters[3] = Monster(0, size - 1);
}
bool Game::hasFinished() {
return !finished;
}
int Game::getScore() {
return score;
}
void Game::cls() {
for(int i = 0; i < 50; i++) {
std::cout << "\n";
}
}
void Game::fill() {
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
area[i][j] = defaultSign;
}
}
for(int i = 0; i < 4; i++) {
area[monsters[i].getX( )][monsters[i].getY( )] = monsters[i].getSign( );
}
area[player.getX( )][player.getY( )] = player.getSign( );
}
void Game::display() {
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
std::cout << area[i][j];
}
std::cout << "\n";
}
}
void Game::round() {
cls( );
fill( );
display( );
playerMove( );
for(int i = 0; i < 4; i++) {
monsters[i].moveTowards(player);
}
score++;
}
void Game::playerMove() {
bool moved = false;
while(!moved) {
char c = getch( );
if(c == 'w' || c == 'W') {
player.move(player.getX( ), player.getY( ) + 1);
moved = true;
}
if(c == 'a' || c == 'A') {
player.move(player.getX( ) - 1, player.getY( ));
moved = true;
}
if(c == 's' || c == 'S') {
player.move(player.getX( ), player.getY( ) - 1);
moved = true;
}
if(c == 'd' || c == 'D') {
player.move(player.getX( ) + 1, player.getY( ));
moved = true;
}
}
}
entity.h
#ifndef ENTITY
#define ENTITY
class Entity {
protected:
int x, y;
char sign;
public:
Entity(int xPos, int yPos);
int getX();
int getY();
void move(int xPos, int yPos);
char getSign();
};
class Player : public Entity {
private:
char sign = 'P';
public:
Player(int xPos, int yPos);
};
class Monster : public Entity {
private:
char sign = 'M';
public:
Monster(int xPos, int yPos);
void moveTowards(Entity p);
};
#endif
entity.cpp
#include <entity.h>
Entity::Entity(int xPos, int yPos) {
x = xPos;
y = yPos;
sign = '*';
}
void Entity::move(int xPos, int yPos) {
x = xPos;
y = yPos;
}
int Entity::getX() {
return x;
}
int Entity::getY() {
return y;
}
char Entity::getSign() {
return sign;
}
Player::Player(int xPos, int yPos)
: Entity(xPos, yPos) {
}
Monster::Monster(int xPos, int yPos)
: Entity(xPos, yPos) {
}
void Monster::moveTowards(Entity p) {
int moveArea[2] = {x, y};
if(x > p.getX( )) moveArea[0]--;
if(x < p.getX( )) moveArea[0]++;
if(y > p.getY( )) moveArea[1]--;
if(y < p.getY( )) moveArea[1]++;
move(moveArea[0], moveArea[1]);
}
Please, help!
The problem is
Monster monsters[4]; // ---> Means initiliaze 4 Monsters with a ctor which takes no arguments(eg default Ctor)
Player player; ---> //The initiliaze 1 Player with a ctor which take no arguments
Since you defined Monster(int xPos, int yPos); default Ctors are deleted. You have to define them yourself. Entity has the same problem. I compiled your code , if you do like below it will work.
Add "Entity() = default;" , initiliaze x and y like below,
class Entity {
protected:
int x = 0; --> Critical
int y = 0; --> Critical
char sign;
public:
Entity() = default; --> Critical
Entity(int xPos, int yPos);
int getX();
int getY();
void move(int xPos, int yPos);
char getSign();
};
Do the same for player and monster
class Monster : public Entity {
private:
char sign = 'M';
public:
Monster(int xPos, int yPos);
Monster() = default; --> Critical
void moveTowards(Entity p);
};
class Player : public Entity {
private:
char sign = 'P';
public:
Player() = default; --> Critical
Player(int xPos, int yPos);
};
You should add a default constructor to your Monster class because it's implicitly called when the Monster array is initialized. If you don't want to do that for some reason, you could change the Monster array contain raw or smart pointers to Monster objects.
I'm trying to construct a two-dimensional boolean array with a class I've created called Grid. The Grid object is a private member class of another class called GameOfLife. Whenever I create a GameOfLife object with the parameters belove, the Grid object first gets created with the default constructor, then it gets created again with the constructor with parameters, and then for some reason Grid's deconstructor runs and deletes everything ? I'm really out of ideas :p I'm running MinGW GCC on Eclipse Luna.
Main.cpp
const int HEIGHT = 25;
const int WIDTH = 25;
#include <iostream>
#include "GameOfLife.h"
int main(int argc, const char * argv[]) {
GameOfLife game = GameOfLife(HEIGHT, WIDTH, false);
game.play();
return 0;
}
Grid.h
#ifndef __Game_Of_Life__Grid__
#define __Game_Of_Life__Grid__
#include <stdio.h>
class Grid {
public:
Grid(int y, int x, bool state);
Grid();
void allocate(int x, int y, bool state);
void deallocate();
void set(int x, int y, bool state);
bool get(int x, int y);
void setAll(bool state);
void switchBoards();
~Grid();
private:
bool ** oldGeneration;
bool ** newGeneration;
int height;
int width;
};
#endif /* defined(__Game_Of_Life__Grid__) */
Grid.cpp
#include "Grid.h"
Grid::Grid(int y, int x, bool state) {
allocate(x, y, state);
}
void Grid::allocate(int x, int y, bool state) {
height = y;
width = x;
oldGeneration = new bool*[height];
newGeneration = new bool*[height];
for (int i = 0; i < height; i++) {
oldGeneration[i] = new bool[width];
newGeneration[i] = new bool[width];
}
}
Grid::~Grid() {
deallocate();
}
void Grid::switchBoards() {
bool ** temp = oldGeneration;
oldGeneration = newGeneration;
newGeneration = temp;
delete temp;
}
bool Grid::get(int x, int y) {
return oldGeneration[y][x];
}
void Grid::set(int x, int y, bool state) {
newGeneration[y][x] = state;
}
void Grid::deallocate() {
if (oldGeneration != NULL || newGeneration != NULL) {
for (int i = 0; i < height; i++) {
delete [] oldGeneration[i];
delete [] newGeneration[i];
}
delete [] oldGeneration;
delete [] newGeneration;
}
return;
}
Grid::Grid() {
oldGeneration = NULL;
newGeneration = NULL;
width = 0;
height = 0;
}
void Grid::setAll(bool state) {
for (int i = 0; i < height; i++) {
for (int n = 0; n < width; n++) {
newGeneration[i][n] = state;
}
}
}
GameOfLife.h
#ifndef __Game_Of_Life__GameOfLife__
#define __Game_Of_Life__GameOfLife__
#include <stdio.h>
#include "Grid.h"
#include <iostream>
class GameOfLife {
private:
Grid board;
public:
GameOfLife(int y, int x, bool state);
GameOfLife();
~GameOfLife();
void play();
void welcome();
void makeBoard();
void updateBoard();
int findAliveNeighbours(int x, int y);
};
#endif /* defined(__Conway__GameOfLife__) */
GameOfLife.cpp
#include "GameOfLife.h"
const int WIDTH = 100;
const int HEIGHT= 75;
GameOfLife::GameOfLife(int y, int x, bool state) {
board = Grid(y, x, state);
}
GameOfLife::GameOfLife() {
board = Grid();
}
GameOfLife::~GameOfLife() {
board.deallocate();
}
void GameOfLife::play() {
welcome();
makeBoard();
for (int i = 0; i < HEIGHT; i++) {
for (int n = 0; n < WIDTH; n++) {
std::cout << board.get(n,i) << " ";
}
std::cout << std::endl;
}
updateBoard();
std::cout << std::endl;
for (int i = 0; i < HEIGHT; i++) {
for (int n = 0; n < WIDTH; n++) {
std::cout << board.get(n,i) << " ";
}
std::cout << std::endl;
}
}
void GameOfLife::makeBoard() {
int x1,x2,x3,x4, y1,y2,y3,y4;
x1 = 10; y1 = 10;
x2 = 10; y2 = 11;
x3 = 10; y3 = 12;
x4 = 11; y4 = 13;
int x5 = 0; int y5 = 0;
board.set(x1, y1, true);
board.set(x2, y2, true);
board.set(x3, y3, true);
board.set(x4, y4, true);
board.set(x5, y5, true);
}
void GameOfLife::welcome() {
std::cout << "Welcome to Conway's Game Of Life"
<< std::endl;
}
GameOfLife::GameOfLife(int y, int x, bool state) {
// board is a member variable that gets initialized
// with the default constructor.
// Then it gets replaced by assignment with a different
// Grid object. The temporary object gets deleted at
// the end of the line.
board = Grid(y, x, state);
}
Change the implementation to:
GameOfLife::GameOfLife(int y, int x, bool state) : board(y, x, state) {}
Similarly, change the default constructor to:
GameOfLife::GameOfLife() {}
The more important problem that needs to be fixed is that you are breaking The Rule of Three.
You need to add proper implementations of the copy constructor and the copy assignment opertor in Grid.
The other, and better, option is to change the internal data of Grid to
std::vector<std::vector<bool>> oldGeneration;
std::vector<std::vector<bool>> newGeneration;
Then, the compiler generated copy constructor and copy assignment operator will be good enough.