C++ :: Constructor Not Initializing Array?

Mar 6, 2015

I am making a tictactoe program that requires me to have a 3 by 3 two dimensional array of integers, in which the constructor should initialize the empty board to all zeroes. The program complies, but it keeps outputting garbage values and i'm not entirely sure why, My print function isn't complete yet, since I want to print out the array in a tic tac toe format, but i'm more concerned with why it's printing garbage values, here is my code:

Code:

// header file
#include <iostream>
#include <array>

[Code] ....

View 7 Replies


ADVERTISEMENT

Visual C++ :: Initializing Array In Constructor?

Mar 8, 2015

I am making a tic tac toe program in which they are asking me to have a 3x3 2 dimensional array of integers and have the constructor initialize the empty board to all zeros. They also want me to place a 1 or 2 in each empty board space denoting the place where player 1 or player 2 would move The problem I'm having is, my code initializes the board to all zeros for each element of the array, and prints it out just fine, but I can't figure out how to re-initialize the values in the array to show where each player moves on the board... I was thinking about having a default constructor that has each value set to zero, and then use a setGame function that can change the values on the board to one or two depending on where the player moves....but I don't know if that's possible.....

here is my code

Code:

// header file
#include <iostream>
#include <array>
class tictac {
public:
tictac(int);

[code]....

View 8 Replies View Related

C++ :: Initializing Object Through Constructor

Oct 15, 2013

sth is in my mind.

fast way of initializing an object through constructor is

Fred::Fred() : x_(whatever) { }

slow way
Fred::Fred() { x_ = whatever; }

ok. i accept it is faster but why?? what is the difference compiler does.

View 6 Replies View Related

C/C++ :: Initializing Constructor In CPropertyPage?

Aug 21, 2013

Here is a declaration of property class
class CMyPropertyPage11 : public CPropertyPage

Here is the definition
CMyPropertyPage11::CMyPropertyPage11() : CPropertyPage(CMyPropertyPage11::IDD)

I really do not understand the role of the
CPropertyPage(CMyPropertyPage11::IDD)

But I need to derive CMyPropertyPage11 from another CPropertyPage, say CBasePropertyPage adn really need to know how to implement the CPropertyPage(CMyPropertyPage11::IDD) initialization again.

View 3 Replies View Related

C++ :: Initializing Map - No Matching Constructor For Initialization

Nov 18, 2014

I am trying to create a `std::map` from `std:: string` to a pointer to member function type. I'm initializing the `map` with an initializer list (braced). When I compile, I get a message from the compiler: No matching constructor for initialization.

Example: [URL] .....

View 4 Replies View Related

C++ ::  Initializing Const Char Member Variable In Constructor?

Jan 23, 2015

I have a class that defines a window (a popup dialog of sorts), and I want the name of that window to be constant. The only problem is that the name of the popup needs to match the title of the parent window, and I get the name of the parent in the constructor. So how do I go about defining this member variable to be constant and initializing it with a value in the constructor?

I want to do something like this, but I know this isn't allowed:

/* class.h */
class foo {
public:
foo(*parentWindowPtr);

[Code] .....

I should mention that yes the name of the parent window is const char *, and I would like to keep it this way.

View 4 Replies View Related

C++ :: Initializing Base Class Part From Derived Object Using Copy Constructor

Feb 20, 2012

class Base
{
char * ptr;
public:
Base(){}
Base(char * str)

[code].....

Obj1 is a derived class object where base class char pointer is initialized with "singh" and derived class char pointer is initilized with "sunil". I want to create Obj2 out of Obj1. Separate memory should be created for Obj2 char pointer (base part and derived part as well) and that should be initialized with the strings contained in Obj1.

Here the problem is: Derived class part can be initialized with copy constructor. How to initialize the base class char poniter of Obj2 with the base class part of Obj1. char pointers in
both the classes are private.

I tried using initializer list but could not succeed.

Is there some proper way to do this?

View 4 Replies View Related

C++ :: Initializing Inner-objects Of Base Class From Driven-class Constructor

Jan 6, 2015

Let's say I have a Car object , and it contains inner Engine object.

Code:
struct Car{
Engine mEngine;
};

In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:

Code:
Car::Car:
mEngin(arg1,arg2,...)
{
other stuff here
}

Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?

Code:
class Toyota:
Car(...),
mEngine(...),
mGear(..)
{
...
};

The other options are:
1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive
2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .

View 5 Replies View Related

C/C++ :: Initializing Array Of Structures

Feb 25, 2014

How to initialize this array of structures in my code

struct CustomerAddress; {
string street;
string city;
string state;
int zip;

[Code] ....

I am going to be using a boolean variable to mark whether or not a specific field has had data entered into it. I figure the best way to do that is to initialize all the elements of the structures to 0. However, with strings and with the nested structure, I'm not sure how to do this.

View 6 Replies View Related

C++ :: Initializing Double Pointer Array?

Aug 1, 2013

How to initialize double pointer array..?

int (**aLines)[2];

View 3 Replies View Related

C++ :: Initializing 2D Dynamic Array With All Entries 0?

Dec 29, 2013

is it possible to create a dynamic array with all entries in it equal to 0?

int** adjMatrix;
adjMatrix= new int*[numOfVertices];
for(int i=0; i<numOfVertices; i++){
adjMatrix[i]=new int[numOfVertices];
}

View 1 Replies View Related

C++ :: Initializing Local One Dimension Array?

Jul 31, 2013

Want to initialize a local one dimensional array. How can I do the same without a loop?

Found from some links that
int iArrayValue[25]={0};
will initialize all the elements to ZERO. Is it?
If yes then can we write below code to initialize the elements to a non-ZERO value?

int iArrayValue[25]={4};

Do we have some other way to initialize an array to a non-ZERO value? Memset can initialize the element to ZERO.

View 7 Replies View Related

C++ :: Initializing Multidimensional String Array?

Nov 15, 2014

In one of my programs I have a 3x3 array of string that I use to display the outcome to rock, paper, scissors, and another 1x3 used for a number guessing game. I have the arrays declared in the header file as follows:

//Games.h
std::string rpsOutcome[3][3];
std::string hiLoOutcome[3];

and then initialized them in the cpp file:

//Games.cpp
string rpsOutcome[3][3] {
//row 1
{ "Both of you picked rock, you tied. Try again",
"You picked rock, the computer picked paper, you lose",

[code]....

From what I've read, Im pretty sure thats how your supposed to initialize multidimensional arrays (using the nested braces), but when I build the project, I get the following error:

123456789101112
1
1> RockPaperScissors.cpp
1> Games.cpp
1>c:userswuubbgoogle drivevisual studio projectsgamesgamesgames.cpp(75): error C2374: 'games::rpsOutcome' : redefinition; multiple initialization

[Code] .....

View 4 Replies View Related

C++ :: Crash When Initializing Indices Array

Nov 26, 2012

Why I get a crash when initializing the indices array below (bottom)-

Code:
struct TerrainGroupData {
TerrainGroupData(){};
TerrainGroupData(Ogre::Vector3 ** iVertices, unsigned long ** iIndices, size_t* iFaceNum, size_t* iVertNum) {
vertices = iVertices;
indices = iIndices;

[Code] ...

View 9 Replies View Related

C++ :: Initializing Array Of Char Pointers Directly

Nov 24, 2014

I'm learning OpenGL using the C API and some of the functions' argument types have proven a bit challenging to me.

One example is the function Code: glShaderSource(GLuint shader, GLsizei count, GLchar const** string, GLint const* length); It resides in foo() which receives a vector "data" from elsewhere Code: void foo(std::vector<std::pair<GLenum, GLchar const*>> const& data); To pass the pair's second element to glShaderSource's third argument, I do the following:

Code:

GLchar const* const source[] = {data[0].second};
glShaderSource(..., ..., source, ...);

Now I have two questions regarding this:

1. Can I initialize a char const** via initialization list, the way I do a char const*?

Code:

// this works
std::vector<std::pair<GLenum, GLchar const*>> const shader_sources = {
{GL_VERTEX_SHADER, "sourcecode"},
{GL_FRAGMENT_SHADER, "sourcecode"}
};
// but is this possible?

std::vector<std::pair<GLenum, GLchar const**>> = { ??? };

2. Is there an alternative to creating a temporary GLchar**, even though that's specifically what the function wants?

View 2 Replies View Related

C :: Declaring And Initializing 3000 Number Array?

Oct 23, 2013

Here are the steps to this program:

1. Declare an array that will hold 3000 numbers
2. Initialize this array by assigning a random number to each element in the array
3. Traverse the array, modifying the current contents of each element in the array so that each value now lies between -3000 and 3000 inclusive
4. Traverse the array to compute the average value of all elements

I have never worked with arrays before and am lost!

View 2 Replies View Related

C++ :: Initializing N-dimensional Array With A List Of Components

Nov 7, 2014

InitializeNArray<4, bool, NARRAY>()(narray, {1,2,3,2}, true);

is supposed to initialize a 4-dimensional std::array a with a[1][2][3][2] = true.

The commented-out line, which is the desired recursion, does not compile for some reason, and the problem third parameter cannot be deduced. So I placed some temporary lines to work in the special case only. Howw to make that recursion work?

#include <iostream>
#include <array>
#include <vector>
#include <memory>
template <typename, int...> struct NArray;

[Code] ....

Incidentally, this is a sub-problem within the task of initializing more generally with

template <typename T, int... N, typename... ARGS>
typename NArray<T, N...>::type NDimensionalArray (const T& value, ARGS&&... args)

Once this subproblem is fixed, then the bigger task is fixed too.

View 7 Replies View Related

C++ :: Initializing Array Of String Type In A Class

Apr 28, 2013

I want to use this array as part of my class. I have tried several different angles trying to get it to work but with out success. I have been checking to see if it works by simply using "cout << dayName[3];" It is printing nothing at all. What is the proper way to initialize this array of strings?

First I tried this:
const string dayName[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

then I tried this:
const string dayName[7];
dayName[0] = "Sunday";
dayName[1] = "Monday";
dayName[2] ="Tuesday";
dayName[3] ="Wednesday";
dayName[4] ="Thursday";
dayName[5] ="Friday";
dayName[6] ="Saturday";

My implemetation file code and my header file code is here (header first):

//dayType.h, the specification file for the class dayType
#include <iostream>
#include <string>
using namespace std;
class dayType{

[Code] .....

View 4 Replies View Related

C++ :: Global Char Type Array Initializing

Mar 20, 2015

I am not getting any error below

--------
#include <iostream>
using namespace std;
void main()
{
char sqr[3];
sqr[0] = '1';
sqr[1] = '1';
cout << sqr[0] << sqr[1];
while (1);
}------------

but if I move my char type array before main , I get error l

----------------------------
#include <iostream>
using namespace std;
char sqr[3];
sqr[0] = '1';
sqr[1] = '1';
void main()
{
cout << sqr[0] << sqr[1];
while (1);
}
-------------------

View 8 Replies View Related

C++ :: No Constructor Could Take Source Type Or Constructor Overload Resolution Was Ambiguous

Mar 1, 2014

i am writing this bank accounts program using structures. i haven't implemented the function before that i want to check if the data is being read and printed. When i build and run the program in visual studio it gives me the following error. "No constructor could take the source type, or constructor overload resolution was ambiguous". Now whats wrong in this program?

/* Bank Accounts Program */
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>//needed to use system() function
using namespace std;
const int MAX_NUM = 50;
struct Name{

[code]....

View 1 Replies View Related

C++ :: Constructor Array Copy

Nov 12, 2013

In the below program, how can copy the array n[] into Array[]. The below is not working..

#include <iostream>
using namespace std;

class arrayPlay {

[Code] .....

View 1 Replies View Related

C++ :: Initialize Array In A Constructor?

Dec 6, 2013

There are two class.How can I initialize an array of different class in a constructor?

class Ticket{
private:
int ID;

[Code]....

I have to provide a no-argument constructor (Cinema();)to initialize the ticket array and give the right ticket ID.

View 1 Replies View Related

C++ :: Array Of Structs With Constructor?

Apr 1, 2013

I can't seem to remember everything I should about constructors. I'm looking for a way to create an array of structs, using a constructor. My code should explain.

struct myStruct
{
private:
int structInt1, structInt2;

[Code].....

View 2 Replies View Related

C/C++ :: How To Pass Array To A Constructor

Feb 24, 2012

I want to pass an array to a constructor, but only the first value is passed--the rest looks like garbage. Here's a simplified version of what I'm working on:

#include <iostream>  
class board {
    public:
        int state[64];
        board(int arr[])

[Code] ....

Why this doesn't work and how to properly pass an array? Notes: I don't want to copy the array. Also, I'm using MSVC++ 2010 Express.

View 1 Replies View Related

C++ :: Making Constructor For Array Of Character

Jan 16, 2015

Code:
class Robot{
char name[15]; //assign to char n
int x, y , direction;
void ToString(int d);

[Code]......

how do i assign char name[] in the class to char n in the constructor?

View 3 Replies View Related

C++ :: Can't Initialize String Array In Constructor

Jul 22, 2013

I am currently practicing designing classes. In one exercise, I am trying to store 15 words in an array, and randomly print one (using the rand() functions and seeding it with crime. I have a header file, a respective .cpp file, and a main .cpp file. When I try to compile the code using g++ GuessWord.cpp UseGuessWord.cpp -o UseGuessWord, I get the following error in my constructor: expected primary-expression before ‘{’ token

Here is my code:

header file (GuessWord.h):
#ifndef GUESSWORD
#define GUESSWORD
#include <string>
using namespace std;

[code].....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved