0

It is stated on the site cppreference.com, something like that

For each declarator, the initializer may be one of the following:

( expression-list ) (1)

= expression (2)

{ initializer-list } (3)

  1. comma-separated list of arbitrary expressions and braced-init-lists in parentheses

But in my code

int main(){

    int a,b=5,c(a,b);
    return 0;
}

when I try to compile, the following error occurs

...error: expression list treated as compound expression in initializer [-fpermissive]

My question is, if list of multiple expressions is allowed in such style of initialization, then why the compiler is not accepting it with variable c? What am I missing?

  • 4
    what do you expect `c(a,b)` to do? – 463035818_is_not_an_ai Oct 18 '21 at 18:18
  • Just testing my clearance about that concept (including comma-separated list in variable initialization), it is expected to be ok, but a compile time error is being generated. – Ammar Mujtaba Tariq Oct 18 '21 at 18:20
  • Do you expect `c` to be of type `int`? How do you expect `c(a,b)` to initialize an `int`? – Cory Kramer Oct 18 '21 at 18:22
  • 1
    `a,b` cannot be an expression list (since an int has no constructor). This is a comma expression in a place that causes ambiguity, and the valid initializer would be `c((a,b))`. – StoryTeller - Unslander Monica Oct 18 '21 at 18:22
  • @StoryTeller-UnslanderMonica_a,b cannot be an expression list_, what actually is the expression list? And _(since an int has no constructor)_, what do you mean by that? – Ammar Mujtaba Tariq Oct 18 '21 at 18:26
  • 2
    I think the answer here is going to be "_valid grammar_ does not guarantee _code that will compile_" – Drew Dormann Oct 18 '21 at 18:27
  • @DrewDormann, kindly explain to me what that means, I am in early stages of learning c++. – Ammar Mujtaba Tariq Oct 18 '21 at 18:29
  • @AmmarMujtabaTariq similar to English or any sufficiently advanced language, C++ has a _grammar_ that describes how a statement can be composed, but following that grammar does not guarantee that you are making sense. `int c(a,b);` obeys the rules of grammar that you have read, but it does not make any sense. It has no meaning. – Drew Dormann Oct 18 '21 at 18:44
  • @DrewDormann , so does it mean that we should not rely on the content of cppreference? Should I consult to another website instead of it? Because I want my code compiled alongwith the clearance of my concepts. – Ammar Mujtaba Tariq Oct 18 '21 at 18:46
  • @AmmarMujtabaTariq cppreference is an _excellent site_. You are misunderstanding what you are reading. I think you are misunderstanding "may be" to mean "is always valid". – Drew Dormann Oct 18 '21 at 18:51
  • In the bottom of the answer bellow, @JosephLarson is saying that cppreference is not good for learning c++, if it is so, can you too refer me a great book on c++? – Ammar Mujtaba Tariq Oct 18 '21 at 18:54
  • [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Drew Dormann Oct 18 '21 at 18:55

1 Answers1

0

All right, let's look at this:

int main(){

    int a,b=5,c(a,b);
    return 0;
}

What do you expect c(a,b) to actually do?

Let's simplify this just slightly:

int main(){

    int a,b=5;
    int c(a,b);
    return 0;
}

This will generate the same syntax error, but it now stands alone. So...

Your code would work if there were a constructor for int that took two ints as parameters. This would also compile:

int c(int a, int b);

But in that case, you're actually defining a function.

Also, this works:

int main() {
    int a = 5;
    int b = 10;
    int c(b);

    std::cout << "C == " << c << std::endl;
}

That works because an int can be initialized from a single int. But you're getting an error because you can't initialize an int from two other ints.

This works:

#include <iostream>

class MyClass {
public:
    MyClass(int a, int b): value(a + b) {}
    int value;
};

int main() {
    int a = 5;
    int b = 10;
    MyClass c(a, b);

    std::cout << "C == " << c.value << std::endl;
}

And maybe that's what the article you read was trying to tell you. Note: cpppreference is NOT a good site for learning C++. Get a good book.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • Please refer me a good book, by reading which, I don't require to consult from any other source, it explains me the concepts thoroughly and which leads me from beginner to advance, and kindly tell me why is that site not good for learning c++? – Ammar Mujtaba Tariq Oct 18 '21 at 18:51
  • cpppreference is a reference site. It attempts to be a tutorial site, but I don't think it does a very good job, especially for beginners. Other people may disagree with me. As for good C++ books -- the ones I used are from the 90s and deeply out of date. But if you google for "good c++ books", you'll get a lot of people who offer suggestions. – Joseph Larson Oct 18 '21 at 19:01
  • Nowhere does it attempt to be a tutorial site. Its target audience is the journeymen, not the apprentice. – StoryTeller - Unslander Monica Oct 18 '21 at 19:13