I have
struct board{
int x[3];
int y[3];
};
// in the class PatternReader
board PatternReader::testPattern1DX() {
struct board aBoard;
int x[3] = { 1,1,1 };
aBoard = x;
return aBoard;
}
Error is "incompatible types in assignment of int *".
How do you set arrays that are inside a struct?
You cannot assign arrays. However, you can can initialize the struct:
board PatternReader::testPattern1DX()
{
board aBoard = {
{1, 1, 1},
{2, 2, 2}
};
return aBoard;
}
This initializes y as well as x.
Add an initializer function to the board struct:
struct board
{
int x[3];
int y[3];
void initX(int* values)
{
for(int i = 0; i < 3; i++)
x[i] = values[i]
}
};
Then use it:
board PatternReader::testPattern1DX()
{
struct board aBoard;
int x[3] = { 1,1,1 };
aBoard.initX(x);
return aBoard;
}
Your code
int x[3] = { 1,1,1 };
aBoard = x;
is creating a variable of type int* with the initial values 1,1,1. You are then trying to assign that to a variable of type board. You don't want to do that. I think you intended:
int x[3] = { 1,1,1 };
aBoard.x = x;
Note the .x at the end of aBoard. However, this is still wrong. You can't assign arrays like that. Look up "copying arrays" instead. Is there a function to copy an array in C/C++?
Honestly, I would suggest making board a class with constructors and and then you can make the constructors behave as you want, and also look into overloading assignment operators. But for now, trying copying from x to aBoard.x is probably what you want.
Related
I have an array of struct having const variables;
I am initialize by using constructor initializer list,
but if I make an array of that struct how to initialise all the elements of array of that structure.
if the struct contains no const var, then it is easily done
if the struct contains no const var, then it is easily done
#include<iostream>
struct s_Nconst{
int num;
int x1;
// s_Nconst(int);
};
int main(){
s_Nconst sN2;
sN2.num=5;
std::cout<<"initial values of num\t"<<sN2.num<<std::endl;
//making array of s_Nconst struct
s_Nconst sN3[sN2.num];
for(int i=sN2.num-1; i>-1; i--,++sN2.num){
sN3[i].x1= sN2.num;
std::cout<<"sn3["<<i+1<<"]\t"<<sN3[i].x1<<std::endl;
}
return 0;
}
but if suppose the struct comprises of const var, then I am able to make single object of the struct using the constructor initializer list, but if I am going to make array of the struct it is giving error
#include<iostream>
struct s_const{
const int x1,y1,z1;
static int num;
s_const(int,int,int);
};
s_const::s_const(int x,int y, int z)
:x1(x),y1(y),z1(z) {
std::cout<<"initial values of x1,y1,z1\t"<<this->x1<<" "<<this->y1<<" "<<this->z1<<std::endl;
}
/*struct s_Nconst{
int num;
int x1;
// s_Nconst(int);
};
s_Nconst::s_Nconst(int x)
:num(x){
std::cout<<"initial values of num\t"<<this->num<<std::endl;
} */
int main(){
int a,b,c;
a=1;b=2;c=3;
s_const s1(a,b,c);
//array of s1 is giving error
s_const sc[3];
//s_Nconst s2(++c);
//s_Nconst sN2;
//sN2.num=5;
//std::cout<<"initial values of num\t"<<sN2.num<<std::endl;
//making array of s_Nconst struct
//s_Nconst sN3[sN2.num];
//for(int i=sN2.num-1; i>-1; i--,++sN2.num){
// sN3[i].x1= sN2.num;
std::cout<<"sn3["<<i+1<<"]\t"<<sN3[i].x1<<std::endl;
//}
return 0;
}
is there any way to make array of structs having const variables and how to initialize them?
You can create an array of const objects, but they have to be initialized. For example, for your structs it should be:
s_const sc[3] = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}};
On a side note, you might prefer std::array to built-in arrays.
Error: braces around scalar initializer for type int*
I'm trying to pass an array to the function but I keep getting this error in the initialization step.
How can I fix this?
#include <iostream>
using namespace std;
void func (int *p[4]);
int main()
{
int *p[4]={ {1,4,5},{3,5,6},{6,6,2},{6,5,3}}; //The error appears here
func(p);
return 0;
}
void func (int *p[4])
{
for(int i=0;i<4;i++)
{
for(int j=0;j<1;j++)
{ cout<<p[i][j]; }
}
cout<<" \t";
}
Problem is what is p.
Whit this definition:
int *p[4];
this is 4 element array of pointers to int.
I guessing you wanted this:
int (*p)[3];
pointer to 3 element array of ints.
Also to use initialization you need an array.
Here is working example.
The variable p is an array of pointers. { 1, 4, 5 } is not a pointer.
You need to make p an array of arrays:
int p[4][3] = { ... };
Because of that change, you have to change the func function argument as well, as p will now decay to a pointer to an array, of type int (*)[3] (that is, the argument for func should be int (*p)[3]).
This would work, however you are going to lose array information because they will decay into pointers:
int arr1[] = { 1,4,5 };
int arr2[] = { 3,5,6 };
int arr3[] = { 6,6,2 };
int arr4[] = { 6,5,3 };
int *p[4] = { arr1, arr2, arr3, arr4 };
I've got some very simple c++ code to show the problem. I initialize my array with values in the ctor. But when I try to access the array in main, those values are replaced with random numbers. Why?
//Example to figure out why initialization values are disappearing
#include <iostream>
struct Struct1
{
float array1[2];
//ctor
Struct1();
};
Struct1::Struct1()
{
float array1[] = {0.2,1.3};
}
int main()
{
Struct1 StructEx;
std::cout<<StructEx.array1[0]<<' ';
std::cout<<StructEx.array1[1]<<std::endl;
return 0;
}
As #crashmstr mentioned, you do not initialise the member of the structure, but a local variable. The following code should work:
struct Struct1
{
float array1[2];
//ctor
Struct1();
};
Struct1::Struct1()
: array1 ({0.2,1.3})
{
}
int main()
{
Struct1 StructEx;
std::cout<<StructEx.array1[0]<<' ';
std::cout<<StructEx.array1[1]<<std::endl;
return 0;
}
Switch on the warnings (-Wall) when compiling, and you will see
float array1[]={0.2,1.3}; is unused
StructEx.array1[0] and StructEx.array1[0] are uninitialized
In the constructor put this
array1[0]=0.2;
array1[1]=1.3;
I come from Java and Ruby so I have a hard time coding simple stuff in c++, c++ being harder to tame...
I want to initialize an array in the class constructor with predefined values that can be accessed by all methods in the class. Yeah, basic stuff.
In other words, I want to do something like this:
Box.h
class SomeClass
{
public:
SomeClass(int something);
SomeMethod();
DoSomething(int thing);
protected:
int _something;
int[] arr;
};
Box.cpp
SomeClass::SomeClass(int something) : something(_something)
{
arr ={16,2,45,65,45};
for (int x = 0; x < 5; x++)
arr[i] = arr[i] * _something;
}
SomeClass::SomeMethod(){
for (int x = 0; x < 5; x++)
DoSomething(arr[i]);
}
SomeClass::DoSomething(int thing){
//whatever
}
How?
#include <iostream>
//#include <initializer_list>
#include <array>
#include <algorithm>
class foo
{
public:
foo() : values({{16,2,45,65,45}})
{
// I left the below commented out in case you want to research more and use other kinds of types for doing it.
//std::initializer_list<int> list = {16,2,45,65,45};
//std::copy(std::begin(list), std::end(list), std::begin(values));
}
void print()
{
std::for_each(std::begin(values), std::end(values),
[](int v) { std::cout << v << ' '; });
}
private:
static const int SIZE = 5;
std::array<int, 5> values;
};
int main()
{
foo fooInstance;
fooInstance.print();
return 0;
}
The above is the best that I could do in a short period of time. I tested with the following compiler. You could use that as a starting point and see if you can learn other ways of doing it. Algorithms such as generate can also be used with lamda expressions in order to generate a controlled set of values (as opposed to a hard coded list as in our examples).
http://www.compileonline.com/compile_cpp11_online.php
You have options for initializing array data members, and two types or array you can use:
Using a C-style array,
struct Foo
{
int a[5] = {1,2,3,4,5}; // initialization at point of declaration
};
struct Bar
{
int a[5];
Bar() : a{1,2,3,4,5} {} // initialization in constructor initialization list
};
Or using an std::array:
struct Foo
{
std::array<int,5> a = {{1,2,3,4,5}}; // initialization at point of declaration
};
struct Bar
{
std::array<int,5> a;
Bar() : a{{1,2,3,4,5}} {} // initialization in constructor initialization list
};
If you can use C++11, there is support for uniform initialization which allows you to assign brace-enclosed lists of values in more contexts than the special case in C.
this question is regarding the syntax of an array of array of structs.
I have a struct that takes in two ints:
struct point
{
int x, y;
};
I have created another struct that takes in 8 of these structs:
//Creating an Array of Array of structs
struct Arraypoint
{
point variable[8];
};
//Not sure if this is the correct way to do it.
Now, in main, I want to declare an array variable of type Arraypoint with 8 indices, so effectively I will have 8 * 8 = 64 elements of struct point and 128 ints (64 x and 64 y).
Also, how would I access an individual element struct point from the array Arraypoint?
Okay after having declared in main lets say Arraypoint is 2.
Arraypoint arr[2];
How do I initialize the elements without having to type in arr[0].variable[0].x = ... or without using for loops.
Why can't I do the following, it doesn't seem to work.
Arraypoint arr[2] = { {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)},
{(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)} }//xy are rand
I have used curly braces in my code, the error returned is missing braces around initializer for type point and too many initializers for type Arraypoint.
In C++, you'd just write:
Arraypoint arr[8];
An individual point could then be accessed via:
arr[i].variable[j];
More practically, though, you'd probably be better off using e.g.
std::vector<std::vector<point> >
or writing your own class with an overloaded operator(int i, int j). For example:
class PointMatrix
{
private:
std::vector<point> m_points;
public:
PointMatrix() : m_points(64) {}
point& operator()(int i, int j) { return m_points[8 * i + j]; }
const point& operator()(int i, int j) const { return m_points[8 * i + j]; }
};
PointMatrix mat;
m(3, 4).x = 23;
got it: ideone.com/ix3hC. Arraypoint::variable has to have it's own { } pair.
struct point
{
int x, y;
};
#define P {0, 0}
struct Arraypoint
{
point variable[8];
};
#define V { P, P, P, P, P, P, P, P}
#define AP { V } //this is the pair you missed
int main() {
Arraypoint arr[2] = { AP, AP };
}
struct Arraypoint arraypoints[8];
is what you're after, I think. To use them:
int firstx = arraypoints[0].variable[0].x;
This isn't so pretty though
struct point { int x, y; };
struct point[8][8] arraypoints;
Is probably better? Don't know what exactly you're after though.
To create an array of Arraypoints, you can do:
Arraypoint arr[8];
To access an element:
arr[i]
will return the i'th Arraypoint element
arr[i].variable[j]
will return the j'th point in the element
arr[i].variable[j].x
will return the x coordinate of that point.
So I realized why I couldn't declare my array as such,
Arraypoint arr[2] = { {(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)},
{(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y),(x,y)} }
//xy are randomn integer values
its because in my struct declaration of Arraypoint, it takes in 8 elements of type point. So
I have to create variables of type point to store(x,y) and then i could store this variable in Array point.
point point1 = {x,y}, ...;
Arraypoint arr[2] = { {point1,point2,point3,point4,point5,....} };
Just for anyone in the future who stumbles across the same problem.