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);
Related
This question already has answers here:
C++ display stack trace on exception
(16 answers)
Closed 8 years ago.
I'm wondering is there any predefined macro or something in C++ that could possible to trace back where the destructor is triggered?
It could be something like this:
class myClass{
myClass();
~myClass();
};
myClass::~myClass(){
printf("Object destroyed in %s.\n", __TRACEBACKMACRO__);
}
int main(){
myClass tempClass;
return 0;
}
It should output something like this:
Object destroyed in main().
It's better to output the scope and namespace information as well.
Additional information:
FUNCTION or func macro seems only work in functions not in structs and classes. reference. Any macros that work in struct and class?
You could use the backtrace library though the avalability depends on the platform and compiler:
with gcc compiler (Linux or MacOS X) to display the stacktrace in C++:
include "execinfo.h" and use backtrace -> backtrace_symbols -> __cxa_demangle
with Windows:
include StackWalker.h and use StackWalker class
Have a look at this article http://oroboro.com/stack-trace-on-crash/ for ulterior details.
Alternatively you could use Boost.Call_stack: http://melintea.github.io/Boost-Call_stack/index.html
This question already has answers here:
Why doesn't ADL find function templates?
(4 answers)
Closed 9 years ago.
Could someone please explain why the following code is giving error (error C2065: 'select' : undeclared identifier) at compile time:
namespace N {
class MyClass{
};
template<int I> void select(MyClass*)
{}
}
void g (N::MyClass* mp)
{
select<10>(mp);
}
void main()
{}
According to Argument Dependent Lookup, this should work fine, since I have specified N:: in `g``s argument. So, select should be visible to compiler.
Why does ADL not work here?
have you tried N::select?
either that or a
using namespace N
should work since simply select is not visible
Any time you utilise a class from a divergent namespace from the one you are currently in you must either reference it directly (N::select) or set up a using namespace (using namespace N;) or set up a direct using statement to it for future use (using N::select)
For disambiguation I would look at this and this , which between them should give you a good start on how/why you cannot simply call select.
Cheers, and feel free to get a hold of me for more info.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
I have a game program and I am getting VERY frustrated. Everything was running fine, and I decided to clean up my program by making separate files for each set of functions. The code is very long with multiple files, but heres the basic idea:
Im on Windows XP using the Code::Blocks IDE
In my entity.h Ive declared all of my functions and variables for that class. In my entity.cpp Ive included it, as well as in all my other files. But Im still getting a huge list of errors that tell me I have an undefined reference to all of the methods in entity.h as well as all my other header files. For example, I have a function call print() to make it easier to print out things, and thats the first method I call from the entity.h file. I get this error:
Heres the code for print():
void print(string f) {
cout<<f<<endl;
}
How Im calling it:
void Player::win(){
entity e;
e.print("You have defeated the orc");
}
The error:
In function 'ZN6Player3winEv': undefined reference to 'entity::print(std::string)'
And yes, I do have an object of entity.
Its also happening for every single other function in the entity class and file.
void print(string f) {
cout<<f<<endl;
}
should be
void entity::print(string f) {
cout<<f<<endl;
}
void print(string f) {
cout<<f<<endl;
}
is a global function
if you want to call
e.print("You have defeated the orc");
then you need an implementation for
void entity::print(string f)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's this C++ syntax that puts a brace-surrounded block where an expression is expected?
I've just come across this strange C/C++ syntax:
#include <stdio.h>
int main() {
printf("%s",
({
static char b__[129];
b__[0] = 55;
b__[1] = 55;
b__[2] = 0;
b__;
})
);
}
This compiles and runs fine using both gcc and g++ (4.5.2). This is the first time I see something like this, and I wonder what exactly this syntax means. I've tried to Google it, but I have no idea what this construct is called.
They're called statement expressions, it's a GNU extension. In your example the result of the expression is b__.
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.