error c2059: syntax error: '(' - c++

I don't understand this error it's written the exact same in the tutorial but my one generates an error.
#include "drawEngine.h"
#include <Windows.h>
#include <iostream>
using namespace std;
DrawEngine::DrawEngine(int xSize, int ySize)
{
screenWidth = xSize;
screenHeight = ySize;
//set cursor visibility to false
map = 0;
cursorVisibility(false);
}
DrawEngine::~DrawEngine()
{
//set cursor visibility to true
cursorVisibility(true);
}
int DrawEngine::createSprite(int index, char c)
{
if (index >= 0 && index < 16)
{
spriteImage[index] = c;
return index;
}
return -1;
}
void DrawEngine::deleteSprite(int index)
{
//in this implementation we don't need it
}
void DrawEngine::drawSprite(int index, int posx, int posy)
{
//go to the correct location
gotoxy(posx, posy);
//draw the image with cout
cout << spriteImage[index];
}
void DrawEngine::eraseSprite(int posx, int posy)
{
gotoxy(posx, posy);
cout << ' ';
}
void DrawEngine::setMap(char **data)
{
map = data;
}
void DrawEngine::createBackgroundTile(int index, char c)
{
if (index >= 0 && index < 16)
{
tileImage[index] = c;
}
}
void DrawEngine::drawBackground(void)
{
if (map)
{
for (int y = 0; y < screenHeight; y++)
{
goto(0, y); // This generates the error
for (int x = 0; x < screenWidth; x++)
{
cout << tileImage[map[x][y]];
}
}
}
}
void DrawEngine::gotoxy(int x, int y)
{
HANDLE output_handle;
COORD pos;
pos.X = x;
pos.Y = y;
output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(output_handle, pos);
}
void DrawEngine::cursorVisibility(bool visibility)
{
HANDLE output_handle;
CONSOLE_CURSOR_INFO cciInfo;
cciInfo.dwSize = sizeof(CONSOLE_CURSOR_INFO);
cciInfo.bVisible = visibility;
output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(output_handle, &cciInfo);
}

I think you meant to write gotoxy(0, y) instead of goto(0, y).
goto is a C++ keyword which jumps to a label, for example:
home:
goto home; // Loops forever
Don't use it, though, it's too easy to create spaghetti code.

The goto(0, y) should probably be gotoxy(0, y). goto is a reserved keyword in C and cannot be used as a function name.

I think you meant gotoxy. goto is something else entirely.

Related

A fix to not crash a map created with a multidimensional vector

I am trying to create a map where you as a player can move through the map using up,down,left & right keys and so far im in the right path. What bothers me is that everytime I reach an element outside its vector scope it errors me out saying
vector subscript out of range
and I know what this means so I'm figuring out a solution to solve this but how am I supposed to do this? Making an if statement telling that if it reaches a certain element it should not increment the value of moveY and just return false
This code below me does not work.. :/
bool Map::Player_Pos(float posY, float posX)
{
this->map[posY][posX] = '#';
if (GetAsyncKeyState(0x28))
{
if (moveY == 10)
{
return false;
}
this->map[posY][posX] = '.';
moveY += 1;
}
return true;
}
The problem lies at bool Player_Pos(float posY, float posX); so any suggestions would be really appreciated!
#include <iostream>
#include <vector>
#include <Windows.h>
class Map
{
private:
float moveY = 0.f;
float moveX = 0.f;
std::vector<std::vector<char>> map;
public:
Map(int dim1, int dim2, char type)
{
map.resize(dim1, std::vector<char>(dim2, type));
}
bool Update()
{
system("cls");
for (int i = 0; i < map.size(); i++)
{
for (int j = 0; j < map.size(); j++)
{
std::cout << map[i][j];
}
std::cout << '\n';
}
return true;
}
bool Player_Pos(float posY, float posX);
float posY() { return moveY; }
float posX() { return moveX; }
};
bool Map::Player_Pos(float posY, float posX)
{
this->map[posY][posX] = '#';
if (GetAsyncKeyState(0x28))
{
if (moveY == 10) // This does not work! Still keeps going and crashes compiler
{
return false;
}
this->map[posY][posX] = '.';
moveY += 1;
}
return true;
}
int main()
{
Map map = Map(10, 10, '.');
while (true)
{
map.Player_Pos(map.posY(), map.posX()); // Initialize position
map.Update(); // Update map
}
return 0;
}

