• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ATC

More C++ stuff...

14 posts in this topic

Trying to do a bit of practical/academic C++ programming for something other than nonsensical purposes is really highlighting how rusty I've gotten in the last couple years of being a C#-exclusive programmer. C++ is such a big language though that I guess this is to be expected. It's really some of the quirks and oddities of the language that are giving me trouble. So here are a few more C++ questions I have...

[b]1)[/b] [u][i]Global operators and linker errors ::[/i][/u]

Why does it cause a linker error to prototype a global operator in a header file then implement it in a source file, like so::

[source lang="cpp"]
// point.h ::
Point operator +( const Point& p1, const Point& p2 );
Point operator -( const Point& p1, const Point& p2 );

// point.cpp ::
Point operator +( const Point& p1, const Point& p2 ) {
Point result = p1;
return ( result += p2 );
}

Point operator -( const Point& p1, const Point& p2 ) {
Point result = p1;
return ( result -= p2 );
}[/source]


[i]Error 1 error LNK2019: unresolved external symbol "struct ATCFramework::Drawing::Point __cdecl ATCFramework::Drawing::operator+(struct ATCFramework::Drawing::Point const &,struct ATCFramework::Drawing::Point const &)" (??HDrawing@ATCFramework@@YA?AUPoint@01@ABU201@0@Z) referenced in function _wmain C:\Users\ATC\Documents\Visual Studio 2012\Projects\ATCWARE\src\ConsoleTest\main.obj ConsoleTest[/i]

(NOTE:: I also get LNK1120, unresolved external symbols, of course...)

But if I just move the implementation of the operators into the header file it compiles and runs fine. What the deal?? [img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img]

[b]2) [/b][i][u]Getting meaningful information from an exception ::[/u][/i]

I first became aware of this trouble because I was throwing exceptions in my code if something went wrong like this ::

[source lang="cpp"]#include <stdexcept>

int wmain( int argc, wchar_t *argv[ ] ) {

throw new runtime_error( "You threw an exception on purpose." );

return 0x00;
}
[/source]


As a C# programmer, I'm wanting that handy little exception box to pop up in VS and say "You threw and exception on purpose.". But instead, I just get a bunch of memory addresses and a pretty picture of the call stack. I thought at first this might be because I'm using the "new" operator, which returns a pointer; and perhaps C++ was interpreting that as "throw unsigned long long" (or whatever, depending on native platform). So I changed it to just "throw runtime_error", hoping it would create a local runtime_error on the stack I could see... but I still get the same thing, and cannot obtain any meaningful information (like my exception message which tells you what the freakin problem is lol). I've dug through all the locals and memory in the debugger looking for this handy little tidbit of information but cannot find it lol. So how do I get any meaningful information from my exceptions?! [img]http://public.gamedev.net//public/style_emoticons/default/huh.png[/img]

[b]3) [/b][i][u]Checking for C++11 support ::[/u][/i]

Short question here... It seems to me that I should easily be able to check for C++11 support by doing something like this:

[i]#if defined(__cplusplus) && (__cplusplus >= 199711)[/i]

Seems to work... Does this seem correct/acceptable or is there a better way of doing it? [img]http://public.gamedev.net//public/style_emoticons/default/ph34r.png[/img]

[b]4) [/b][i][u]"POD" initialization of structures ::[/u][/i]

Afaik, you can only use POD initialization if you structure defines nothing other than data... no constructors, no methods, no operators, etc... But is there some way to work [i]around [/i]this? Suppose I wanted to make my own Vector3 structure that offered c'tors, methods and operators, but I also wanted to be able to initialize it like so: Vector3 pos = { x, y, z }; Is there not some fancy constructor/operator trick I can use? lol

Anyhow, enough questions for now... I'll wait for these to be answered before asking any more.

Thanks,

--ATC-- Edited by ATC
0

Share this post


Link to post
Share on other sites
[quote name='Madhed' timestamp='1350845592' post='4992519']
[quote name='ATC' timestamp='1350845223' post='4992515']
Vector3 pos = { x, y, z }
[/quote]

Why not

[code]
Vector3 pos(x,y,z);
[/code]
?
[/quote]

True... Didn't even think about that! [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

EDIT: Btw, I have no idea what's going on but the <code> feature on the forums won't work right for me... At least it's showing my code all comes out in one, mangled line in my above post... Edited by ATC
0

Share this post


Link to post
Share on other sites
[quote name='ATC' timestamp='1350845223' post='4992515']
[b]4) [/b][i][u]"POD" initialization of structures ::[/u][/i]

Afaik, you can only use POD initialization if you structure defines nothing other than data... no constructors, no methods, no operators, etc... But is there some way to work [i]around [/i]this? Suppose I wanted to make my own Vector3 structure that offered c'tors, methods and operators, but I also wanted to be able to initialize it like so: Vector3 pos = { x, y, z }; Is there not some fancy constructor/operator trick I can use? lol
[/quote]

Strictly speaking, that trick would be called C++11. However, even with this syntax it would actually call the constructor with these parameters and not directly initialize the variables. Since you are most likely using VS2012 and it's not supporting that new syntax yet, it's probably a moot point.
0

Share this post


Link to post
Share on other sites
Regarding your first question, I know it seems dumb but those errors happen all the time, did you actually compile and link the point.cpp file?
0

Share this post


Link to post
Share on other sites
Greatly appreciated, everyone, especially frob for such a thoughtful and detailed answer!

But is it not possible to even get/read the exception information at all?

throw logic_error( "This exception is stupid." );

Can I NOT [i]ever [/i]read that information ("This exception is stupid.")? It would be great if I could see that information when an exception is thrown whilst debugging... otherwise it can almost be impossible to tell what went wrong!
0

Share this post


Link to post
Share on other sites
You throw an object, you catch an object. In this case you threw a logic_error object. That is derived from the exception class.

You can attempt to catch an exception object, and if so you can use what() to get your message.

[CODE]
try
{
...
throw logic_error("This exception is stupid."); // Throw a logic_error object, which is derived from the exception class.
...
}
catch (exception& e) // If the thrown object can be cast to an exception object, catch it here.
{
cerr << "exception caught: " << e.what() << endl;
}
[/CODE]
2

Share this post


Link to post
Share on other sites
[quote name='ATC' timestamp='1350845991' post='4992521']
EDIT: Btw, I have no idea what's going on but the <code> feature on the forums won't work right for me... At least it's showing my code all comes out in one, mangled line in my above post...
[/quote]
You are using [[i]source[/i]] tags. You should be using [[i]code[/i]] tags.


[quote name='ATC' timestamp='1350845223' post='4992515']
[b]1)[/b] [u][i]Global operators and linker errors ::[/i][/u]

Why does it cause a linker error to prototype a global operator in a header file then implement it in a source file, like so::

[code]
// point.h ::
Point operator +( const Point& p1, const Point& p2 );
Point operator -( const Point& p1, const Point& p2 );

// point.cpp ::
Point operator +( const Point& p1, const Point& p2 ) {
Point result = p1;
return ( result += p2 );
}

Point operator -( const Point& p1, const Point& p2 ) {
Point result = p1;
return ( result -= p2 );
}[/code]


[i]Error 1 error LNK2019: unresolved external symbol "struct ATCFramework::Drawing::Point __cdecl ATCFramework::Drawing::operator+(struct ATCFramework::Drawing::Point const &,struct ATCFramework::Drawing::Point const &)" (??HDrawing@ATCFramework@@YA?AUPoint@01@ABU201@0@Z) referenced in function _wmain C:\Users\ATC\Documents\Visual Studio 2012\Projects\ATCWARE\src\ConsoleTest\main.obj ConsoleTest[/i]

(NOTE:: I also get LNK1120, unresolved external symbols, of course...)

But if I just move the implementation of the operators into the header file it compiles and runs fine. What the deal?? [img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img]
[/quote]
You did not fully qualify the definition of the operator in the .CPP file.
It should be:[code]
ATCFramework::Drawing::Point ATCFramework::Drawing::operator + ( const Point & p1, const Point & p2 ) {
Point result = p1;
return ( result += p2 );
}[/CODE]


L. Spiro Edited by L. Spiro
1

Share this post


Link to post
Share on other sites
[quote name='L. Spiro' timestamp='1350951718' post='4992944']
You are using [[i]source[/i]] tags. You should be using [[i]code[/i]] tags.
[/quote]

Hmmm... I've been clicking on the little code icon on the text editing toolbar (bottom row, farthest to the right -- to the right of the indent and alignment buttons). Is that not the right thing to be using?

[quote name='L. Spiro' timestamp='1350951718' post='4992944']
You did not fully qualify the definition of the operator in the .CPP file.
It should be:[code]
ATCFramework::Drawing::Point ATCFramework::Drawing::operator + ( const Point & p1, const Point & p2 ) {
Point result = p1;
return ( result += p2 );
}[/CODE][/quote]

I don't have to fully qualify other things, like my class/struct member methods, and I don't believe I've ever had this problem with free functions. What's the difference?

[quote name='frob' timestamp='1350949882' post='4992942']
You throw an object, you catch an object. In this case you threw a logic_error object. That is derived from the exception class.
You can attempt to catch an exception object, and if so you can use what() to get your message.
[/quote]

