I discovered this in the source code for the Unreal Engine 4 and didn't recognize it. The specific instance of it is:
#undef UCLASS
#define UCLASS(...) \
ARadialForceActor_EVENTPARM
I'm a fairly new programmer and this kind of macro is unfamiliar to me. My guess was that it is defining a macro that will take every overloaded version of the function UCLASS (in other words, every instance of a function named UCLASS, regardless of the type and number of parameters) and replace it with the variable ARadialForceActor_EVENTPARM. Is that correct? If not, does anyone know what it means? I tried writing a code snippet to test this, but it returned error: "." may not appear in macro parameter list. I'll include it below, in case I was right about the macro, in which case I would appreciate it if someone could point out where I went wrong in my code:
#include <iostream>
#define foo( . . . ) bar
using namespace std;
int foo() {cout <<54321<<endl;}
int main()
{ bar=12345;
cout<<bar<<endl;
return 0;
}
Your guess of the meaning of #define foo(...) bar is correct. Your error is in thinking that . . . and ... are the same; they are not. ... must be written with no spaces between the dots. If I modify your program to read
#include <iostream>
#define foo(...) bar
using std::cout;
int foo()
{
cout << 54321 << '\n';
}
int main()
{
bar = 12345;
cout << bar << '\n';
return 0;
}
(please take note of other stylistic corrections; future readers of your code will thank you)
then I get error messages consistent with foo() having been replaced with bar in the function definition, as if you had written
int bar
{
cout << 54321 << '\n';
}
which is, in fact, what this tells the preprocessor to do. (The preprocessor, as always, has no understanding of the syntax of the language proper.)
#define FOO(...)
this means your macro can accept variable number of arguments. See here for details.
Related
I've looked at a couple other questions asking this, but mine seems to be a lot simpler of a case then the ones I've been through, so I'll ask my case for this.
Learn.h:
#ifndef LEARN_H
#define LEARN_H
class Learn
{
public:
Learn(int x);
~Learn();
private:
const int favourite;
};
#endif
Learn.cpp:
#include "Learn.h"
#include <iostream>
using namespace std;
Learn::Learn(int x=0): favourite(x)
{
cout << "Constructor" << endl;
}
Learn::~Learn()
{
cout << "Destructor" << endl;
}
Source.cpp:
#include <iostream>
#include "Learn.h"
using namespace std;
int main() {
cout << "What's your favourite integer? ";
int x; cin >> x;
Learn(0);
system("PAUSE");
}
The above code in itself does not output any error.
However, I do get a couple errors after I replace Learn(0) with Learn(x). They are:
Error E0291: no default constructor exists for class Learn
Error C2371: 'x' : redefinition; different basic types
Error C2512: 'Learn' : no appropriate default constructor available
Any reason for this? I really want to actually input the integer variable x inside it rather than the 0. I know this is only practice and I'm new to this, but really, I'm a little confused as to why this doesn't work.
Any help would be appreciated, thanks.
Parsing issue:
Learn(x);
is parsed as
Learn x;
You should use
Learn{x};
to build your temporary or
Learn some_name{x};
//or
Learn some_name(x);
if you want an actual object.
Okay, I figured out the problem I was having. I didn't realize that the call is done as part of an object assignment. The notation in C++ seems to be a bit different that way.
So Learn(x) should be replaced with Learn obj(x) apparently.
This notation is a little off from other programming languages where you can usually write className(inputs) variableName.
I had similar code and came up with a different errors because I was compiling with Linux in a terminal. My errors were:
error: conflicting declaration 'myObject str'
error: 'str' has a previous declaration as 'const string str'
when I tried myObject(str);
Sure Learn{x}; (and in my case myObject{str};) will successfully compile, but it creates a temporary object, then destroys it in the same line.
This is not what I wanted to do. So an alternative solution is to create a new object like:
Learn var_name = new Learn(x);
This way you can reference it for future use.
So i just learned how to seperate classes and the youtube totourial is stressing on doing this alot, here's the link
https://www.youtube.com/watch?v=NTip15BHVZc&list=PLAE85DE8440AA6B83&index=15
My code is the exact same as his, and in the cpp file theres this thing:
mainClass::myfunction; (mainclass is the name of my class, myfunction is my function)
when i try to execute my program, it gives an error:
unidentified reference to 'mainClass::myfunction()'
here's my main.cpp file code:
#include <iostream>
#include "mainclass.h"
using namespace std;
int main()
{
mainClass bo;
bo.myfunction();
return 0;
}
here's my mainclass.h code:
#ifndef MAINCLASS_H
#define MAINCLASS_H
class mainClass
{
public:
myfunction();
};
#endif // MAINCLASS_H
my mainclass.cpp:
#include "mainclass.h"
#include <iostream>
using namespace std;
mainClass::myfunction()
{
cout << "I am a banana" << endl;
}
I don't know much about these so could you just tell me what the errors here are, because i copied everything correctly from the guy's totourial but still it doesn't work
P.S: this happens to me alot, i understand everything, nothing works, i copy everything, nothing works, and then i literally do exactly what the person is doing, still nothing works on all three of PC's, so i dont think the problem is with the devices lol
I doubt you completely copied and pasted that code because I'm fairly sure a teacher shouldn't be teaching having functions without a specified return type, but let's jump into it anyways...
Possibility #1
You meant to create a constructor for the class. In that case, please make sure the constructor function has the same name as the class. Also, you can't call it through .mainClass(), as it is a constructor.
class mainClass
{
public:
mainClass();
};
mainClass::mainClass()
{
cout << "I am a banana" << endl;
}
Possibility #2 You meant to create the class member function myfunction. You really should be specifying what return type your function is of. Some compilers will auto-assume int return type, and so the function you created is int myfunction();, but you really should be specifying it as void myfunction(); since you didn't return anything. Addl. info: Does C++ allow default return types for functions?
Next, change how you are giving the definition, by adding the return type.
void mainClass::myfunction()
{
cout << "I am a banana" << endl;
}
Possibility #3 Those should work, but another issue is that you might not have linked mainclass.cpp, so there is no definition available. In code blocks, right click on the project name and hit Add Files, then add the mainclass.cpp so the linker can define mainClass::myfunction().
To troubleshoot if the mainclass.cpp is being built with the project, try adding
#error I'm included! to the file mainclass.cpp after #include "mainclass.h". If you get an error I'm included!, then it is linked and you can remove the #error.
I want to mangle a name passed to a macro to use it as a name of a function. For example I have a macro called foo as below:
#define foo(name) void name(const string& in) { \\do something }
But I want this macro to be called with parameter like this foo(Bar::do) so I have to change the name to the proper one to use it as a function.
Is there any way to able to implement this? I use c++11 and g++5.x.
Edit:
The problem that I encountered is because of having an old version of a macro, that it could be used with no restriction, so now I have many parts in my code that uses a macro such as foo(Bar::do), but now I have to generate a function using these names but the function names must be distinctive, so I decided to use macro input parameter as a function name, but I encountered the problem I explained.
This is an example:
void Bar::do(some_parameters)
{
FOO(Bar::do);
}
This is the part of code that I had before, now I want to add a second macro above this part, like this:
FOO2(Bar::do)
void Bar::do(some_parameters)
{
FOO(Bar::do);
}
The both names must be the same, but the FOO2 must generate a function based on the input parameters.
Edit2:
I try here to explain the problem more accurate.
Before, I had a macro like this:
#define FOO(name) \
some codes here
But now I want that have a macro which able to expand as below:
#define FOO2(name) void proper_fcn_name(name)(const string &name){ do the same thing here}
#define FOO(name) proper_fcn_name(name)(#name)
Which I can use that like this:
FOO2(some_name)
void Bar::fcn()
{
FOO(some_name);
}
And if some_name was in the format of Bar::fcn I want that proper_fcn_name changes that to for example Bar_fcn. In final I wish to have something like this, if some_name was Bar::fcn:
void Bar_fcn(const stirng& name) { }
void Bar::fcn()
{
Bar_fcn("Bar::fcn");
}
I must mention that Bar::fcn is just a name but I have to consider this because my library users used this style before.
I was interested by your question and made the following example:
#include <iostream>
#include <string>
using namespace std;
#define foo(name) void name (const string& in) { cout << in << endl; }
// function with name bar_do
foo(bar_do)
// function with name bar_do
foo(do_bar)
int main(void)
{
bar_do("The first test");
do_bar("The second test");
}
In fact it works (it is the output),
The first test
The second test
but I cannot understand what do you want from that strange usage of macro definition - now that looks like automatic copy-paste approach for producing identical functions with different names.
UPDATE
If you really want to use namespace (e.g. Bar) consider the following example:
#include <iostream>
#include <string>
using namespace std;
#define foo(name) void name (const string& in) { cout << in << endl; }
namespace Bar{
void func(const string& in);
}
foo(Bar::func)
int main(void)
{
Bar::func("The special test");
}
You should understand, that namespace have to be defined, and do is keyword of C++ language (so, you should not use it as function name)
UPDATE 2
If it is possible to change FOO(Bar::do); to FOO(Bar,do); in your old code, you can do simple substitution to generate Bar_do identifiers for call and for definition:
#include <iostream>
#include <string>
using namespace std;
#define defgen(class_name,method_name) void class_name##_##method_name(const string& in) { cout << in << endl; }
#define callgen(class_name,method_name,...) class_name##_##method_name(__VA_ARGS__);
// produses definition
defgen(Bar,do)
int main(void)
{
// produces call with particular value as an argument
callgen(Bar,do,"The call test");
}
kfsone said (in comments to your question):
MYMACRO(A::b) might produce A_b_stub
but unfortunately I do not know how (and suppose that it is impossible) to do replacement of :: with other characters (e.g. _).
I don't think it can be stressed enough that you should not do this and this sounds like there's an XY problem here.
You should probably explain the problem in full, why are you trying to generate this function, how is it used, etc.
Anyway enough preaching, here's a work-around:
#include <iostream>
#include <map>
#include <string>
#include <functional>
std::map<std::string, std::function<void(const std::string&)>> g_functions;
#define FOO(name) static void* nonsense##__COUNTER__ = \
(void*)&( g_functions[ #name ] = \
[](const std::string& in) \
{ \
std::cout << "generated function for : " << #name \
<< " called with arg:" << in << std::endl; \
});
#define FOO2(name) {g_functions[ #name ](#name); }
namespace Bar
{
FOO(Bar::func)
void func()
{
FOO2(Bar::func);
}
}
int main() {
Bar::func();
return 0;
}
If your compiler doesn't have __COUNTER__ you can sort of use __LINE__ most of the time (what is one more ugly hack on top of this pile).
I am learning C++ macro. And now I am totally confused. So help me! Below is my code, what I am trying to do is to use the C++ macro and call different functions using "template" such that I could only write one function and this function can be used to do the same things for different types. But when I compile this code, it throws the following errors.
testCPP.cpp: In function 'void test_int_Macro(int)':
testCPP.cpp:14: error: a function-definition is not allowed here before '{' token
testCPP.cpp:26: error: expected `}' at end of input**
#include<iostream>
#include<cstdint>
using namespace std;
#define Query_Data(Type)\
void test_##Type##_Macro(Type data){ \
cout<<"Test: "<<sizeof(data)<<" "<<endl; \
//cout<<"Type is "<<##Type##<<endl;\
}
Query_Data(int)
Query_Data(char)
int main(){
cout<<sizeof(unsigned)<<endl;
cout<<sizeof(char)<<endl;
cout<<sizeof(int32_t)<<endl;
int num=6;
char c='a';
Query_Data(num);
//Query_Data(c);
}
In C++, macros are very simple and stupid. The proeprocessor simply regurgitates whatever the macro is defined to be wherever you invoke it.
If you do the preprocessing on a piece of paper, what you come up with is:
int main(){
cout<<sizeof(unsigned)<<endl;
cout<<sizeof(char)<<endl;
cout<<sizeof(int32_t)<<endl;
int num=6;
char c='a';
void test_num_Macro(Type data){
cout<<"Test: "<<sizeof(data)<<" "<<endl;
//cout<<"Type is "<<##Type##<<endl;
}
}
The compiler gave you an error message which in this case was spot-on:
testCPP.cpp:14: error: a function-definition is not allowed here before '{' token
Look at the main function you end up with as a result of the preprocessing. It has a function declared within it. That, obviously, isn't allowed.
Now, if we follow this train of thought to the next logical stage, the question becomes "so how do I achieve what I'm trying to achineve?"
You said that:
what I am trying to do is to use the C++ macro and call different
functions using "template" such that I could only write one function
and this function can be used to do the same things for different
types.
And there is a facility in C++ for exactly this. Coincidentally, they are called templates. Here's how you might use one here:
template <typename Val>
void test ()
{
cout << "Test: " << sizeof (Val) << " " << endl;
}
int main(){
cout<<sizeof(unsigned)<<endl;
cout<<sizeof(char)<<endl;
cout<<sizeof(int32_t)<<endl;
int num=6;
char c='a';
test <char> ();
test <int> ();
}
Macros are a cludge. The are not sophisticated, they skirt around the C++ type system, they are very hard to debug and maintain. It is generally advised to avoid using them unless you have no other choice. There are places when you do have no other choice -- but this isn't one of them.
John Dibling is quite right. But I would like to point out that the easiest way to debug preprocessor issues is usually by running g++ -E src.cpp which shows you the file after it's been preprocessed. (remove your #include's first or you'll get way too much output)
And the reason for the the "expects '}' at end of input" error is that commented line in your macro. The preprocessor treats it as an empty line (without a trailing ) and so doesn't look farther and leaves your '}' out of the define.
You should avoid using the preprocessor in C++ for anything but #include and include guards in header files.
Macros are simple text replacement snippets, so you will end up defining those functions inside main function, a thing you cannot do in C++. You can see it very well if you run your code through the preprocessor:
void test_int_Macro(int data){ cout<<"Test: "<<sizeof(data)<<" "<<endl;
void test_char_Macro(char data){ cout<<"Test: "<<sizeof(data)<<" "<<endl;
int main(){
cout<<sizeof(unsigned)<<endl;
cout<<sizeof(char)<<endl;
cout<<sizeof(int32_t)<<endl;
int num=6;
char c='a';
void test_num_Macro(num data){ cout<<"Test: "<<sizeof(data)<<" "<<endl;;
}
Thus said, you should (and probably, you already got it) use templates:
template <class Type>
void Query_Data(Type data) {
std::cout << "Test: " << sizeof(data) << " " << std::endl;
}
If you wish, you can specialize the template for specific types like:
template<>
void Query_Data<int>(int data) {
std::cout << "This is an integer!" << std::endl;
}
and use them as intended:
int main(){
cout<<sizeof(unsigned)<<endl;
cout<<sizeof(char)<<endl;
cout<<sizeof(int32_t)<<endl;
int num=6;
char c='a';
Query_Data(num);
Query_Data(c);
}
This is a small program :
#include <iostream>
using namespace std;
int main() {
f();
system("pause");
}
void f() {
static int x = 20 ;
class tester {
public :
tester() {
cout << x ;
}
} x1;
}
The error that i get here is :error C3861: 'f': identifier not found
If i place the function f above main I will get the desired output.
Why it is so ?
I was told that program execution begins at main. According to this the code should run in the first case also.
How does the compiler start reading the program?
The beginning of the compilation and the beginning of the execution of the program are two different things.
The execution starts from the main.
The compilation begins from the beginning of the file; the compiler don't "jump around" the file to find the needed pieces, but it reads the input in a linear fashion (I suspect that this related, among the other things, to the fact that the C++ grammar is really complicated).
When the compiler is at some point in parsing the file, it only knows what has been declared/defined up to that point1.
Because of this, function prototypes (and non-defining declarations in general) have been invented: the prototypes of all the functions defined in the file are put at the beginning of the file, typically after the #include directives or in a separated include file. The prototypes tell to the compiler that such functions will be defined later, and what is the function signature (i.e. name, parameters, return value).
The prototype is made as a normal function, but without the body, which is replaced by a semicolon2. For example, in your code you would write
void f();
before the main.
IIRC there are some relaxations to this rule that allow the compiler to "wait" for some declarations to make some template magic work, but this is not relevant here.
In a prototype is also common not to write the names of the parameters, leaving just their type (this can be done also in function definitions, but it doesn't make much sense there unless you have a formal parameter you don't use). Still, I prefer to leave the parameter names there as a form of documentation.
I was told that program execution begins at main.
And that's exactly the point.
The compiler starts from main, and then sees a call to f(), which it has not encountered so far (as it is defined afterwards), so it does not know what to do with it.
If you want to define f after main you can place a function prototype before, such as
#include <iostream>
using namespace std;
void f(); // <--- This tells the compiler that a function name f will be defined
int main() {
f();
system("pause");
}
void f() {
static int x = 20 ;
class tester {
public :
tester() {
cout << x ;
}
} x1;
}
To be able to call a function it must have been declared at some earlier point in the code. This is just a rule of the language designed to help compilers.
You can declare the function earlier with e.g.
void f();
...and then define it after main as you have done.
The compiler starts at the top and reads down to the bottom.
you'll need to have something like:
#include <iostream>
using namespace std;
void f();
int main() {
f();
system("pause");
}
void f() {
static int x = 20 ;
class tester {
public :
tester() {
cout << x ;
}
} x1;
}
No, the compiler needs to see at least a declaration of f() before it is used. A c(++) code file is a simple text file and must be read from begin to end by the compiler.
During the compilation process, when the compiler is evaluating main() it needs to know what f() is in advance to be able to generate the correct assembly code to call this function. That's why you need to put it before main() in this case.
As an alternative you can declare the prototype of f() before main() so the compiler knows it's a local function declared somewhere else on your file:
void f(); // prototype
int main()
{
// .. code ..
}
void f() // implementation of f()
{
// .. code ..
}