|
If it's used as a string, then why don't you #define it as a string? If you don't know how to correctly use #define, why do you use it at all? It's bad style anyway! Make it a const string instead:
const std::string VERSION = "1.0.0";
There. Works every time. And if the compiler complains, the code that uses it is wrong! That is the advantage of using const instead of #define: the compiler will notify you of usage problems, whereas in case of #define there's no guarantee the compiler will catch a glitch, and if it does, it will likely not point to the right position in your code.
Vaclav_Sal wrote: PS What is the correct name for "the stuff" after #define and VERSION?
The correct name is "clutter", or more to the point: "stuff that clogs your global namespace". #define symbols have a nasty habit of colliding with variable and function names elsewhere because they pollute the entire global namespace. Just don't use it!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Stefan_Lang wrote: #define symbols ... pollute the entire global namespace
Ummm... what? They are gone as soon as the preprocessor completes.
|
|
|
|
|
That's true, of course. Still you may have a clash with a global symbol.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
That's OK, it'll just rock the casbah.
|
|
|
|
|
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
It once took me more than a day to resolve an issue that manifested as some inexplicable and incomprehensible error message somewhere in the depths of the MS-provided STL implementation. In the end it turned out that the #defined symbols min and max from the windows header files managed to wreak so much havoc in the implementation files of std::valarray, that the error messages not only were totally unrecognizable but also pointed to an entirely different point in the code!
That's what I mean by cluttering the global namespace: just about anywhere in your code, any macro from a totally unrelated part, has the potential to totally destroy your code to the point where you neither recognize the location nor cause of the problem! Fixing such an issue in a codebase of 3 million lines of code is not fun at all!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
The problem is not with the defined value, but in how you are trying to use it. It will be difficult to use that value unless you make it a string, though you can stringize it. The following shows how it can be done (I used Borland's C/C++):
# include <stdio.h>
# define X(x) Y(x)
# define Y(y) #y
#define VERSION 1.0.0
void main() { printf ( "%s" , X ( VERSION ) ) ; }
http://msdn.microsoft.com/en-us/library/7e3a913x.aspx[^]
In general, if you use DEFINEs for constants (and you shouldn't), only use values that are valid literals.
Vaclav_Sal wrote: What is the correct name for "the stuff"
MSDN calls it "token-string".
Syntax
#define identifier token-stringopt
#define identifier( identifieropt, ... , identifieropt ) token-stringopt
http://msdn.microsoft.com/en-us/library/teas0593.aspx[^]
modified 16-Oct-14 15:20pm.
|
|
|
|
|
Please read the OP and if you do not know the answer do not bother to reply.
I did not ask for a lecture why not to use #define.
|
|
|
|
|
Please read the response.
|
|
|
|
|
I thought that was a pretty good response...
|
|
|
|
|
That's just rude.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
Vaclav_Sal wrote: Apparently #define VERSION does not simply replace the VERSION with literal 1.0.0 but the 1.0.0 is analyzed by GCC compiler. When you use that statement the value after the word VERSION has two decimal points so it is not a valid token, and the compiler rejects it. A #define statement must contain valid C/C++ code, so the value after the identifier must be a valid literal or expression which resolves to a valid literal; see http://msdn.microsoft.com/en-us/library/teas0593.aspx[^].
|
|
|
|
|
My test (above) with Borland's compiler had no trouble -- provided I stringized the value. But there are better ways to skin that cat.
|
|
|
|
|
Same with Microsoft C++, as you would expect.
|
|
|
|
|
And VAX/DEC/HP C of course.
|
|
|
|
|
Thanks for the link Richard.
Here is the reason why it had too many decimal points
"The token-string argument consists of a series of tokens, such as keywords, constants, or complete statements."
It passes if the token string is just 1.0, but obviously it has to be correctly formatted for printf to display right.
|
|
|
|
|
when we are going to develop code c++
1.why we are using visual studio 2008
2.why we are using unix
3.why we need SQL commands
|
|
|
|
|
1. Because it is a good IDE for developing Windows/Web applications.
2. I don't know, why are you using Unix?
3. To access your database.
|
|
|
|
|
- You can use
Visual Studio for writing C++ applications targetting the Windows platforms. - If you are using
UNIX (or Linux ) then you are NOT using Visual Studio (and you are using a UNIX or Linux compiler, e.g. gcc ). SQL commands are used to deal with databases, see, for instance the SQL Wikipedia page[^].
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
Hi Guys,
I have a small problem i hope you guys can help.
Actually i have an Win32 application which contacts to a cloud server and the communication between my client and server code is both the XML and json formats.
But can u suggest me some other formats where size of my code should be less and communication should be fast and reliable.
Any suggestions please.
Thanks in advance.
|
|
|
|
|
There is bson which is a binary json. Many software products use sqlite3 and sync it to their server. For example Skype. You can also create yourself a small TCP/IP based protocol.
- Michael Haephrati מיכאל האפרתי
|
|
|
|
|
Thanks Michael actually we are thinking of writing our own binary format will it be a good idea ?
|
|
|
|
|
That seems to be the best idea but it is also important to structure the data properly and sdqlite3 can be great for doing so. You can ofcourse encrypt the sqlite3 database file then.
- Michael Haephrati מיכאל האפרתי
|
|
|
|
|
Member 10813090 wrote: where size of my code should be less and communication should be fast and reliable.
And why is what you are currently doing a problem? Is there a measured bottleneck that you have determined is caused by your code?
|
|
|
|
|
Using Visual Studio 2013 C++, I got compilation errors that I couldn't explain.
The compilation errors were:
*main.cpp(325): error C2601: 'FLAG' : local function definitions are illegal
main.cpp(323): this line contains a '{' which has not yet been matched
main.cpp(326): fatal error C1075: end of file found before the left brace '{' at 'main.cpp(323)' was matched*
But there was nothing wrong with my code. I counted all brackets and the number matched. There weren't any function inside another function.
I solved it by removing all "//" comments from the source code. It seems that the reason for that is bad line formatting which causes the compiler to miss a line break, so the line after a comment is treated as a comment as well.
For example:
This_is_a_line;
is treated as:
There are many posts of the net about similar problems and some even suggested that they could be caused by a memory (RAM) fault on the machine, so before you replace your RAM, just remove the comments and see...
- Michael Haephrati מיכאל האפרתי
|
|
|
|