yoav
i did : bool strCmp(char* c1, char* c2){ if(strcmp(c1, c2) == 0){ return true; }else{return false;} and this gave me an a error
yoav
why?
Ilya
i did : bool strCmp(char* c1, char* c2){ if(strcmp(c1, c2) == 0){ return true; }else{return false;} and this gave me an a error
I'd you write like this it shows you are not a c++ programmer at all, knowing nothing...
Ilya
i did : bool strCmp(char* c1, char* c2){ if(strcmp(c1, c2) == 0){ return true; }else{return false;} and this gave me an a error
That is what your should have written bool strCmp(char* c1, char* c2){ return !strcmp(c1, c2); }
Ilya
i did : bool strCmp(char* c1, char* c2){ if(strcmp(c1, c2) == 0){ return true; }else{return false;} and this gave me an a error
Or, in modern c++, auto [](const char* c1, const char* c2) { return !strcmp(c1, c2); }
Anonymous
Lambda expression
yoav
Or, in modern c++, auto [](const char* c1, const char* c2) { return !strcmp(c1, c2); }
strcmp return an int 0 for true other for false and its not cpp is just c language
yoav
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<stdbool.h> int main(){ char hello[] = "hello"; char bye[] = "bye"; str(hello, bye); } bool str(char* c1, char* c2){ if(strcmp(c1, c2) == 0){ return true; }else{return false;} return false; } errors: source_file.c: In function ‘main’: source_file.c:9:5: warning: implicit declaration of function ‘str’ [-Wimplicit-function-declaration] str(hello, bye); ^ source_file.c: At top level: source_file.c:11:6: error: conflicting types for ‘str’ bool str(char* c1, char* c2){ ^ source_file.c:9:5: note: previous implicit declaration of ‘str’ was here str(hello, bye); ^ why?
yoav
It's correct
so why i get error
olli
so why i get error
declare or define your method before main
olli
Why?
either remove the auto or assign it
olli
Why?
<source>: In function 'int main()': <source>:5:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' 5 | auto [](const char* c1, const char* c2) { | ^ <source>:5:10: error: empty structured binding declaration 5 | auto [](const char* c1, const char* c2) { | ^~ <source>:5:13: error: expected primary-expression before 'const' 5 | auto [](const char* c1, const char* c2) { | ^~~~~ <source>:5:29: error: expected primary-expression before 'const' 5 | auto [](const char* c1, const char* c2) { | ^~~~~ <source>:5:10: error: invalid initializer for structured binding declaration 5 | auto [](const char* c1, const char* c2) { | ^~ Compiler returned: 1
yoav
declare or define your method before main
ok its not like java we cant call function we didnt define before?
olli
ok its not like java we cant call function we didnt define before?
yes it's not like java, you need to at least declare them. put bool str(char* c1, char* c2); before main (or the whole definition) e.g. // declare foo void foo(); int main() { foo(); } // define / implement foo void foo() { }
olli
It's correct
not sure what the auto is supposed to be, lambdas use trailing return types
Artöm
Or in normal C
I meant const
Anonymous
not sure what the auto is supposed to be, lambdas use trailing return types
I think he meant to write smth like that: auto equal = <lambda>;
olli
I think he meant to write smth like that: auto equal = <lambda>;
I think so too, although he could also mean to have auto as return type. (basically function as lambda). Doesnt matter in the end anyway lol :D
Augmented
hello, friends... excuse me for the silly question... but... which one is better to use? "type" const or const "type"... e.g. const int x=5; or int const x=5; what about when it's const parameter for a function ? or a const pointer type
olli
hello, friends... excuse me for the silly question... but... which one is better to use? "type" const or const "type"... e.g. const int x=5; or int const x=5; what about when it's const parameter for a function ? or a const pointer type
const T is the same as T const - doesnt really matter which one you choose, just be consistent for pointer it's const T * const ptr; or T const * const ptr; The first const means the value the pointer points to is const, the second means the pointer is const
Liam
Yes, that's why I asked... because in the case with pointers it does matter
They are just the same. Pick one and use it till the end of the world.
Augmented
Liam
why not using std::swap?
Ilya
either remove the auto or assign it
This is returned type of the function.
olli
This is returned type of the function.
as mentioned above, lambda uses trailing return types Hence your syntax is ill-formed
Pavel
hello, friends... excuse me for the silly question... but... which one is better to use? "type" const or const "type"... e.g. const int x=5; or int const x=5; what about when it's const parameter for a function ? or a const pointer type
There are people who prefer the first and who prefer the second. Most projects I know just picked one of these (that their lead architect or whoever started the project preferred) and made it part of codestyle. So you can really choose one and use it in your projects, and in others' projects use those that they prefer.
Ilya
as mentioned above, lambda uses trailing return types Hence your syntax is ill-formed
Work out a little bit and make it correct in the context of the program you use
Artöm
Some people do const T but T const&, because second is read right to left
MᏫᎻᎯᎷᎷᎬᎠ
will c++20 have a better alternative for forward declaration with the help of modules?? Like if we have: A() was defined before B() Can A() call B() in modular system in C++20?!
MᏫᎻᎯᎷᎷᎬᎠ
MᏫᎻᎯᎷᎷᎬᎠ
Reason?
Another form of ugliness in my code
WHITE DEVIL
it mean declare the class
Ilya
it mean declare the class
I 'm not asking...
MᏫᎻᎯᎷᎷᎬᎠ
MᏫᎻᎯᎷᎷᎬᎠ
Ilya
How about making them optional
How are overloads related to this?
MᏫᎻᎯᎷᎷᎬᎠ
Ilya
Yeah And function prototypes
Class declararion/definition and function declaration/definition are not related to each other
Ilya
And
Why do you think this is somehow connected?
Artöm
How are overloads related to this?
Could be possible with epoches, but I think it contradicts the way C++ code compiles
MᏫᎻᎯᎷᎷᎬᎠ
What the hell are you saying?!
MᏫᎻᎯᎷᎷᎬᎠ
Who said they are connected?! Me?! Not
Ilya
will c++20 have a better alternative for forward declaration with the help of modules?? Like if we have: A() was defined before B() Can A() call B() in modular system in C++20?!
OK , anyway, modules don't change anything in the language, don't add new semantics, they just provide another way of translation of source code.
MᏫᎻᎯᎷᎷᎬᎠ
What's wrong if I can use a function that was not declared but, straightforward defined after the upper function in the source
MᏫᎻᎯᎷᎷᎬᎠ
Ilya
Yeah And function prototypes
Because, this term "forward declaration" is applied only to classes, not to functions and other things, AFAIR
MᏫᎻᎯᎷᎷᎬᎠ
Without adding a function prototype above void A()
Ilya
Without adding a function prototype above void A()
prototypes MUST be declared before using functions, with or without modules.
Ilya
Whatt?!!
I mean if you add function declaration first.
MᏫᎻᎯᎷᎷᎬᎠ
I mean if you add function declaration first.
Yeah That's what I was talking about
MᏫᎻᎯᎷᎷᎬᎠ
And it should disappear
MᏫᎻᎯᎷᎷᎬᎠ
Or be there optional
Ilya
I mean if you add function declaration first.
But modules cannot do anything with it, you just cannot call an abstract callable object before it is known
MᏫᎻᎯᎷᎷᎬᎠ
Like being an "implicit declaration" or sth