|
You should never define variables inside header files (headers are for declarations), because you may get multiple definitions of the same symbol (if, as usual, the header is included by many sources).
If you need to access a variable from multiple sources then you have to:
- Declare it as
extern inside an header file. - Define it inside just one source file.
- Include the header file into every source that needs to access the variable itself.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks . .
|
|
|
|
|
Hey,
Iam reading this book and Iam writing my own program aside.
Iam currently reading about some functions related to linux.
#include <stdio.h>
#include <iostream>
using namespace std;
int main(int argc,char *env[])
{
int i=0;
cout << env[i] << endl;
return 0;
}
This is the program, now how would I actually put the output of env[i] into a string?
In my own program Iam playing around with conversions and string editing and stuff, now I need to convert the output of env[i] into a string though, not sure how to do that.
Thankfull for any help, greetins 
|
|
|
|
|
ALLERSLIT wrote: int main(int argc,char *env[])
This should probably be:
int main( int argc, char *argv[], char *env[] )
ALLERSLIT wrote: how would I actually put the output of env[i] into a string?
Have you tried:
std::string str = env[i];
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Oh thanks, gotcha!
let's say I wouldn't put
char *env[] there into the main() thingy, how would I be able to get the output of env[i] then?
Like
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
char *env[];
string str = env[9];
cout << str << endl;
return 0;
}
I get this error: |14|error: storage size of ‘env’ isn't known
Makes sense to me, but how would I avoid that error?
|
|
|
|
|
ALLERSLIT wrote: Makes sense to me, but how would I avoid that error?
By declaring env as:
char *env[10];
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
If I declare it the way you did, I can not put it into a string the same way i learned in the last post.
|
|
|
|
|
You seem to be misunderstanding main() 's signature and it's various formats. That said, what exactly is it that you are trying/wanting to do?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Trying to figure out how to get the outpuf of env[i] without putting all that other stuff into main()'s signature.
|
|
|
|
|
Whether you declare them in the signature or not, the number of command-line arguments, the command-line arguments themselves, and the environment variables are "sent" to main() regardless. Obviously, if you opt to not declare them in main() 's signature, you will not have access to them.
That said, what is env and how is it declared?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
You may access the _environ variable.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
That makes no sense to me, even if you fix the error the way David (correctly) suggested (the array content is still uninitialised).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
ALLERSLIT wrote: how would I avoid that error?
By declaring env properly according to the rules thus:
int main(int argc, char* argv[], char *env[])
{
}
Your sample above declares env as a local variable that is never initialised so you cannot get anything out of it. Remember that main() is simply a function that is called by the framework with the three parameters as I have described.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
I have made a .net application which uses a COM component hosted by a C++ service. The service is a simple C++ service developed using ATL (and not using MFC).
Everything works fine on my dev machine as most of the work is done by VisualStudio itself.
Now I have to make installer of my application (application should be able to get install on XP, VISTA and Windows 7). I am following below steps to register my service:
"C:\Program Files\MyApplication\MyService.exe" -service (For registering the service)
"C:\Program Files\MyApplication\MyService.exe" -Embedding (For registering the COM component hosted by service)
And to unregister my service and COM component hosted by it I am following below step:
"C:\Program Files\MyApplication\MyService.exe" -UnregServer
Q: Please let me know if steps to register and unregister the service and COM component it hosts, are correct or I am doing something wrong. Or I am missing something.
I don't have VISTA and Windows 7 on my machine and have not ever worked on them. So its important to confirm before I release my application.
Thanks in Advance
Regards
Aseem
|
|
|
|
|
Good day.
Is there an "efficient" way to check if a resource ID used in the code is effectively defined in the resource file ?
For example if a function is declared but not defined, the linker will report an error if the function is called.
I'd like to be able to report that if I do something like that :
#define IDS_MY_STRING 1234;
CString s;
s.LoadString(IDS_MY_STRING);
and IDS_MY_STRING is not in the RC file.
The compiler and/or the linker and/or the resource compiler will warn me about that.
Thanks.
Max.
Watched code never compiles.
|
|
|
|
|
You could use the "source browser" to check all references to IDS_MY_STRING .
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
As far as I know, it doesn't exists a way to do automatically what you are asking for.
What you can do in Visual Studio is: go to the Resource view of your project (you can use the shortcut Ctrl+Shift+E), then right-click on the root node of your resources (e.g. on myproject.rc) and choose the option Resource Symbols....
What you get is a list of all the identifiers defined inside the resource.h file, each one with its numeric value and a flag indicatin whether there exists or not a resource inside the .rc file that uses it. In the textbox named Used by: you have informations about which kind of resources exists that use the selected identifier.
|
|
|
|
|
Hello,
I would to develop an application in c++ to decode some messages.
The user can specify the value of each field and when a message arrives, it is decoded with the rules first specified.
e.g.
I have a message formed by 16 bit and at most I can have 32 messages in the same frame.
The user can add the rules to decode the frame.
Beginning the uses must specify the fields that form the frame and when the frame arrive, the application fills the fields specified by the user.
How can i implement this application?
is there some tool that simplifies the development?
Thank You,
Andrea.
|
|
|
|
|
The word message is overused in software, so that it doesn't mean anything at all outside its context. I guess you are talking about a couple of bytes which come from a certain source (e.g. a serial port).
AFAIK there's no library which could help you for that. However, this shouldn't be too complex to implement yourself. But it depends also what you have to do exactly: how does this parsing works ? Can you give a concrete example (with a concrete message and concrete rules) ?
|
|
|
|
|
thanks for your answer.
A message is represented as Int16 type.
The user, at run-time, can specify e.g (rule):
bit1 :on/off field
bit2 - bit5: address
bit 6 - 11: sub address
and so on...
another rule could be:
bit1: T/R field
bit2 - bit10 telemetry
and so on...
When i write code don't know the rules.
When the rules have been specified, the user select a particular rule and when arrive a message, the specified fields are filled according to the selected rule.
Thank You.
|
|
|
|
|
Andrea Di Domenico wrote: the specified fields are filled according to the selected rule.
I still don't get what those fields are. Are they simple variables ? Any particular type ?
As Carlo suggested, you might take a look at a scripting language, although this is perhaps a bit overkill for your case (that's what I'm trying to figure by asking you so many questions).
|
|
|
|
|
You may embed a scripting language in your application, like (the excellent) Lua. Then, users can formulate the decoding rules by implementing functions (or simple data structures) using the hosted language.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I am not sure how experienced you are in C++ but one thing you will find is that the address operator (&) does not work with bits of a byte. This will be a very important factor since you are configuring which bits to peel apart.
One thing I have done to help with this is I made functions to unpack the bits of a byte into an array of bytes and to also do the converse - pack the LSB of an array of bytes into one byte. Here is what the unpacker looks like :
void BitUnpack( uchar byteArray[], uchar bits )
{
for( uint n = 0; n < 8; ++n )
{
uchar mask = 1 << n;
byteArray[n] = ( bits & mask ) ? 1 : 0;
}
}
This will give you an array of bytes where each one corresponds to the value of the bit at that location. This means that byteArray[2] will be 1 if the 3rd bit is set and it will be 0 if not. If you unpack all of the bits of your message into array of bytes it will be easy to then repack, say, bits 3-7 into one value and so on.
Note that the function above unpacks just one byte. If you call this repeatedly you can unpack all of the bytes you receive into arrays for processing later. BTW - you may want to tweak the packer function to have it pack arbitrary numbers of bits to make things easier for you later. Here's the packer I use :
void BitPack( uchar byteValue, uchar byteArray[] )
{
for( uint n = 0; n < 8; ++n )
{
if( byteArray[n] )
{
uchar mask = 1 << n;
byteValue |= mask;
}
}
}
If you always work exclusively with 16-bit values then change the uchars to ushorts and the 8s to 16s and you should be ready to go. I should mention that I define the uchar, ushort, uint, and ulong types for myself but you don't have to use those since VC++ already defines the capitalized versions of these types.
What this all comes down to is sorting out the bits and then reassembling them later. I hope these functions give you some ideas on how to approach this.
modified on Tuesday, October 26, 2010 6:13 PM
|
|
|
|
|
Congrats! You invented std::bitset[^]
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
|
|
|
|
|
I doubt it. Those two functions were first written more than twenty years ago. 
|
|
|
|