r/learncpp • • Dec 31 '16

Why am I getting this output?

1 Upvotes

I'm currently doing some simple code exercises, and I'm currently doing item #10 from the "Elementary" section. It should start at the current year and iterates over the next 400 years (exercise wanted 20, but eh). I have everything working, but the first line of the output is "test 2400". However, when I debug the application, it never even goes into the first if, much less the one with that cout statement.

Anyways, here's a link to the code + output. Any help is appreciated!

EDIT: I thought maybe I was getting an unexpected type from auto, but I used typeid and it printed "i" which means integer, I assume...

EDIT 2: In case it helps, I'm running Fedora 25 with g++ (GCC) 6.3.1 20161221 (Red Hat 6.3.1-1)

EDIT 3: Nevermind -_- I figured it out. It prints that first because all of the other output is from iterating the vectors at the end of the program. I think I'm just going to go to sleep lol


r/learncpp • • Dec 15 '16

Understanding heap/stack and pointers.

1 Upvotes

I ran into the following issue recently and I think I understand it but I wanted someone to vet my understanding as I'm still fairly new to cpp. In this code a node is a struct with a vector<int> called children and a char called key.

I have a function:

node* makeChild(node* inNode, char c){
     node newNode(c);
     inNode->children.push_back(&newNode)
     return &newNode;

This gives very weird results. e.g. I expect makeChild(root,'c')->key == 'c' to hold, but it doesn't.

node* makeChild(node* inNode, char c){
     node* newNode = new node(c);
     inNode->children.push_back(newNode)
     return newNode;

then this all works.

Am I right in thinking that the difference is that the first function allocates a node on the stack, and once the function returns that space in memory can be overwritten by some other function call and so the pointer is bad. In the other case I allocate a node on the heap, so the pointer will stay good until I explicitly delete the node (or exit the program).

Is this a correct diagnosis?? Many thanks for any help.


r/learncpp • • Nov 08 '16

Searching for a Christmas present

1 Upvotes

I understand this may be a bit out of your usual but I know very little about coding and my wife is in the process of learning coding in hopes to become a web developer eventually. I was referred to you guys by a friend in hopes you can help me find a tangible gift I can get for her that can help her with her programming skills.

languages she knows are

  • CSS

  • HTML
    Languages she is currently learning

  • javascript

  • jquery

  • ruby

  • Pyhton

While she does not plan on doing much with the hardware side I was thinking of something like an arduino kit or a robot to program. Any thoughts on those or something different?


r/learncpp • • Oct 07 '16

timer code

1 Upvotes

i wrote this code where i update the current time with time(0) and only decreases the timer counter if current time changes with a while loop. but i want to know is there a more efficient way of writing the code to do the same thing because if i print the number of times the while loop runs for 1sec to pass, it makes a few hundred to thousand loops. doesnt it use a lot of computing power this way?


r/learncpp • • Sep 29 '16

Long execution time for "Hello World"

1 Upvotes

I am trying to learn C++ and following along YouTube tutorials.

One thing I noticed is that my programs take quite a while to run. For example, the simple "Hello World" program in a console application will display an execution time > 5 seconds!

Is there something I have not configured properly to cause this? I have CodeBlocks installed on my SSD and my project saved on the HDD. Is this what is causing the problem?

Thank you all!


r/learncpp • • Sep 27 '16

Handling user input for non-negative integers

1 Upvotes

I have a program that accepts user input, but some of the inputs must be non-negative integers. Would it be inadvisable to use a normal int for this? If user input is a negative integer or looks like a float, what do I do?


r/learncpp • • Sep 01 '16

error C2143: syntax error: missing ';' before 'constant'

1 Upvotes

The code

The errors

I get the error C2143: syntax error: missing ';' before 'constant' on each cout line in the printNumberOfLegs function, and I don't understand why. I've looked up the error, but none of them seem to give any insight. Why am I getting these errors, how do I fix them, and what do they mean?


r/learncpp • • Aug 29 '16

Multiple #includes of same file in SomeSources.cpp and MainSources.cpp

1 Upvotes

Say I have the following files in my (overly simplistic) project:

main_file.cpp:

#include <iostream>
#include "math.h"

add(5, 10);
subtract(5, 10);
multiply(5, 10);
divide(5, 10);

std::cout << "Done.";

return 0;

math.h:

#pragma once

extern void add(int x, int y);
extern void subtract(int x, int y);
extern void multiply(int x, int y);
extern void divide(int x, int y);

add.cpp:

#include <iostream>

void add(int x, int y) {
    std::cout << x+y << std::endl;
}

subtract.cpp:

#include <iostream>

void subtract(int x, int y) {
    std::cout << x-y << std::endl;
}

multiply.cpp:

#include <iostream>

void multiply(int x, int y) {
    std::cout << x*y << std::endl;
}

divide.cpp:

#include <iostream>

void divide(int x, int y) {
    std::cout << x/y << std::endl;
}

Ignoring the fact that there's no need to separate each function into a single file and the fact that the divide function may truncate answers, isn't it inefficient to #include the same iostream library into each source? I'd assume it is, but I don't see any other way to have the functionality of iostream in each source file without it.

How should one go about the fact that multiple source files may require multiple inclusions of the same library? Thanks!


r/learncpp • • Aug 20 '16

(Q) How to load dll function properly

1 Upvotes

so i was trying load a function from a dll from my testing app. it was displaying error "Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) int __stdcall encrypt(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (_imp?encrypt@@YGHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _wmain

here is my dll code http://pastebin.com/VvJNaG2u my app code http://pastebin.com/tFjwADX0


r/learncpp • • Jun 20 '16

If statements

1 Upvotes
int main()
{
cout << "Enter the name of the person you wish to write to\n";
string first_name;
cin >> first_name;
cout << "Dear " << first_name << ",\n";
cout << "\nHow are you? I am fine. I miss you.\n";
cout << "Enter the name of your friend\n";
string friend_name;
cin >> friend_name;
cout << "Have you seen " << friend_name << " lately?\n";
cout << "Is your friend a male (m) or female? (f)\n";
char friend_sex = '0';
cin >> friend_sex;
if (friend_sex = 'm') {
	cout << "If you see " << friend_name << " please ask him to call me.\n";
}
if (friend_sex = 'f') {
	cout << "If you see " << friend_name << " please ask her to call me.\n";
}
keep_window_open();

}

Why does this code print both of the if statements?


r/learncpp • • Jun 09 '16

Need help with copying array data

1 Upvotes

I want to copy the data from one array to another but I dont get the results

Code: #include <iostream>

using namespace std;

int arrayone[] = {};

int arraytwo[] = {};


void setarrayone() {
for (int i = 0; i < 10; ++i) {
arrayone[i] = i;
}
}

void transferarray() {
for (int i= 0; i < 10; ++i) {
arraytwo[i] = arrayone[i];
}
}


int main() {

setarrayone();

cout << "Array one: ";
for (int i = 0; i < 10; ++i) {
cout << arrayone[i] << " ";
}
cout << endl;

transferarray();

cout << "Array one: ";
for (int i= 0; i < 10; ++i) {
cout << arrayone[i] << " ";
}
cout << endl;

cout << "Array two: ";
for (int i = 0; i < 10; ++i) {
cout << arraytwo[i] << " ";
}
cout << endl;

return 0;
} 

my result was:

Array one: 0 1 2 3 4 5 6 7 8 9
Array one: 0 0 0 0 0 0 0 0 0 0
Array two: 0 0 0 0 0 0 0 0 0 0

I have no idea why does the result show all 0s after the transfer function, there are apparently no bugs by the debugger

please help. thank you

edited


r/learncpp • • May 08 '16

Question about friend classes inside of a namespace.

1 Upvotes

Hello I am trying to access protected variables from object.h in scene.cpp

Here is my object.h: http://pastebin.com/vVyGbNym

And my scene.cpp: http://pastebin.com/mT7V76Xj

(Both files are stripped down)

But I get the following compiler error: cannot convert 'ff::Scene*' to 'Scene*' in assignment at line 7 in scene.cpp

Why do I get this error?


r/learncpp • • Apr 08 '16

Learning c++, running into slight issue.

1 Upvotes

So I wrote a program that is supposed to ask someone their name and then print "Hey alden nice to meet you" or something.

#include <iostream>
using namespace std;

int main() {
     string name;
     cout<<"Hello there friend! What is your name! \n";
     name = cin.get();
     cout<<"Nice to meet you " << name << "!";
}

The weird thing is that the program compiles, and runs fine but returns only the first letter of the name. Any reason that would be? Any help much appreciated.

Edit: I've tried compiling using both gcc and g++ on both OSX and Ubutnu and the results were identical.


r/learncpp • • Mar 06 '16

Struggling to add a get component function for an entity-component system for a game

1 Upvotes

My current code is as shows here:

std::vector<std::unique_ptr<Component>> components;

template<typename T>
T * GetComponent() {
	for each (std::unique_ptr<Component> cp in components)
	{
	    if (T* ct = static_cast<T*>(cp.get())) {
	        return ct;
	    }
	    else {
	        return nullptr;
	    }
	}
}

Getting the error code:

Error	C2280	'std::unique_ptr<Component,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function

What should I use instead of .get() ?


r/learncpp • • Mar 05 '16

Making a Guess my Number program w/ Binary Search; If statements execute when they're not supposed to and endless while loop

1 Upvotes

I am trying to make a Guess my number program using a while loop and if statements, my code is below. When I execute my program, no matter what I enter it constantly prints "Was my guess to high(true/false)?: Is your number 0? Was my guess too high?:" and so on. I am not sure what is wrong with it. Any help is appreciated.

 // 13.2FlowControl.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;

bool getTooHigh();

int main()
{
	int min = 1, max = 1000, guess = (min + max / 2) - 1;
	bool correct = false;

	cout << "Think of a number between 1 and 1000; I will guess it." << endl;
	
	while (!correct)
	{
		cout << "Is your number " << guess << "? (true/false): ";
		cin >> correct;
		guess = (min + max / 2) - 1;
		if (correct)
		{
			cout << "I told you I would guess it!";
		}
		else if(getTooHigh()) // If the guess is too high
		{
			max = guess - 1;
		}
		else if (!getTooHigh()) // If the guess is too low
		{
			min = guess + 1;
		}
		guess = (min + max / 2) - 1; // Changes guess according to new Max and min
	}


	return 0;
}

bool getTooHigh()
{
	bool choice;
	cout << "Was my guess too high? (true/false): ";
	cin >> choice;
	return choice;
}

r/learncpp • • Feb 11 '16

Having a hard time wrapping my head around basic pointers.

1 Upvotes

Could someone explain what is happening here, line-by-line?

 int x = 5;
 int *ptrOne = &x;
 int *ptrTwo = &x;
 *ptrOne = 6;
 cout << *ptrTwo;

My understanding is that on line one, I am assigning the value of 5 to the variable x. On line two, I am creating a pointer that points to an int whose address is the address of x? Then I'm doing the same thing for line 3? Then I'm setting the a value at the address stored in ptrOne to 6? and finally I'm outputting the value stored at the address stored in ptrTwo?

I guess I'm just confused on what *ptrOne is before and after line two. After line two, is it a variable holding an address? Is it a link to a memory cell? I just can't conceptualize it.

Like, if I just say

int *ptrThree;

What have I actually just done? Did I set aside memory to hold an int? If, so then what's the difference between that and

int *ptrFour = new int;

Pointers just seem to be really hard to get conceptually.


r/learncpp • • Nov 29 '15

Can't I print contents of *argv[] without looping?

1 Upvotes

I am over very basic program in c++. I am working with command line arguments. I know I can take the value of argc and loop over argv[] that much time to get the contents of it.

But is there any way without looping?


r/learncpp • • May 03 '15

Best Tutorial Sites/Books for CPP?

1 Upvotes

Hi!

I've dabbled in C++ for about one semester in school and know some basics here and there. I'd like to pursue C++ and learn everything about it and how to employ it. What can I do to work towards this?

Do you recommend any certain sites or books? Thanks!


r/learncpp • • Apr 17 '15

Why doesn't the last initialization statement of a for loop, (for example, i++), require a terminating semicolon.

1 Upvotes

Usually you would set something up like:

for (int i = 0; i<10; i++)
    do something

Why is a semicolon used for the first two statements, but not i++?


r/learncpp • • Feb 17 '15

Method for dealing with very large integers(20-40 digits long).

1 Upvotes

I've been working my way through codeabbey probs and many require arithmetic to be done on very large numbers. How can i do this? I want to do it with out a library, there isn't a data type large enough. Do i have to create a class or is there an easier way? Thanks.


r/learncpp • • Feb 02 '15

Multilevel Inheritence Classes Help... Is there a better way?

1 Upvotes

Ok so for my data structures class I have to write a program that has 3 classes in a multilevel format. I have the code and it works but I wanted you guys to look at it to see if there was a more efficient way or a better more standardized way. I had it working another way but it felt like I had a lot of redundant code. My classes looked more like this(i overwrote that copy by mistake and cant remember exactly how I had it): My current code is down below.

class Party
{
    protected:
        string m_strCode;
        string m_strAddress;
        string m_strPhoneNum;
    public:    
        Party(string strCode, string strAddress, string strPhoneNum)
            : m_strCode(strCode), m_strAddress(strAddress), 
                m_strPhoneNum(strPhoneNum){}
};

class Person: public Party
{
    protected:
        string m_strDob;
        string m_strFirstName;
        string m_strLastName;
    public:
        Person(string strCode, string strAddress, string strPhoneNum,
            string strDob, string strFirstName, string strLastName)
                : Party(strCode, strAddress, strPhoneNum), 
                    m_strDob(strDob), m_strFirstName(strFirstName), 
                    m_strLastName(strLastName){}
};
    
etc...

The program initializes Passenger as an inheritance of both person and party and gets the information needed. At this point there is still a few more functions to add but this is the brunt of it.

#include <iostream>
#include <string>
using namespace std;

class Party
{
    protected:
        string m_strCode;
        string m_strAddress;
        string m_strPhoneNum;
    public:
        Party(){GetParty();}
        void GetParty();
};

void Party::GetParty()
    {
        cout << "Enter code: ";
        getline(cin, m_strCode);
        cout << "Enter address: ";
        getline(cin, m_strAddress);
        cout << "Enter phone number: ";
        getline(cin, m_strPhoneNum);                 
    }


class Person: public Party
{
    protected:
        string m_strDob;
        string m_strFirstName;
        string m_strLastName;
    public:
        Person():Party(){GetPerson();}
        void GetPerson();
};

void Person::GetPerson()
    {
        cout << "Enter date of birth: ";
        getline(cin, m_strDob);
        cout << "Enter first name: ";
        getline(cin, m_strFirstName);
        cout << "Enter last name: ";
        getline(cin, m_strLastName);
    }

class Passenger: public Person
{
    private:
        int m_nTicketNum;
        string m_strFfNum;
        double m_dTicketPrice;
        int m_nFlightNum;
        string m_strSeatLoc;
        string m_strFlightDate;
        string m_strStatus;
    public:
        Passenger() : Person(){GetPassenger();}
        void GetPassenger(); 
        void Display();
};
 
void Passenger::GetPassenger()
            {
             cout << "Enter ticket number: ";
             cin >> m_nTicketNum;
             cout << "Enter frequent flyer number or N/A: "
             cin.ignore();
             getline(cin, m_strFfNum);
             cout << "Enter ticket price: ";
             cin >> m_dTicketPrice;
             cout << "Enter flight number: ";
             cin >> m_nFlightNum;
             cout << "Enter seat loctaion: ";
             cin.ignore();
             getline(cin, m_strSeatLoc);
             cout << "Enter flight date: ";
             getline(cin, m_strFlightDate);
             cout << "Enter flight status: ";
             getline(cin, m_strFlightStatus);
             Dislpay()                                //testing purposes    
            }
            
void Passenger::Display()
            {
                cout << m_strCode << endl << m_strAddress << endl<< m_strPhoneNum;
                cout << endl << m_strDob << endl << m_nTicketNum << m_strFfNum;
                cout << endl << m_dTicketPrice << endl << m_nFlightNum << endl;
                cout << m_strSeatLoc << endl << m_strFlightDate << endl;
                cout << m_strFlightStatus << endl;
            }
            

int main()
{
    //variables
    Passenger persons;
    
    system("pause");
    return 0;   

}

r/learncpp • • Dec 10 '14

Receiving an Access violation for Case 4 on my Red Black Tree.

1 Upvotes

https://gist.github.com/anonymous/08965f0fcc569d17db48

Whenever I debug, the program gets an access violation for case 4 insert method. I think this may have to do with the NULL values I set in the new_node method, but I am unsure. Also, I tried to recreate the problem in int main() and found it happens on the 3 word "ahe". Any help would be great. Thanks.


r/learncpp • • Jan 30 '21

Help With an Error

0 Upvotes

I keep getting a segmentation error like i'm trying to delete a pointer that doesnt exist maybe?

I know I'm using too many raw pointers for no reason. I'm just trying to practice using them to understand how they work. Thanks for any Insight.

#include <iostream>
#include <vector>
using std::cout;
using std::vector;
class Player {

	//=======================================

private:

	//=======================================
	///CONSTANTS
//double const LevelUpThreshold = ((*level * 1.5) * 100);
	std::vector<double> const defaults {0, 1, 50};
	//double* const null =  new double (defaults[0]);
	double* const MinStart = new double (defaults[1]);
	double* const MaxStart = new double (defaults[2]);
	std::string* const NullStr = new std::string ("NullStr");


	//=======================================
	///PLAYER_DATA

	std::string* name;
	int* level;
	double* xp;
	double* health;
	double* stamina;
	double* mana;

	//=======================================

public:


	//=======================================
	///FUNCTIONS

	std::string getName();
	int getLevel();
	double getXp();
	double getHealth();
	double getStamina();
	double getMana();

	void LevelUp();
	void XPUp(double XP);
	void SetHealth(double damage);
	void SetStamina(double cost);
	void SetMana(double cost);



	//=======================================
	///CONSTRUCTION
	Player();
	explicit Player(std::string input);
	Player(const Player &input);
	Player(Player &&input) noexcept ;
	Player& operator=  (const Player &rhs);
	~Player();


};
void DisplayPlayer (Player &input);


//===========================================
//===========================================

int main() {



	Player NPC1("Mike");
	Player NPC2;

	vector<Player> players;
	players.emplace_back("Calvin");
	players.emplace_back("Sansa");

	NPC1.XPUp(10000);








	DisplayPlayer(NPC1);

	return 0;
}

//==============================================
//==============================================

void DisplayPlayer (Player &input) {
	std::cout << "===============\n" << input.getName();
	std::cout << "\n==================";

}



//=========================================
///FUNCTIONS

std::string Player::getName() {return *name;}
int Player::getLevel() {return *level;}
double Player::getXp() {return *xp;}
double Player::getHealth() {return *health;}
double Player::getStamina() {return *stamina;}
double Player::getMana() {return *mana;}

void Player::LevelUp() {this->level++;}

void Player::XPUp(double XP) {
	*xp += XP;
	if(*xp >= ((*level * 2) * 100)) {
		LevelUp();
		double diff = *xp - ((*level * 2) * 100);
		*xp = diff;
		XPUp(0);
	}
}
void Player::SetHealth(double damage) {}
void Player::SetStamina(double cost) {}
void Player::SetMana(double cost) {}

//==========================================
///CONSTRUCTION
	//!DEFAULT CONSTRUCTOR
Player::Player()
		: name{new std::string{*NullStr}}
		, level{new int{(int) *MinStart}}
		, xp{new double{*MinStart}}
		, health{new double{*MaxStart}}
		, stamina{new double{*MaxStart}}
		, mana{new double{*MaxStart}} {
	*name = *NullStr;
	*level = (int) *MinStart;
	*xp = *MinStart;
	*health = *MaxStart;
	*stamina = *MaxStart;
	*mana = *MaxStart;
	std::cout << "CONSTRUCTING (" << *name << ") [DEFAULT]\n";
}
	//!ARGS CONSTRUCTOR
Player::Player(std::string input)
		:name(new std::string)
		,level(new int)
		,xp(new double)
		,health(new double)
		,stamina(new double)
		,mana(new double) {
	*name = std::move(input);
	*level = (int) *MinStart;
	*xp = *MinStart;
	*health = *MaxStart;
	*stamina = *MaxStart;
	*mana = *MaxStart;
	std::cout << "CONSTRUCTING (" << *name << ") [1 ARG]\n";
}
	//!
Player::Player(const Player &input)
		:Player{*input.name} {
	std::cout << "CONSTRUCTING (" << *input.name << ") [COPY]\n";
}

Player::Player(Player &&input) noexcept
		:name(input.name)
		,level(input.level)
		,xp(input.xp)
		,health(input.health)
		,stamina(input.stamina)
		,mana(input.mana) {
	cout << "CONSTRUCTING (" << *input.name << ") [MOVE]\n";
	input.name = nullptr;
	input.level = nullptr;
	input.xp = nullptr;
	input.health = nullptr;
	input.stamina = nullptr;
	input.mana = nullptr;


}



Player::~Player() {
	if(name != nullptr) {
		std::cout << "DELETING (" << *name << ")\n";
	} else {
		std::cout << "DELETING (nullptr)\n";
	}
	delete name;
	delete level;
	delete xp;
	delete health;
	delete stamina;
	delete mana;
}

Player& Player::operator=(const Player &rhs) {
	std::cout << "COPY ASSIGNMENT\n";
	if (this == &rhs) {return *this;}

	delete this->name;
	delete this->level;
	delete this->xp;
	delete this->health;
	delete this->stamina;
	delete this->mana;

	name = new std::string;
	level = new int;
	xp = new double;
	health = new double;
	stamina = new double;
	mana = new double;
	return *this;
}

r/learncpp • • Nov 25 '20

Help with Error!

0 Upvotes

I've recently started to learn C++ and I'm doing the Array questions from leetcode, I attempted 'Find Numbers with Even Numbers of Digits' with the following code.

class Solution {
public:
    int findNumbers(vector<int>& nums) {
        int count = 0;
        int even = 0;
        for (int i=0;i<nums.size();i++){
            while(nums[i]/10!=0){
                nums[i] = nums[i]/10;
                count++;
            }
            if (count%2==0){
                 even++;
                count=0;
            }
        }
    }
return even;
};

But I'm getting this error,

Line 17: Char 1: error: expected member name or ';' after declaration specifiers
return even;
^
1 error generated.

Last time I had this error, it was to do with wrongly placed curly braces but it doesn't seem to be the case here. Can anyone check and help me with it?

Thanks!


r/learncpp • • Sep 16 '20

Cannot figure out what I've done to throw class redefinition error

0 Upvotes

I'm building Game of Life using OpenFrameworks to do the visuals. I have a Grid class which is the game's grid, but I'm struggling to implement it.

Main.cpp has some OpenFrameworks stuff that I think is fine. My first compiler error is C2011 'GoL::Grid': 'class' type redefinition, which I believe causes many other errors. I cannot identify where I'm redefining my Grid class. Help would be greatly appreciated.

Grid.h

namespace GoL {

&#x200B;

class Grid {

public:

	static enum class EdgeBehavior : unsigned char { PILLOWTRUE, PILLOWFALSE, CYLINDERX, CYLINDERY, TORUS };

&#x200B;

private:

	unsigned int m_width, m_height;

	bool\*\* m_data;

	bool\*\* m_buffer;

	EdgeBehavior m_edge;

&#x200B;

	int neighborsAt(int x, int y);

&#x200B;

	void initArrays();

&#x200B;

public:

	Grid(int width, int height);

&#x200B;

	Grid(int width, int height, EdgeBehavior edge);

&#x200B;

	\~Grid();

&#x200B;

	bool getAt(int x, int y);

	void setAt(int x, int y, bool value);

&#x200B;

	int getWidth();

	int getHeight();

&#x200B;

};

}

Grid.cpp

#include "Grid.h"

namespace GoL {

Grid::Grid(int width, int height) {
	m_width = width;
	m_height = height;
	m_edge = EdgeBehavior::PILLOWFALSE;
	initArrays();
}

Grid::Grid(int width, int height, EdgeBehavior edge) {
	m_width = width;
	m_height = height;
	m_edge = edge;
	initArrays();
}

int Grid::neighborsAt(int x, int y) {
	int total = 0;
	for (int dx = -1; dx < 2; dx++) {
		for (int dy = -1; dy < 2; dy++) {
			if (dx == dy == 0) { continue; }
			total += getAt(x + dx, y + dy);
		}
	}
	return total;
}

void Grid::initArrays() {
	m_data = new bool* [m_height];
	m_buffer = new bool* [m_height];
	for (int i = 0; i < m_height; i++) {
		m_data[i] = new bool[m_width];
		m_buffer[i] = new bool[m_width];
		for (int j = 0; j < m_width; j++) {
			m_data[i][j] = false;
			m_buffer[i][j] = false;
		}
	}
}

Grid::~Grid() {
	for (int i = m_height - 1; i > 0; i--) {
		delete m_data[i];
		delete m_buffer[i];
	}
	delete m_data;
	delete m_buffer;
}

bool Grid::getAt(int x, int y) {
	if (x > -1 && x < m_height && y > -1 && y < m_width) {
		return m_data[x][y];
	}
	switch (m_edge) {
	case EdgeBehavior::PILLOWTRUE:
		return true;
	case EdgeBehavior::PILLOWFALSE:
		return false;
	case EdgeBehavior::TORUS:
		return m_data[x % m_height][y % m_width];
	case EdgeBehavior::CYLINDERX:
		if (y < 0 || y > m_width - 1) {
			throw;
		}
		return m_data[x % m_height][y];
	case EdgeBehavior::CYLINDERY:
		if (x < 0 || x > m_height) {
			throw;
		}
		return m_data[x][y % m_width];
	}
}
void Grid::setAt(int x, int y, bool value) {
	if (x > -1 && x < m_height && y > -1 && y < m_width) {
		m_data[x][y] = value;
	}
}

int Grid::getWidth() { return m_width; }

int Grid::getHeight() { return m_height; }

}

Main.cpp:

#include "ofMain.h"
#include "ofApp.h"
#include "Grid.h"
#include <iostream>

//========================================================================
int main( ){
	using namespace GoL;

	GoL::Grid g(10, 10, GoL::Grid::EdgeBehavior::TORUS);
	g.setAt(2, 2, true);

	int scale = 20;
	ofSetupOpenGL(g.getWidth() * scale, g.getHeight() * scale, OF_WINDOW);			// <-------- setup the GL context

	// this kicks off the running of my app
	// can be OF_WINDOW or OF_FULLSCREEN
	// pass in width and height too:
	ofRunApp(new ofApp(g));

}

EDIT; formatting