Optional<T> class template

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Optional<T> class template

      Hiya. New to forums, so excellent to have a community of budding game coders, thanks Mike for an excellent book! :)

      Okay- onto the question:
      I downloaded some source code included in the book, and since i'm in the process of learning C++'s more advanced of features and things, I made a "sandbox" project, where I just write code, compile, and see how different modules work. One of the things I got around to trying was the optional<T> template!

      So, for one of my little mini-code things, I got to write a method that returns a optional<int>, because it could give a valid value, or an invalid value. I didn't want to use exceptions for that due to overheads.

      Upon using that, optional.h is parsed, but somehow it throws a trillion errors. it says, "error: m_bValid undeclared (first use of function)". However it is quite clear to me that m_bValid is a member of the optional_base, and optional is derived from optional_base.
      It also moans about m_Data in the same way. They are the only errors.

      I can't understand what the compiler's complaining about. Does anybody else have problems with optional.h?

      Yes, I #included "optional.h" (with the correct path) into the source file that was using optional<T>.

      I can assure you all my other code compiles fine, and all required libraries are linked correctly etc.



      And also on a general note, did I read somewhere you HAVE to put template classes in the same file as where they're gonna be used?... In that case what's the good of the header file with templates in them? :/


      Your help is appreciated! Thanks!

      OS: Windows XP SP2
      Compiler: MingW g++
      IDE: Eclipse with CDT (lol probably the only one using Eclipse)
      A Toyota's a toyata.
      A dog, a panic in a pagoda!
      God! A red nugget! A fat egg under a dog!
      Go hang a salami, I'm a lasagna hog!
    • Haha - yes I forgot to mention, I did extract the code for the Optional stuff from "templates.h" into my own "optional.h", which I put into the folder "stuff",
      so i #include "stuff/optional.h". I'm sure it's not the inclusion that's the problem.

      And MukRaker, templates are the best XD

      Any ideas?
      A Toyota's a toyata.
      A dog, a panic in a pagoda!
      God! A red nugget! A fat egg under a dog!
      Go hang a salami, I'm a lasagna hog!
    • Man I don't have any idea - something you can do though is try to find the problem through the divide and conquer method - rip out enough code/headers until it compiles, then slowly add stuff back in.

      Try defining a really simple version of optional first, something really bare bones. At some point the problem will manifest itself with the inclusion of a single line of code, and you solution will become a lot easier to find.
      Mr.Mike
      Author, Programmer, Brewer, Patriot