0
#include "std_lib_facilities.h"

class Token {
public:
        char kind;
        double value;
        string name;
        Token ( char ch ) : kind{ch} { } 
        Token ( char ch, double val ) : kind {ch}, value {val} { } 
        Token ( char ch, string n ) : kind {ch}, name {n} { } 
};


int main ( void )
{
        char ch; 

        cin >> ch;    

        // Token == ch; // Fails to compile - see below.
        Token {ch};

        return 0;
}

What is the different meaning between: Token {ch}; versus Token = ch; What does this error means?

error: expected unqualified-id

ERROR WHEN USING `Token =  ch;`.
$ c++ -std=c++11 -o poc_Token_assignments poc_Token_assignments.cpp
poc_Token_assignments.cpp:20:8: error: expected unqualified-id
        `Token =  ch;`
              ^
1 error generated.

Non-error when using curly braces (Token {ch};):

$ c++ -std=c++11 -o poc_Token_assignments poc_Token_assignments.cpp
$   (COMPILED PERFECTLY USING THOSE {} CURLY BRACES)
Joris Timmermans
  • 10,814
  • 2
  • 49
  • 75
nostromo
  • 61
  • 1
  • 1
  • 11
  • 5
    `Token {ch};` creates a temporary `Token` object and then throws it away at the `;`. `Token = ch;` is not valid syntax. – Richard Critten Jan 06 '23 at 12:23
  • 1
    https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Jan 06 '23 at 12:25
  • 2
    why do you think the difference is "subtle" ? Do you actually mean `Token t = ch;` ? Thats not assignment – 463035818_is_not_an_ai Jan 06 '23 at 12:26
  • I am trying to understand the whole code of the example calculator02.cpp on a Bj Strup book ("PROG Pples and Ptice Using C++") [To be ready to debbug the calculator02_buggy.cpp version challenge]. The point is that the book starts smooth but then assumes suddently a lot of OOP concepts (not explaining anything before and even using personal libraries like "std_lib_facilities.h" that was a madness to set from the beginning. Not to say C++11,99 versions...) and then i had to go through a lot of ytbe videos and reading on stackoverflow and everywhere to understand initializator lists... Etc... – nostromo Jan 06 '23 at 12:35
  • 1
    To understand a myriad of things... Not enough, the program is very recursive, so that is another layer of difficulty. As i see by your answers, i am merging erroneously classical variables assignments with OOP concepts. The POC (prove of concept) code that i put here is from a switch on the code: ... case ... case... return Token {ch}; // let each character represent itselt case.... case.... Using your answer, what i see is that this "return", returns a initializating constructor? Not an assignment. Is that? – nostromo Jan 06 '23 at 12:36
  • The question is: when you call a constructor... The syntax should be... Token () PARENTHESIS, NOT CURLY BRACES, NO? Not: Token {} So my confussion is there, because in a lot of videos, they say that "curly braces work as an assignment operator, 'but better' (?¿?¿?), in case of doubt, user curly braces (?¿?¿?)". Not great answers... So whats the point with curly braces in C++ and how one can solve this bad understood ambiguity? Is there overlapping between curly braces in a OOP context vs curly braces in classical context? (like, f. e. arrays assignment... x[] = {....}. Thank you. – nostromo Jan 06 '23 at 12:47
  • to solve "this bad understanding" don't attempt to learn C++ from videos - get a good book. And i wouldn't say Stroustrups book is a particularly good one. – Neil Butterworth Jan 06 '23 at 12:53
  • In the example you quote here - the switch/case says "return Token {ch} " - which means - construct a Token with list-initialization from ch *and return it*. So the temporary gets returned to the caller - it doesn't just disappear like it does in your PoC example. That's an important detail to understand the usefulness of the code and the context of the question. So yes - your comment a few lines above is a good understanding - it's an initializing Token constructor. – Joris Timmermans Jan 06 '23 at 14:46

1 Answers1

3

It is pretty simple, look at these three lines:

5;          // Creates an int literal which goes away right away
int = 5;    // Syntax error: Cannot assign a value to a type
int a = 5;  // Correct: Store the value 5 in the variable a

Now for your code:

Token {ch} is similar to the first line. A Token is created and then destroyed right away.

Token = ch similar to the second line: You cannot assign a value to a type.

What I think you want is one of these:

Token t = ch;
Token t{ch};
Token t(ch);

For the difference between the two last I will refer you to: What are the advantages of list initialization (using curly braces)? or perhaps better; a good book.

Frodyne
  • 3,547
  • 6
  • 16
  • Ok. I will get a coffee and read all this slowly. Last question: is this book (Bjrn Strup's "T C++ Progr Lang 4th Edit") mentioned in the link ("What are the adv...?") you just pointed, a nice book for a good pace learning, absorbing and practicing all this concepts or do you think there would be another suggestion (i have to say i am using Kochan's to learn C and is great... Explaining every concept & inducing examples and problems to code while). This Bj is like C++'s pope & sure to gets to the sky in the precise architecture of C++ and deep abstractions, but any kochan's like approaches? – nostromo Jan 06 '23 at 13:29
  • I haven't read that many C++ books, so I will refer you to this list of books that the StackOverflow C++ community seems to review favorably: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list (the Bjarne Stroustrup book you mention is on it, but the review does say: "This is not a book for novice programmers") – Frodyne Jan 06 '23 at 13:54
  • perhaps better analogy for OPs line is `int{5};` – 463035818_is_not_an_ai Jan 09 '23 at 14:12