Thread: including templatized vector and vector

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    including templatized vector and vector

    why is it that I got this error, when I compile the code by using the -Dproject flag?:

    Code:
    /vehicle.h:21: error: âprojectâ has not been declared
    /vehicle.h:21: error: expected `{' before âvectorâ
    /vehicle.h:21: error: expected initializer before â<â token
    /vehicle.h:33: error: âprojectâ has not been declared
    /vehicle.h:33: error: expected `{' before âvectorâ
    am I doing anything wrong? the code is attached below...
    The weird thing is that if I take out the #include <vector> in the vehicle.h file and then compile the file using -Dproject it works, however if I include it with athe #include <vector> it doesn't work... I just don't know why
    Last edited by -EquinoX-; 11-21-2009 at 11:47 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I recommend running it through the preprocessor only (-E with GCC), and you should find something like this:
    Code:
    namespace 1{
    
    template <class type, int n> class vector;
    and

    Code:
    class State : public 1::vector<double, 4> { };
    Use a convention for macro names that doesn't let them get mixed up with other identifiers (e.g macro names and only macro names are ALL CAPS).

    -----------

    Another thing:

    Code:
    template <class type, int n>
    bool checkSize(const vector<type, n>& v1, const vector<type, n>& v2)
    {
       try {
          if( v1.size != v2.size )
             throw "Operations of two vectors of not the same size!";
    	return true;
       }
       catch( char * str ) {
          std::cout << "Exception raised: " << str << std::endl;
       }
    }
    1) Don't use throw for flow control inside a function. Exceptions are thrown to indicate to the caller that something is wrong, otherwise you can just use normal flow control constructs:

    Code:
    template <class type, int n>
    bool checkSize(const vector<type, n>& v1, const vector<type, n>& v2)
    {
    
       if( v1.size == v2.size )
           return true;
       std::cout << "Exception raised: Operations of two vectors of not the same size!" << std::endl;
       //note the lack of return statement!?: did you mean: return false;?
    }
    2) Printing an error message and continuing as if nothing happened (in this case returning an indeterminate value) is not a particularly good error handling strategy. The caller will not know what happened in checkSize.

    3) If you need to throw exceptions, prefer throwing exception objects (preferable one defined in <stdexcept> or derived from those) over built-in types.
    Last edited by anon; 11-22-2009 at 05:13 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed