Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualATC

Posted 21 October 2012 - 12:49 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?? Posted Image

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

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? Posted Image

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

#1ATC

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?? Posted Image

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

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? Posted Image

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

PARTNERS