Giving values to class members

I need to set values for variables which are struct members(variable1,variable2) in the code below. values should be different and they are just set once for each variable(initializing).

How can I use set function for this, void setValue(const mystruct & val)

Do I need to do something with the constructor of myclass, since I get error undefined reference to mystruct::mystruct()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 Struct mystruct
{
mystruct();
int a,b,c,d;
};

class myclass
{
public:
myclass();
void setValue1(const mystruct & variable);
void setValue2(const mystruct & variable);

private:
mystruct variable1;
mystruct variable2;
};
Use mystruct's constructor.
1
2
3
4
5
6
7
8
9
10
11
struct mystruct
{  mystruct(int,int,int,int);
    int a,b,c,d;
};

mystruct::mystruct(int _a, int _b, int _c, int _d)
{   a = _a;
    b = _b; 
    c = _c;
    d = _d;
}


You can then use your set functions as follow:
1
2
3
4
5
6
7
8
myclass x;

main ()
{   myclass x;

    x.setValue1 (mystruct(1,2,3,4));
}




It still gives me an error saying undefined reference to mystruct::mystruct() in the constructor for myclass. How can I do something in the class constructor to avoid this ?
You must write the implementation for mystruct::mystruct().
The following compiles cleanly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
struct mystruct
{  mystruct(int _a, int _b, int _c, int _d);
    int a,b,c,d;
};

class myclass
{
public:
  myclass();
  void setValue1(const mystruct & variable);
  void setValue2(const mystruct & variable);

private:
  mystruct variable1;
  mystruct variable2;
};

mystruct::mystruct(int _a, int _b, int _c, int _d)
{  a = _a;
    b = _b; 
    c = _c;
    d = _d;
}

int main ()
{   myclass x;

    x.setValue1 (mystruct(1,2,3,4));
}

Topic archived. No new replies allowed.