So it's only going to be useful if I [i]catch[/i] the exception? Hmmm, that kinda sucks... BTW, is the correct way to throw exception(...) or [i]new[/i] exception(...)? I've been operating under the assumption that using throw [i]new[/i] is like throwing type exception* which could be interpreted by the runtime as "throw unsigned long long" (or unsigned long in x86), since that's technically what a pointer is... Edited by ATC
0

Share this post


Link to post
Share on other sites
[quote name='ATC' timestamp='1350959555' post='4992964']
Hmmm... I've been clicking on the little code icon on the text editing toolbar (bottom row, farthest to the right -- to the right of the indent and alignment buttons). Is that not the right thing to be using?
[/quote]
To the left of the Quote icon is an icon that looks like “<>”. Use this.

[quote name='ATC' timestamp='1350959555' post='4992964']
I don't have to fully qualify other things, like my class/struct member methods, and I don't believe I've ever had this problem with free functions. What's the difference?
[/quote]
They were implicitly qualified by being inside “namespace ATCFramework { }”.

You have a class inside the ATCFramework namespace and declare an operator for it.
Inside the .CPP you start with “ATCFramework {” at the top, put all the class definitions next, and at the end you put “}”.
If those classes have operators it is not necessary to do this:

[CODE]bool ATCFramework::MyClass::operator == ( etc. ) {
}[/CODE]

The ATCFramework:: part is implicit.

However your Point class is nested inside another class.
If you are still inside the “namespace ATCFramework { }” area you still don’t need to add the “ATCFramework::”, but you do need to add both classes:
[CODE]bool MyClass::Point::operator == ( etc. ) {
}[/CODE]


L. Spiro Edited by L. Spiro
0

Share this post


Link to post
Share on other sites
[quote name='L. Spiro' timestamp='1350961610' post='4992972']
They were implicitly qualified by being inside “namespace ATCFramework { }”.

You have a class inside the ATCFramework namespace and declare an operator for it.
Inside the .CPP you start with “ATCFramework {” at the top, put all the class definitions next, and at the end you put “}”.
If those classes have operators it is not necessary to do this:

bool ATCFramework::MyClass::operator == ( etc. ) {
}


The ATCFramework:: part is implicit.

However your Point class is nested inside another class.
If you are still inside the “namespace ATCFramework { }” area you still don’t need to add the “ATCFramework::”, but you do need to add both classes:
[/quote]

Not sure if this changes anything but Point is not nested in another class... "Drawing" is actually a namespace nested in "ATCFramework".
0

Share this post


Link to post
Share on other sites
Then you would have to do:[CODE]
namespace ATCFramework {
namespace Drawing {

Point Point::operator + ( etc. ) {
}
}
}
[/CODE]
If you want to avoid fully qualifying things.



L. Spiro
0

Share this post


Link to post
Share on other sites
[quote name='ATC' timestamp='1350959555' post='4992964']
So it's only going to be useful if I catch the exception? Hmmm, that kinda sucks...
[/quote]
Er, yes, most C++ features are useful only if you use them. QED.

Did you think the language should require a dialog box with an OK button to be thrown up if you don't catch an exception? How's that going to work for the ARM chip controlling your automobile engine?
[quote]
BTW, is the correct way to throw exception(...) or new exception(...)? I've been operating under the assumption that using throw new is like throwing type exception* which could be interpreted by the runtime as "throw unsigned long long" (or unsigned long in x86), since that's technically what a pointer is...
[/quote]
Throw by value, catch by const reference. Do not create exceptions on the free store.

The runtime interprets type [i]pointer to ...[/i] as type [i]pointer to ...[/i], not as type [i]unsigned long long[/i]. There are many architectures in which an address can not be stored in a data register (or vice-versa) so don't assume anything about convertability between unrelated types.. C++ is indeed a strongly-typed language: don't expect types to decay to unrelated types arbitrarily.
0

Share this post


Link to post
Share on other sites
[quote name='Bregma' timestamp='1350971832' post='4992992']
Er, yes, most C++ features are useful only if you use them. QED.

Did you think the language should require a dialog box with an OK button to be thrown up if you don't catch an exception? How's that going to work for the ARM chip controlling your automobile engine?
[/quote]

That's not what I'm talking about... I [i]do[/i] expect an error message to pop up when I'm debugging in my IDE. [img]http://public.gamedev.net//public/style_emoticons/default/happy.png[/img]

[quote name='Bregma' timestamp='1350971832' post='4992992']
Throw by value, catch by const reference. Do not create exceptions on the free store.

The runtime interprets type pointer to ... as type pointer to ..., not as type unsigned long long. There are many architectures in which an address can not be stored in a data register (or vice-versa) so don't assume anything about convertability between unrelated types.. C++ is indeed a strongly-typed language: don't expect types to decay to unrelated types arbitrarily.
[/quote]

Gotcha, thanks!
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0