• Advertisement
Sign in to follow this  

More C++ stuff...

This topic is 1917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?? blink.png

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?! huh.png

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? ph34r.png

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

Share this post


Link to post
Share on other sites
Advertisement

[quote name='ATC' timestamp='1350845223' post='4992515']
Vector3 pos = { x, y, z }


Why not


Vector3 pos(x,y,z);

?
[/quote]

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

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

Share this post


Link to post
Share on other sites

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.

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?

Share this post


Link to post
Share on other sites

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?? blink.png

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.
[/quote]
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? ph34r.png
[/quote]

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?
[/quote]

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.

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

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.


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

Share this post


Link to post
Share on other sites

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?? blink.png

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


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

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

If you want to avoid fully qualifying things.



L. Spiro

Share this post


Link to post
Share on other sites

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...
[/quote]
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.

Share this post


Link to post
Share on other sites

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


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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement