Weird way to write "hello world" [duplicate] - c++

This question already has answers here:
Closed 13 years ago.
Possible Duplicates:
Changing c++ output without changing the main() function
How to assign a method's output to a textbox value without code behind
How to write hello world without modifying main function?
Thanks
int main(){return 0;}

#include<iostream>
int hello() {
cout<<"Hello World"<<endl;
}
static int foo = hello();
int main(){return 0;}

Just add this code to a .cpp file somewhere.
class Hack {
Hack() { cout << "Hello World"; }
} hackInstance;

Use the preprocessor to #define an expansion for return to print hello world, then return.

I'm assuming there's a creative use of #define preprocessor statements that can make this work.

Related

error: cannot declare pointer to ‘void’ member [duplicate]

This question already has answers here:
Explanation of function pointers
(4 answers)
Closed 8 months ago.
I have the following scenario: I have a third party library and a header associated with it, that I'm using in my project. I completed my project and now I'm unit testing my code and I want to use a stub library, created by myself, for the third party library.
The header for the third party library contains structures that looks like this:
typedef struct{
int (*init)(int * var);
void (*close)(void);
} AInterface
To create my stub version, I created a .cpp file, in which I include the header, and started to implement it like this:
int AInterface::*init(int* var)
{
return 0;
}
void AInterface::*close(void)
{}
But when I compile my code I get the following error: "stub.cpp:76:33: error: cannot declare pointer to ‘void’ member
void AInterface::*close(void)"
I have searched the internet but with no success.
My questions are:
what I am doing wrong here?
is there another way to implement the stub?
I have also tried to implement the stub like this:
int initAInterface(int * var);
int (AInterface::*init)(int * var) = &initAInterface;
int initAInterface(int* var)
{
return 0;
}
But I get the following error:
error: cannot convert ‘int (*)(int*)’ to ‘int (AInterface::*)(int*)’ in initialization
int (AInterface::*init)(int* var) = &initAInterface;
Also I have no main in my stub .cpp file.
Those are function pointers and expect you to give static functions to them, then pass the struct to the library
//#################################################################
//Library header
typedef struct {
int (*init)(int* var);
void (*close)(void);
} AInterface;
//#################################################################
//A CPP file somewhere
//You might need extern "C" if you're mixing C and C++
#include "LibraryHeader.h"
int myInitFuction(int* var)
{
std::cout << "I wrote the init function for this" << std::endl;
return 0;
}
void myCloseFunction(void){
std::cout << "I wrote the close function for this" << std::endl;
}
//Or some other function doesn't need to be main if it's not the entry point
int main()
{
AInterface myInterface;
myInterface.init = myInitFuction;
myInterface.close = myCloseFunction;
SomeLibraryFuction(&myInterface);
}

Print function contents as string but also be able to run it as code

I would like to make a function that can print the contents of another function without breaking the functionality of the copied function.
For example:
int functionToCopy()
{
int a{ 5 };
int b{ 6 };
return a + b;
}
void printCopiedFunction()
{
some magical code to print the contents of the first function;
}
int main()
{
std::cout << functionToCopy() << '\n';
std::cout << printCopiedFunction() << '\n';
return 0;
}
Output:
11
int functionToCopy()
{
int a{ 5 };
int b{ 6 };
return a + b;
}
I'm only a beginner and C++ is my first language. I've done a lot searching and thinking but the only way I could think of is just literally copying the function and making a 2nd function a string, which would double my code and I'd rather avoid that. The program I'd like to do this with currently has 26 functions that would need copying like that so a single function that can be reused would be much preferred.
std::string copiedFunction()
{
std::string str{ R"(
int functionToCopy()
{
int a { 5 };
inb b { 6 };
return a + b;
})"
};
return str;
}
Any help is much appreciated! This is the only time I've ever asked for help like this on a forum but I think this is just beyond my abilities at this point. I understand this may not be possible or it may be very complex and just beyond my scope at this time. Thank you in advance!
C++ does not have reflection so you can't directly do this using the language alone. But you can do it by moving the function to a header file, #includeing that file in your .cpp file, and also building the header file into an object file which makes it contents available as a constant string.
First, put this in foo.h:
inline int functionToCopy()
{
int a{ 5 };
int b{ 6 };
return a + b;
}
Then, using the information from https://stackoverflow.com/a/46221837/4323 use objcopy to create an object file:
objcopy --input binary --output elf64-x86-64 foo.h foo.o
Then change your main file like this:
#include <iostream>
#include <string_view>
#include "foo.h"
extern "C" const char* _binary_foo_h_start; // defined in foo.o
extern "C" const char* _binary_foo_h_size;
std::string_view printCopiedFunction()
{
return {_binary_foo_h_start, _binary_foo_h_size};
}
int main()
{
std::cout << functionToCopy() << '\n';
std::cout << printCopiedFunction() << '\n';
}
And link foo.o into your executable at build time.
Note that objcopy is available on Linux and some other systems, you may need to look for an equivalent on your platform. For example if you have xxd: https://stackoverflow.com/a/411000/4323
On Windows you can embed the text file (which is the header file) as a "resource" in your executable, and load the resource at runtime, like this: Embed Text File in a Resource in a native Windows Application

C++ “undefined reference to“ in main method [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
It looks basic and it’s probably due to a beginner mistake but I can’t figure out why...
When compiling I get an error like below from int main():
“undefined reference to 'Hello::World::PaintService::PaintService()"
paint.cpp
using namespace Hello;
int main(int argc, char **argv) {
World::PaintService service;
service.start_painting(argc[1]);
}
PaintService and start_painting are defined like below:
paint_service.h
namespace Hello {
namespace World {
class PaintService {
public:
PaintService();
void start_painting(...);
}; } }
paint_service.cpp
namespace Hello {
namespace World {
void start_painting(....) {
... //paint
}
} }
It seems simple to call that start method like service.start_paint() in another class after calling PaintService service, but something is wrong. I have tried lots of variations yet couldn’t figure out :-/ Could someone point out what I’m doing wrong?
Thanks!
To declare a method, you need to include the name of the class.
namespace Hello {
namespace World {
void PaintService::start_painting(....) { ... }
void PaintService::PaintService() { ... }
} // namespace World
} // namespace Hello
https://repl.it/repls/PunySaneSpools#main.cpp

how can i use CLIPS source code in my C++ project?

first,i changed the source code's extension to .cpp. And add them all into my C++ project.
the source files.jpg
second.im my main func,i code this:
namespace ClipsEmbed{
int main(){
Environment *theEnv;
theEnv = CreateEnvironment();
cout << Load(theEnv, "auto.clp");
Reset(theEnv);
Run(theEnv,-1L);
return 0;
}
}
after shooting some link and compile erro,it runs ok,but it just a CLIPS IDE,not my program.
just like this:
the result .jpg
i guess there is another main function,but i did not know where,so please help.how can i use the source code not the DLL in c++ project.
Your explanation is very confused and omitting details, but I think what you need is (at the simplest level):
namespace ClipsEmbed{
int main(){
Environment *theEnv;
theEnv = CreateEnvironment();
cout << Load(theEnv, "auto.clp");
Reset(theEnv);
Run(theEnv,-1L);
return 0;
}
}
int main () { return ClipsEmbed::main(); }
The main() is inside the ClipsEmbed namespace, so to satisfy the linker you need a plain main() in the global namespace, so the simplest solution is as illustrated, calling the namespace's main() from the global main().
Following feedback:
extern "C" int main () { return ClipsEmbed::main(); }
Might help?

irrklang engine was not declared at void [duplicate]

This question already has answers here:
How to declare a global variable in C++
(5 answers)
Closed 7 years ago.
EDIT 2:
Solved! Use the code below and it worked!
irrklang::ISoundEngine* engine = irrklang::createIrrKlangDevice();
Just place the code above at the top of the code. (Maybe the next line of include or namespace)
I'm using irrKlang to play audio but I had a problem:
#include <irrKlang.h>
void playSound() {
engine->play2D("src/Click.wav");
}
int main() {
irrklang::ISoundEngine* engine = irrklang::createIrrKlangDevice();
playSound();
engine->drop();
return 0;
}
When I run this code, it show that 'engine' (that in the void) was not declared in this scope.
I test this at int main but it work. The problem is that it only worked at main but not at void.
Anything I can use to fix this error? Or is it a bug?
Thanks in advance.
That is expected. irrklang::ISoundEngine* engine is defined in main function but not in playSound().
A straightforward solution would be to pass engine as an argument
void playSound(irrklang::ISoundEngine* engine) {
engine->play2D("src/Click.wav");
}
and in main call it like this
playSound(engine);