C++ C4716 Beginner error. Understand the error conecpt but not sure how to make the program work

I'm currently working on a program for one of my classes that goes like so:
#include <iostream> // std::cout
using namespace std;
#include <windows.h> // SetConsoleCursorPosition(HANDLE,COORD)
#include <conio.h> // _getch()
struct Vector2
{
int x, y;
Vector2() :
x(0), y(0)
{}
Vector2(int x, int y)
{
x = x;
y = y;
}
bool is(int a_x, int a_y)
{
if (a_x == x, a_y==y)
{
return true;
}
return false;
}
};
class Entity //new data class
{
public:
Entity(int x,int y, char i)
{
pos.x = x;
pos.y = y;
icon = i;
}
void setX(int x)
{
pos.x = x;
}
int getX()
{
pos.x;
}
void setY(int y)
{
pos.y = y;
}
int getY()
{
pos.y;
}
void setIcon(char i)
{
icon = i;
}
char getIcon()
{
icon;
}
private:
Vector2 pos;
char icon;
};
enum Gamestate
{
RUNNING, WIN, LOST, USER_QUIT
};
void moveCursor(int x, int y)
{
COORD c = { x,y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int main()
{
// player data
Entity e(3, 4, 1);
//game data
Gamestate state = RUNNING;
int input;
Vector2 size(20, 15);
Vector2 winPosition(size.x / 2, size.y / 2);
do
{ //draw the game world
moveCursor(0, 0);
for (int row = 0; row < size.x; row++)
{
for (int col = 0; col < size.y; col++)
{
cout << '.';
} cout << '\n';
}
//draw player
moveCursor(e.getX(), e.getY());
cout << e.getIcon();
//get input from user
input = _getch();
//process the user's input
switch (input)
{
case 'w': //move up
e.setY(e.getY() - 1);
break;
case 'a': //move left
e.setX(e.getX() - 1);
break;
case 's': //move down
e.setY(e.getY() + 1);
break;
case 'd': //move right
e.setX(e.getX() + 1);
break;
case 27: //quit game
state = USER_QUIT;
break;
}
//show game state
moveCursor(0, size.y + 1);
switch (state)
{
case WIN:
cout << "You WON! Congratulations!\n" ;
break;
case LOST:
cout << "You lost...\n";
break;
}
if (winPosition.is(e.getX(), e.getY()))
{
state = WIN;
}
else
{
state = LOST;
}
} while (state == RUNNING);
//User input ESCAPE to quit program
cout << "Press ESCAPE to quit.\n";
while (_getch() != 27)
;
return 0;
}
The error occurs for Entity::getY,Entity::getIcon,Entity::getX
If I'm understanding the error correctly it's occurring because there are no values returned from main? But everything I try to do to fix it just leaves me with more errors then before.
You have a couple problems. In your get functions, you say you are supposed to return something, then never do. Another thing is in the constructor for struct Vector2 you say
Vector2(int x, int y)
{
x = x;
y = y;
}
But this doesn't change the member variables. You need to specify which x and y you are trying to change by either changing the names or using this.

Invalid use of incomplete type "class"

I know there has been tons of questions like that, but unfortunately after hours of googling and browsing through all of them, none of the answers I read helped. Therefore I am making my own version of my question. The error message I get is: "error: invalid use of incomplete type ‘std::iterator_traits::value_type {aka class Cell}’" My code:
cell.h
#ifndef CELL_H
#define CELL_H
#include <QPushButton>
#include <QMouseEvent>
#include <vector>
class Padding;
class Cell : public QPushButton
{
Q_OBJECT
public:
friend class Padding;
Cell(int x, int y, Padding* padding, QWidget* parent = 0) : QPushButton(parent), x(x), y(y),
padding(padding)
{
setFixedSize(20, 20);
}
Cell(const Cell& object) : QPushButton(), x(object.x), y(object.y), padding(object.padding)
{
setFixedSize(20, 20);
}
int getX() { return x; };
int getY() { return y; };
bool hasMine() { return mine; };
void setHasMine(bool mine) { this -> mine = mine; };
bool isFlagged() { return flagged; };
bool didExplode() { return exploded; };
bool getHasBeenClicked() { return hasBeenClicked; };
void clicked();
~Cell() {};
Cell operator=(const Cell& object)
{
if(&object == this)
{
return *this;
}
padding = object.padding;
x = object.x;
y = object.y;
mine = object.mine;
flagged = object.flagged;
exploded = object.exploded;
hasBeenClicked = object.hasBeenClicked;
setFixedSize(20, 20);
return *this;
}
private:
Padding* padding;
int x;
int y;
bool mine = false;
bool flagged = false;
bool exploded = false;
bool hasBeenClicked = false;
void mousePressEvent(QMouseEvent* e);
void rightClicked();
};
#endif // CELL_H
cell.cpp
#include "cell.h"
#include "padding.h"
void Cell::mousePressEvent(QMouseEvent* event)
{
if(event -> button() == Qt::LeftButton)
{
clicked();
}
else if(event -> button() == Qt::RightButton)
{
rightClicked();
}
}
void Cell::clicked()
{
hasBeenClicked = true;
// TODO: Set the button frame to flat. DONE.
setFlat(true);
// TODO: Make the button not click able. DONE.
setEnabled(false);
// TODO: Display appropriate number on the button, or mine and end the game. DONE.
if(mine)
{
// TODO: Send game over signal and end the game.
//setIcon(QIcon("mine_clicked.png"));
setText("/");
exploded = true;
padding -> gameOver();
}
else
{
setText(QString::number(padding -> countMinesAround(this)));
}
if(padding -> countMinesAround(this) == 0)
{
// Trigger chain reaction; uncover many neighboring cells, if they are not mines.
padding -> triggerChainReactionAround(this);
}
}
void Cell::rightClicked()
{
if(text() != "f")
{
setText("f");
(padding -> minesLeft)--;
}
else
{
setText("");
(padding -> minesLeft)++;
}
flagged = !flagged;
}
padding.h
#ifndef PADDING_H
#define PADDING_H
#include <QWidget>
#include <QGridLayout>
#include <vector>
class Cell;
class Padding : public QWidget
{
Q_OBJECT
public:
friend class Cell;
enum class Difficulty
{
Beginner,
Intermediate,
Advanced,
Custom
};
Padding(QWidget* parent = 0);
void newGame();
void gameOver();
void setLevel(Padding::Difficulty difficulty) { this -> difficulty = difficulty; };
void setPaddingHeight(int height) { paddingHeight = height; };
void setPaddingWidth(int width) { paddingWidth = width; };
void setMines(int mines) { this -> mines = mines; };
int getMinesLeft() { return minesLeft; };
~Padding() {};
private:
struct DifficultyLevelsProperties
{
struct BeginnerProperties
{
const int PADDING_HEIGHT = 9;
const int PADDING_WIDTH = 9;
const int MINES = 10;
} Beginner;
struct IntermediateProperties
{
const int PADDING_HEIGHT = 16;
const int PADDING_WIDTH = 16;
const int MINES = 40;
} Intermediate;
struct AdvancedProperties
{
const int PADDING_HEIGHT = 16;
const int PADDING_WIDTH = 40;
const int MINES = 99;
} Advanced;
} LevelProperties;
Difficulty difficulty = Difficulty::Beginner;
int paddingHeight;
int paddingWidth;
int mines;
// Mines that are not flagged.
int minesLeft;
// Time in seconds since the game was started.
int secondsSinceStart;
std::vector<Cell> cells;
QGridLayout* paddingLayout;
const int CELLS_HEIGHT = 20;
const int CELLS_WIDTH = 20;
int countMinesAround(Cell*);
void triggerChainReactionAround(Cell*);
void updateSecondsSinceStart();
};
#endif // PADDING_H
padding.cpp
#include "padding.h"
#include <QGridLayout>
#include <QTimer>
#include <QTime>
#include <QDebug>
#include "cell.h"
Padding::Padding(QWidget* parent) : QWidget(parent)
{
newGame();
paddingLayout = new QGridLayout(this);
paddingLayout -> setSpacing(0);
}
void Padding::newGame()
{
if(difficulty == Padding::Difficulty::Beginner)
{
paddingHeight = LevelProperties.Beginner.PADDING_HEIGHT;
paddingWidth = LevelProperties.Beginner.PADDING_WIDTH;
mines = LevelProperties.Beginner.MINES;
}
else if(difficulty == Padding::Difficulty::Intermediate)
{
paddingHeight = LevelProperties.Intermediate.PADDING_HEIGHT;
paddingWidth = LevelProperties.Intermediate.PADDING_WIDTH;
mines = LevelProperties.Intermediate.MINES;
}
else if(difficulty == Padding::Difficulty::Advanced)
{
paddingHeight = LevelProperties.Advanced.PADDING_HEIGHT;
paddingWidth = LevelProperties.Advanced.PADDING_WIDTH;
mines = LevelProperties.Advanced.MINES;
}
minesLeft = mines;
cells.clear();
for(int i = 0; i < paddingHeight; i++)
{
for(int j = 0; j < paddingWidth; j++)
{
// TODO: Use smart pointers instead of raw pointers.
Cell* cell = new Cell(j + 1, i + 1, this);
cells.push_back(*cell);
delete cell;
}
}
qsrand(QTime::currentTime().msec());
for(int i = 0; i < mines; i++)
{
// TODO: Fix the randomness of the numbers. DONE.
cells[qrand() % (paddingHeight * paddingWidth) + 1].setHasMine(true);
}
for(int i = 0; i < cells.size(); i++)
{
paddingLayout -> addWidget(&cells[i], cells[i].getY(), cells[i].getX());
}
}
void Padding::gameOver()
{
for(int i = 0; i < cells.size(); i++)
{
cells[i].setEnabled(false);
if((cells[i].hasMine()) && (!cells[i].getHasBeenClicked()))
{
cells[i].clicked();
}
}
}
int Padding::countMinesAround(Cell*)
{
int minesCounter = 0;
for(int i = 0; i < cells.size(); i++)
{
qDebug() << QString::number(cells[i].getX());
if(((x - cells[i].getX() == 0) || (x - cells[i].getX() == 1) || (x -
cells[i].getX() == -1)) && ((y - cells[i].getY() == 0) || (y -
cells[i].getY() == 1) || (y - cells[i].getY() == -1)) &&
(cells[i].hasMine()))
{
minesCounter++;
}
}
return minesCounter;
}
void Padding::triggerChainReactionAround(Cell*)
{
for(int i = 0; i < cells.size(); i++)
{
if(((x - cells[i].getX() == 0) || (x - cells[i].getX() == 1) || (x -
cells[i].getX() == -1)) && ((y - cells[i].getY() == 0) || (y -
cells[i].getY() == 1) || (y - cells[i].getY() == -1)) &&
(!cells[i].getHasBeenClicked()))
{
cells[i].clicked();
}
}
}
Sorry for how long the whole thing, but I could not shorten it as I can't locate what causes the error. Also please ignore any TODO's or any lines that are commented out and I forgot to delete them. Please help!
When you forward declare a type, you can only use pointers or references to that type objects, so this line in padding.h is pretty much not compiling:
std::vector<Cell> cells;
I suppose the compiler complaint comes from where it is trying to decide how to build/destroy a Cell object in a vector. To do that, it needs information about the type, generally from the type declaration (i.e. the header file).

Accessing object in array recreating object?

I am making a console game and I am making a map. The map is an array of vectors. The vectors contain the characters that I am printing to the console. My code:
"Window.h"
#include <string>
#include <vector>
class Row {
public:
std::vector<char> row;
int id;
Row();
void printRow();
void resetRow();
void insertStringIntoRow(std::string toInsert, int startIndex);
std::vector<char> getRow() {
return row;
}
};
class Window {
public:
void showMap();
void writeToMap(std::string stringToInsert, int rowNum, int startIndex);
void writeInRectangle(std::string stringToWrite, int rowNum, int startIndex);
void setCursorToPosition(int x, int y);
void resetMap();
Row getRowAt(int index);
};
void initColors();
void setWindow(Window windowToSet);
Window getGameWindow();
"Window.cpp"
#include "Window.h"
#include <iostream>
#include <Windows.h>
#include <WinBase.h>
#include <stdlib.h>
#include "color.h"
using namespace eku;
Row map[25];
//Class Window
void Window::showMap() {
setCursorToPosition(0, 0);
for (int i = 0; i < 25; i++) {
getRowAt(i).printRow();
}
}
void Window::writeToMap(std::string stringToInsert, int rowNum, int startIndex) {
Row r = getRowAt(rowNum);
r.insertStringIntoRow(stringToInsert, startIndex);
}
void Window::writeInRectangle(std::string stringToWrite, int rowNum, int startIndex) {
if (startIndex != 0) {
std::string topbar = "~";
for (int i = 0; i < stringToWrite.length() + 2; i++) {
topbar += ' ';
}
topbar += '^';
getRowAt(rowNum - 1).insertStringIntoRow(topbar, startIndex - 1);
}
std::string toInsert = "~ ^" + stringToWrite + "~ ^";
getRowAt(rowNum).insertStringIntoRow(toInsert, startIndex - 1);
if (startIndex != 25) {
std::string bottombar = "~";
for (int i = 0; i < stringToWrite.length() + 2; i++) {
bottombar += ' ';
}
bottombar += '^';
getRowAt(rowNum + 1).insertStringIntoRow(bottombar, startIndex - 1);
}
}
void Window::setCursorToPosition(int x, int y) {
HANDLE hOut;
COORD Position;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Position.X = x;
Position.Y = y;
SetConsoleCursorPosition(hOut, Position);
}
void Window::resetMap() {
for (Row row : map) {
row.resetRow();
}
}
Row Window::getRowAt(int index)
{
return map[index];
}
//Class Row
const char WHITEBACKCOL = '~';
const char DEFCOL = '^';
int i = 0;
Row::Row() {
row.resize(80, ' ');
id = i;
i++;
}
void Row::printRow() {
for (int i = 0; i < row.size(); i++) {
switch (row[i]) {
case WHITEBACKCOL:
setcolor(black, white);
case DEFCOL:
setcolor(white, black);
default:
std::cout << row[i];
}
}
}
void Row::resetRow() {
row.resize(80);
for (int i = 0; i < 80; i++) {
row[i] = ' ';
}
}
void Row::insertStringIntoRow(std::string toInsert, int startIndex) {
int endIndex = (startIndex + toInsert.length());
int stringPos = 0;
for (int i = startIndex; i < endIndex; i++) {
if (i < row.size() - 1) {
row.at(i) = toInsert[stringPos];
}
else {
row.push_back(toInsert[stringPos]);
}
stringPos++;
}
}
Window defWindow;
void initColors() {
concolinit();
setcolor(white, black);
}
void setWindow(Window windowToSet) {
defWindow = windowToSet;
}
Window getGameWindow() {
return defWindow;
}
"Main.cpp"
#include <iostream>
#include "GameEngine.h"
#include "Window.h"
int main() {
setWindow(Window());
initColors();
getGameWindow().writeInRectangle("Hia", 1, 10);
getGameWindow().showMap();
}
Whenever I call showMap() all I get is a blank console. It seems to be only printing the default map of spaces instead of the text I entered. I also tried using just printRow() to print the single rows that I edited but they also showed only spaces.
I was able view changes to the vector row in the insertStringIntoRow() method but then even though the changes should there they didn't show anywhere else. It almost seems like my Row object is being created every time I access it. I am new to C++ so any help is appreciated. Thanks in advance!
This function is being used incorrectly. Warning signs are that it is non-const, yet used as an accessor. It returns a copy of the vector, not a reference.
std::vector<char> getRow() {
return row;
}
The way you are calling it, you are expecting it to modify the row in-place, but all you are actually doing is modifying a copy of the row, which is then immediately discarded. Example:
getRowAt(rowNum).insertStringIntoRow(toInsert, startIndex - 1);
The easy fix for this behaviour is to return a reference from getRow():
std::vector<char> & getRow() {
return row;
}
The other thing to fix is where you assign this to a temporary variable instead of using it inline. In that case, you can make the temporary a reference:
Row & r = getRowAt(rowNum);
r.insertStringIntoRow(stringToInsert, startIndex);
The correct way to implement this is to also provide a const-version, so that it can still be called on const objects where the caller does not want to modify it.
const std::vector<char> & getRow() const {
return row;
}
I've only put this extra bit in here because it is good practice, but you should not do it in this program. It would clutter your code which is already full of worse practices =)
send a reference to writeToMap() instead of variables.
This will make changes to the original map objects instead of copy-variables.

Segfault when trying to access a boolean inside of a class

const int PIXEL_WIDTH = 10;
const int PIXEL_HEIGHT = 10;
const int WORLD_X = 64; //WORLD_X * PIXEL WIDTH = SCREEN_WIDTH if you want the world to be the same size as the screen
const int WORLD_Y = 64;
enum Pixel_Types {
AIR,
WALL,
DIRT,
STONE
};
class Pixel
{
int x, y;
bool affected_by_gravity;
Pixel_Types type;
public:
Pixel() : affected_by_gravity(false), type(AIR), x(0), y(0) {}
Pixel(int temp_x, int temp_y) : affected_by_gravity(false), type(AIR), x(temp_x), y(temp_y) {}
int getX() { return x; } //x is 0-63, scales up in the rendering code
int getY() { return y; } //y is 0-63, scales up in the rendering code
int getScreenX() { return x*PIXEL_WIDTH; } //x is 0-63, scales up in the rendering code
int getScreenY() { return y*PIXEL_HEIGHT; } //y is 0-63, scales up in the rendering code
bool setDeltaX(int temp_delta_x);
bool setDeltaY(int temp_delta_y);
void setAffectedByGravity(bool yesorno) { affected_by_gravity = yesorno; }
bool getAffectedByGravity() { return affected_by_gravity; }
Pixel_Types getType() { return type; }
void setType(Pixel_Types what_type) { type = what_type; }//if (type == DIRT or type == STONE) { affected_by_gravity = true; } }
};
std::vector<Pixel> world; //the world is a dynamically allocated thing
Pixel* getPixelFromCoordinates(int x, int y)
{
if (x > 63) x = 63;
else if (x < 0) x = 0;
if (y > 63) y = 63;
else if (y < 0) y = 0;
for (int pixel_index = 0; pixel_index < world.size(); pixel_index++) {
if (world.at(pixel_index).getX() == x && world.at(pixel_index).getY() == y) {
return &world.at(pixel_index);
}
}
return NULL;
}
bool Pixel::setDeltaX(int temp_delta_x) {
if (x+temp_delta_x > SCREEN_WIDTH/PIXEL_WIDTH or x+temp_delta_x < 0) {
return false;
}
if (getPixelFromCoordinates(x+temp_delta_x, y)->type == AIR) {
x += temp_delta_x;
return true;
}
return false;
}
bool Pixel::setDeltaY(int temp_delta_y) {
if (y+temp_delta_y > SCREEN_HEIGHT/PIXEL_HEIGHT or y+temp_delta_y < 0) {
return false;
}
if (getPixelFromCoordinates(x, y+temp_delta_y)->type == AIR) {
y += temp_delta_y;
return true;
}
return false;
}
void generateWorld()
{
for (int world_generation_index = 0; world_generation_index < 4096; world_generation_index++) {
int x = world_generation_index % WORLD_X; //the world is 64 pixels left and right, and 64 up and down. this math is pretty easy and just extrapolates that. also each pixel is 10 pixels across, times 64 pixels = 640 (the resolution)
int y = floor(world_generation_index / WORLD_Y); //both x and y start at 0
world.push_back(Pixel(x, y));
if (x == 0 || x == 63) {
world.at(world_generation_index).setType(WALL);
}
if (y == 1) {
world.at(world_generation_index).setType(WALL);
}
}
std::cout << "World size: " << world.size() << std::endl;
}
void createPixel(int x, int y, Pixel_Types type)
{
std::cout << x << std::endl;
std::cout << y << std::endl << std::endl;
y = (SCREEN_HEIGHT / PIXEL_HEIGHT) - y; //compensate for the stupid inverted y in opengl
//if (getPixelFromCoordinates(x, y)->getType() == AIR) {
getPixelFromCoordinates(x, y)->setType(type);
//}
}
void physicsOneStep()
{
for (int pixel_index = 0; pixel_index < world.size(); pixel_index++) {
if (world.at(pixel_index).getType() == DIRT or world.at(pixel_index).getType() == STONE) {//if (world.at(pixel_index).getAffectedByGravity()) {
world.at(pixel_index).setDeltaY(-1);
//std::cout << world.at(pixel_index).getX() << std::endl;
//std::cout << world.at(pixel_index).getY() << std::endl << std::endl;
}
}
}
So when I try to run this code (part of a larger project) it occasionally gives me a Segfault on calling setType(DIRT) from within createPixel(). I know that the values provided to createPixel() are within the range that they are allowed to be (0 to 64). It seems to segfault if you click (which calls createPixel()) in the same spot twice. The line that the debugger says segfaults is
void setType(Pixel_Types what_type) { type = what_type; }
though, I've verified that the values that I have supplied to this are correct.
Since there is no dynamic allocation inside the class, having a segfault on such an allocation most certainly occur because the this pointer itself is incorrect (NULL or badly allocated). You should get up the traceback when it segfaulted to see how the object on which you called setType was allocated. For example, shouldn't the line
world.push_back(Pixel(x, y));
be
world.push_back(new Pixel(x,y));
?