Reputation: 2866
I am starting to learn C++ and I keep running into this problem where people will give example code, but when I try to complile it, I get errors because they used some definition, declaration, function, or variable which was defined in some library that they conveniently forgot to include. While I have had success finding some of these using google to find what header to include to use that function, its getting a little tedious.
I am using visual studio express for Windows Desktop right now.
Here is an example:
The following link gives an example of reading and writing to a serial port.
The example use CString to declare the variable PortSpecifier.
No where in the document do they mention what libraries they are using (The mention they use the windows API).
VS does not recognize Cstring. Searching for CString shows there is a string.h or cstring library. However these do not have the identifier in them.
MSDN has a wonderful page describing what CString is and has a cryptic #include "afx.h" at the bottom of the page, but including this header does not give the compiler the description of Cstring still. So I am left wondering the internet for hours trying to find a way to get VS to accept CString (or find a substitute).
1)Is there a function that finds which library or header has the definition for the given offending "word"?
2)Is there some data base I can type that word into to find out what I need to include to have VS recognize it?
3)Is it SOP to leave the reader to guess what library he needs to run your script?
Upvotes: 0
Views: 386
Reputation: 8171
Unfortunately, no, there is not magic function or master database which figure out which library a random class/type/whatever belongs to. Your best bet is to Google and pay attention to the details as you investigate.
Yes, ideally code samples should describe any dependencies they have. But unfortunately not everyone who posts code on the internet is completely professional or thorough about it. In fact, they may be nearly as new to things as you are.
For your particular example, the first google result I get for "CString" is Using CString. And on that page, if you look at the navigation on the left, you find it's under the "MFC and ATL" section. A little research on those should explain that they're libraries from Microsoft which are provided with Visual Studio; although I don't believe the Express editions have them.
Another thing that would hopefully become apparent from looking at the documentation and examples of CString is that it's a string container class. So essentially anything it can do can be done with std::string. You might have to refactor a little though.
Upvotes: 2