object initialisation

I wanted to initialise an object of one class(say class A) in another class (say class B). I was successful using :

A a1{1,1}

but the following gave me error :

A a(1,1):

but the above initialisation works in the main()?

Here is the whole code:

inside Class 1 CVector.h:

1
2
3
4
5
6
7
8
9
  #include <iostream>

class Cvector {
public:
    int x,y;
    Cvector () {};
    Cvector (int a,int b) : x(a), y(b) {}
   
};


inside class Cvector2.h :

1
2
3
4
5
6
7
8
#include <iostream>
#include "Cvector.h"
class Cvector2 {
    
    Cvector fooF {3,10}; // works
    Cvector fooF2 (1,1); // error!?
};


inside main :

1
2
3
4
5
6
7
8
9
10

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

int main () {
   Cvector foo (3,1);
    Cvector bar (1,2);
    Cvector bar2((1,2),(1,4));
    Cvector bar3[3] ;
    }


Why is Cvector fooF2 (1,1); wrong?

My second question is , in the main function ,before (c++11 style) curly braces initialisation came out what was the default way to initialise

 
Cvector bar3[3];

was this the only or something different? :

 
Cvector bar3[3] ={{1,2},{1,3},{2,3}}

because i found the following to give errors :

1
2
3
Cvector bar3[3] ((1,2),(1,3),(2,3)); // error
Cvector bar3((1,2),(1,3),(2,3)) [3]; //error
Cvector bar3 ((1,2),(1,3),(2,3)); // correct , but what if i want the number 3 to be displayed 


Thanks!
Last edited on
For the first question, generally we are more productive when you provide the error. But in this case the second instance is broken because the compiler thinks that you are trying to declare a function named 'fooF2' that returns your class type but you are listing static numbers where it expects to see variable types. Where as the first one is just a value initialization of a variable named 'fooF' of type CVector. Using different operators will usually give you different results, that should be expected.

EDIT: As for the first part of the second question; personally I just always used a for loop. I never used literals for array declarations though, I always used a const lvalue so that I could use it to delimit the for loops range. This way you only have to keep track of it in one spot if later you want it changed.

The second part of your second question is more interesting though. I honestly don't know what the comma operator is doing in there, but you're not declaring an array.
Last edited on

The errors were:

1)expected ")'
1)expected parameter declarator
I knew what they were. Believe it or not I've made these same kinds of mistakes when I was learning this stuff as well. That comment was more for future reference.
> I was successful using : A a1{1,1}
> but the following gave me error : A a(1,1)

The initialiser included in the declaration of a member variable must be a brace-or-equal-initializer.

> but the above initialisation works in the main()?

In other contexts, initialiser may be either a brace-or-equal-initializer or an ( expression-list )

> My second question is , in the main function ,before (c++11 style) curly braces initialisation came out
> what was the default way to initialise Cvector bar3[3];

If Cvector is a Copyable type, Cvector bar3[3] = { Cvector(1,2), Cvector(1,3), Cvector(2,3) } ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Cvector {
public:
    int x,y;
    Cvector () {};
    Cvector (int a,int b) : x(a), y(b) {}
};

class Cvector2 {

    Cvector foo1 {3,10}; // braced-init-list, fine
    Cvector foo2 = Cvector(1,1) ; // = initializer-clause, fine
    Cvector foo3 = {5,6} ; // = initializer-clause, fine

    Cvector bar1[3] { {1,2}, {1,3}, {2,3} } ; // fine
    Cvector bar2[3] = { {1,2}, {1,3}, {2,3} } ; // fine
    Cvector bar3[3] = { Cvector{1,2}, Cvector{1,3}, Cvector{2,3} } ; // fine
    Cvector bar4[3] = { Cvector(1,2), Cvector(1,3), Cvector(2,3) } ; // fine
};



> Why is Cvector fooF2 (1,1); wrong?

>> But in this case the second instance is broken because the compiler thinks that
>> you are trying to declare a function named 'fooF2'

No; there is no MVP here.
Last edited on
MVP: Minimal Viable Product? That's a marketing term that I never thought I would see a programmer acknowledge much less use. I don't see how it applies to the function operator though.
MVP (C++): Most Vexing Parse (origin: Effective STL, Item 6)

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
30
class Cvector
{
    public:
        int x, y;
        Cvector () {};
        Cvector ( int a, int b ) : x( a ), y( b ) {}
};

int main()
{
    Cvector fooF2 ( 1, 1 ) ; // this can't be a declaration; no mvp
    fooF2 = { 1, 2 } ; // fine
    
    Cvector fooF3 { int(), int() } ; // this can't be a declaration; no mvp
    fooF3 = { 1, 2 } ; // fine
    
    Cvector fooF4 ( int {}, int {} ) ; // this can't be a declaration; no mvp
    fooF4 = { 1, 2 } ; // fine
    
    Cvector fooF5 ( int(), int{} ) ; // this can't be a declaration; no mvp
    fooF5 = { 1, 2 } ; // fine
    
    Cvector fooF6 ( int(), int() ) ; // this can be a declaration; mvp
    // *** warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
    // ** note: add a pair of parentheses to declare a variable
    fooF6 = { 1, 2 } ; // **** error: assignment to function

    Cvector fooF7 ( ( int() ), int() ; // this can't be a declaration; no mvp
    fooF7 = { 1, 2 } ; // fine
}

http://coliru.stacked-crooked.com/a/45489bf335275e6b
Last edited on
Topic archived. No new replies allowed.