|
as it displayed in registry !
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Hello everyone,
The compile error of the following code is,
foo.cpp(7) : error C2146: syntax error : missing ';' before identifier '_'
foo.cpp(7) : error C2065: '_' : undeclared identifier
foo.cpp(7) : error C2143: syntax error : missing ';' before 'string'
I do not know why there is errors. I think in MERGE macro, the value of parameter a is hello and the value of parameter b is world in my case, and I defined hello_world to "H E L L O", it should be fine...
Any ideas?
<br />
#define MERGE(a,b) a ## _ ## b<br />
#define hello_world "H E L L O"<br />
<br />
int foo()<br />
{<br />
char* p0;<br />
p0 = MERGE("hello", "world");<br />
<br />
return 0;<br />
}<br />
thanks in advance,
George
|
|
|
|
|
This macro invocation expands to the following:
"hello"_"world"
This is not a legal C++ statement.
Steve
|
|
|
|
|
Thanks Steve,
I am a little confused. When I input MERGE ("hello", "world"), since the definition of MREGE (a, b) is a##_##b, and the value of parameter a is hello and the value of parameter b is world, the result should be hello_world, what is wrong with my above analysis? Why it will result in "hello"_"world"?
regards,
George
|
|
|
|
|
Firstly this page on Token-Pasting[^] may be of interest; a Google[^] search on the topic may also be of interest. If you wanted a macro to do what you're after this would do the trick (I'm not sure why you'd want one however):
#include <iostream>
using namespace std;
#define MERGE(f, s) f s
void main()
{
cout << MERGE("Hello ", "world!") << endl;
}
Steve
|
|
|
|
|
Thanks Steve,
It is my pure technical interest.
Your code works, cool!
regards,
George
|
|
|
|
|
If my guess is correct, you're trying to do something like the following:
#define MERGE(a,b) a##_##b
#define hello_world "H E L L O"
int main()
{
char* p0;
p0 = MERGE(hello, world);
printf("%s\n", p0);
return 0;
}
the above code compiles and sets p0 = " H E L L O"
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.
|
|
|
|
|
Thanks CPallini,
Your code works. But why my code is not working? I just changed MERGE (hello, world) to MERGE ("hello", "world").
I think if I input MREGE ("hello", "world"), then the result will be hello_world by the definition of MERGE macro, then by the hello_world macro, the result should be "H E L L O".
What is wrong with my code?
regards,
George
|
|
|
|
|
George_George wrote: What is wrong with my code?
The double quotes " .
If you need to build the hello_world macro then you have to merge hello with world .
As it stands, your code produces "hello"_"world" that is just a sintax error.
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.
|
|
|
|
|
Thanks CPallini,
Cool!
regards,
George
|
|
|
|
|
The best way to solve such problems is to see the code as the compiler is seeing it. Compile with the /P or /E switch and look at the preprocessor output.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks DavidCrow,
It works! Cool!
regards,
George
|
|
|
|
|
I have the following code to read data from a list of files. The code read the first file and read the data correctly. I used break point debug and found at "fclose(f_ptr2)" giving error: "Access Violation"
//
FILE *f_ptr2;
//
//
for (int ii=0;ii
|
|
|
|
|
Review and correct your post: the for loop can't be properly seen and some indentation wouldn't hurt either.
Steve
|
|
|
|
|
Also, do you check the return value from fopen()?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi,
I am using SetupDiGetClassDevs() API to retrieve some information related to Battery, but while using "GUID_DEVICE_BATTERY" macro, Visual Studio 8 gives linker error that "unresolved external symbol _GUID_DEVICE_BATTERY".
What could be reason for this?
I have verified for appropriate libraries.
Whenever I build this project in VC++ 6.0, it gets build successfully.
|
|
|
|
|
I have looked for the symbol myself in the LIBs that come with Visual Studio 2005 and, sure enough, it wasn't there.
So I guess you'd have to define it yourself:
const GUID GUID_DEVICE_BATTERY = {0x72631e54L, 0x78A4, 0x11d0, {0xbc, 0xf7, 0x00, 0xaa, 0x00, 0xb7, 0xb3, 0x2a }};
Florin Crişan
|
|
|
|
|
GUID_DEVICE_BATTERY is defined in the BatClass.h file and internally uses the DEFINE_GUID macro. You must include <initguid.h> in one of your source files before you include BatClass.h.
Judy
|
|
|
|
|
I am working on hashing homework code and i have hit a wall.
The first time through a while loop, it works fine, but every time after that, it is not working correctly.
The output currently looks like:
ending address: Cluster size:
4 5 <- this is correct
97 0 <- this should be 1
116 0 <- this should be 1
241 5 <- this should be 6
Here is the code i wrote:
void HashTable::clusterCount(void)
{
unsigned long numClusters=0;
unsigned long clusterLength=0;
unsigned long i=0;
unsigned long totalLength = 0;
while(i<tableSize)
{
//startCluster=clusterLength;
clusterLength = 0;
while((i<tableSize)&&(hashTableArray[i]!=0)){
clusterLength++;
i++;
}
totalLength += clusterLength;
cout<< (i-1) <<" "<< clusterLength <<endl;
while((i<tableSize)&&(hashTableArray[i]==0))
{
i++; // increments i when the array address is empty
}
numClusters++;
i++;
}
cout<<"average cluster Length "<<totalLength/numClusters<<endl;
cout<<"numClusters "<<numClusters<<endl;
}
|
|
|
|
|
i has been changed in inner while loop.
Regards,
Paresh.
|
|
|
|
|
D'oh!! Too many i++
Arrg.. ive been looking at that for 2 hours and i missed it.
Thanks!!!
~Drew
|
|
|
|
|
Regards,
Paresh.
|
|
|
|
|
Hello all,
I am making client server application in c++.
I am using following lines of code.
int m_sock = socket(AF_INET, SOCK_STREAM, 0 );
but m_sock contains -1 and it is not creating socket for me.
Any body know how can i solve this problem?
Thanks in advance
Manish Patel.
B.E. - Information Technology.
|
|
|
|
|
My reply is a little off topic: don’t use the “m_” Hungarian prefix on local variables! It’s meant to indicate class members.
Steve
|
|
|
|
|
its ok but whats the solution for me.?
Manish Patel.
B.E. - Information Technology.
|
|
|
|