View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# More C++ stuff...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

14 replies to this topic

### #1ATC  Members

Posted 21 October 2012 - 12:47 PM

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...

1) Global operators and linker errors ::

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]

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

(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??

2) Getting meaningful information from an exception ::

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?!

3) Checking for C++11 support ::

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

#if defined(__cplusplus) && (__cplusplus >= 199711)

Seems to work... Does this seem correct/acceptable or is there a better way of doing it?

4) "POD" initialization of structures ::

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 around 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, 21 October 2012 - 12:49 PM.

_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

___________________________________________________________________________________

Posted 21 October 2012 - 12:53 PM

Vector3 pos = { x, y, z }

Why not

Vector3 pos(x,y,z);

?

Edited by Madhed, 21 October 2012 - 12:53 PM.

### #3ATC  Members

Posted 21 October 2012 - 12:59 PM

Vector3 pos = { x, y, z }

Why not

Vector3 pos(x,y,z);

?

True... Didn't even think about that!

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, 21 October 2012 - 01:01 PM.

_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

___________________________________________________________________________________

### #4Trienco  Members

Posted 21 October 2012 - 10:34 PM

4) "POD" initialization of structures ::

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 around 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

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.
f@dzhttp://festini.device-zero.de

Posted 22 October 2012 - 01:55 PM

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?

### #6frob  Moderators

Posted 22 October 2012 - 02:39 PM

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...

1) Global operators and linker errors ::

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:: ... But if I just move the implementation of the operators into the header file it compiles and runs fine. What the deal??

This is one of the quirks that evolved over time.

In the original proto-c++, you would provide a forward declaration that basically said "I'm going to make a function and here is the signature". Then at link time, the linker would go through all the libraries and find the function that matches the signature. The function declaration could exist in every file since it was just a placeholder; but the function definition could only exist in a single source file.

Later, there came along inline functions and template functions and such. Instead of just using the name, they wanted to use the code in place. These are called "inline functions". They require certain rules in order to work properly, which Google can explain better than a single forum post.

2) Getting meaningful information from an exception ::

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.

Exceptions were not part of the earlier proto-c++ language. They were bolted on to the side of the language to try to fix some problems. C++ is a language of not paying for stuff you don't use. You are correct that they don't give you much pretty information, in large part because that is generally not used. If you want to include that information you need to provide it yourself into your exception.

3) Checking for C++11 support ::

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

#if defined(__cplusplus) && (__cplusplus >= 199711)

Seems to work... Does this seem correct/acceptable or is there a better way of doing it?

Yes.

4) "POD" initialization of structures ::

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 around 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?

As others pointed out, use a constructor with parameters, the same way you would with c#.
Vector3 pos(x,y,z);

You can do this with c++ structures as well. A c# structure is a very different thing than a c++ structure.

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #7ATC  Members

Posted 22 October 2012 - 04:56 PM

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 ever 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!
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

___________________________________________________________________________________

### #8frob  Moderators

Posted 22 October 2012 - 05:51 PM

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.

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;
}


Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #9L. Spiro  Members

Posted 22 October 2012 - 06:21 PM

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...

You are using [source] tags. You should be using [code] tags.

1) Global operators and linker errors ::

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::

// 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 );
}

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

(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??

You did not fully qualify the definition of the operator in the .CPP file.
It should be:
ATCFramework::Drawing::Point ATCFramework::Drawing::operator + ( const Point & p1, const Point & p2 ) {
Point result = p1;
return ( result += p2 );
}

L. Spiro

Edited by L. Spiro, 22 October 2012 - 06:23 PM.

### #10ATC  Members

Posted 22 October 2012 - 08:32 PM

You are using [source] tags. You should be using [code] tags.

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?

You did not fully qualify the definition of the operator in the .CPP file.
It should be:

ATCFramework::Drawing::Point ATCFramework::Drawing::operator + ( const Point & p1, const Point & p2 ) {
Point result = p1;
return ( result += p2 );
}

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?

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.

So it's only going to be useful if I catch the exception? Hmmm, that kinda sucks... 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...

Edited by ATC, 22 October 2012 - 08:33 PM.

_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

___________________________________________________________________________________

### #11L. Spiro  Members

Posted 22 October 2012 - 09:06 PM

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?

To the left of the Quote icon is an icon that looks like “<>”. Use this.

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?

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:
bool MyClass::Point::operator == ( etc. ) {
}

L. Spiro

Edited by L. Spiro, 22 October 2012 - 09:07 PM.

### #12ATC  Members

Posted 22 October 2012 - 10:23 PM

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:

Not sure if this changes anything but Point is not nested in another class... "Drawing" is actually a namespace nested in "ATCFramework".
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

___________________________________________________________________________________

### #13L. Spiro  Members

Posted 22 October 2012 - 11:14 PM

Then you would have to do:
namespace ATCFramework {
namespace Drawing {

Point Point::operator + ( etc. ) {
}
}
}

If you want to avoid fully qualifying things.

L. Spiro

### #14Bregma  Members

Posted 22 October 2012 - 11:57 PM

So it's only going to be useful if I catch the exception? Hmmm, that kinda sucks...

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?

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...

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.
Stephen M. Webb
Professional Free Software Developer

### #15ATC  Members

Posted 23 October 2012 - 09:59 AM

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?

That's not what I'm talking about... I do expect an error message to pop up when I'm debugging in my IDE.

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.

Gotcha, thanks!
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine