Weird errors.

This is a discussion on Weird errors. within the C++ Programming forums, part of the General Programming Boards category; Hello ! I'm getting many errors I have no idea how to correct them. The code seems fine to me ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    Weird errors.

    Hello !

    I'm getting many errors I have no idea how to correct them. The code seems fine to me but it does look like it isn't.

    Code:
    #ifndef MATRIX_H_INCLUDED
    #define MATRIX_H_INCLUDED
    
    #include <list>
    
    template < class T >
    class Matrix
    {
    public:
    	bool SquareMatrix( );
    	static MATRIX_PTR Minor(const Matrix< T >&);
    private:
    	typedef std::auto_ptr< Matrix< T > > MATRIX_PTR;
    	unsigned int Rows, Cols;
    	std::list< std::list< T > > Data;
    	MATRIX_PTR Temp;
    };
    
    template < class T >
    bool Matrix< T >::SquareMatrix( )
    {
    	return (Rows == Cols);
    }
    
    template < class T >
    MATRIX_PTR Matrix< T >::Minor(const Matrix< T >& m)
    {
    	
    }
    
    #endif // MATRIX_H_INCLUDED
    error C2236: '<Unknown>' '<Unknown>' inattendu
    error C2143: syntax error : missing '>' before'{'
    error C2079: 'Matrix' uses class 'Matrix' undefined
    error C2988: cannot recognize template declaration / definition
    error C2059: syntax error : '<end Parse>'
    error C2143: syntax error : missing ';' before'<'
    error C2988: cannot recognize template declaration / definition
    error C2059: syntax error : '<'
    error C2039: 'Minor' : is not a member of 'operator``global namespace'''
    error C2039: 'SquareMatrix' : is not a member of 'operator``global namespace'''
    error C2447: '{' : function header missing (old format formal list ?)
    syntax error : missing ';' before'{'
    Any info would be more than welcome. I have tried moving around the typedef, using a #define instead, not defining a new type at all.. etc.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You should place the typedef before the code that uses it (Minor returns a MATRIX_PTR). I'm not sure if that will solve the problem, but if you haven't tried it already I'd do that. Just add a private block above the public one.

    Of course, can you return a private typedef from a public function? You might have to make the typedef public.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    I put that typedef up there in the public section, before the two functions and I get the same errors. I also tried adding an empty constructor, just in case, but it didn't change anything either.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You should #include <memory> to use auto_ptr.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Ahah, thanks. I still get the same errors though.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    There is an issue I see though. I have an auto_ptr to a Matrix, however it is impossible to have a class object of type Foo inside class Foo itself. I don't know how to get around of this..

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    A pointer to an object is not the same as an object directly.

    How do the errors change if you replace the auto_ptr with a raw pointer, though? (Just out of interest.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    It doesn't change anything. I have tried right after posting. I knew that trick already but I think an auto_ptr<> is much more suited to my needs.

    Basically what I want to do is have a temp object for when I don't want to modify a matrix but rather return a matrix derived from it. For example return a minor, its transposed matrix, upper triangle, lower triangle.. I also removed the static keyword which I don't even know why I put it there in the first place but still no changes... I can't even see what's wrong.. =/ Especially when I check what MSDN has to say about the first error:

    http://msdn2.microsoft.com/en-us/library/wx7x0ezw.aspx

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    88
    It's the typedef that's the source of your pain. This compiles with g++:
    Code:
    #ifndef MATRIX_H_INCLUDED
    #define MATRIX_H_INCLUDED
    
    #include <list>
    #include <memory>
    
    template <class T>
    class Matrix
    {
    public:
    	bool SquareMatrix( );
    	static std::auto_ptr< Matrix<T> > Minor(const Matrix< T >&);
    private:
    	unsigned int Rows, Cols;
    	std::list< std::list< T > > Data;
    	std::auto_ptr< Matrix<T> > Temp;
    };
    
    template <class T>
    bool Matrix< T >::SquareMatrix( )
    {
    	return (Rows == Cols);
    }
    
    template <class T>
    std::auto_ptr< Matrix<T> > Matrix<T>::Minor(const Matrix<T>& m)
    {
    	
    }
    AFAIK, templates and typedefs don't play well together. You may just have to bite the bullet there...

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    AFAIK, templates and typedefs don't play well together. You may just have to bite the bullet there...
    Maybe not. I'm guessing that a typename might work some magic here:

    Code:
    #ifndef MATRIX_H_INCLUDED
    #define MATRIX_H_INCLUDED
    
    #include <list>
    #include <memory>
    
    template < class T >
    class Matrix
    {
    public:
        typedef std::auto_ptr< Matrix< T > > MATRIX_PTR;
        bool SquareMatrix( );
        static MATRIX_PTR Minor(const Matrix< T >&);
    private:
        unsigned int Rows, Cols;
        std::list< std::list< T > > Data;
        MATRIX_PTR Temp;
    };
    
    template < class T >
    bool Matrix< T >::SquareMatrix( )
    {
        return (Rows == Cols);
    }
    
    template < class T >
    typename Matrix<T>::MATRIX_PTR Matrix< T >::Minor(const Matrix< T >& m)
    {
    
    }
    
    #endif // MATRIX_H_INCLUDED
    The typedef should be public since it is used in the public interface.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    903
    I already tried without typedef'ing and got the same errors. I also tried the typename trick and got the very same errors. I left the typedef public but it didn't change anything either. I tried cleaning and rebuilding the solution as well but the result is the same.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    903
    I have tried creating a completely new empty command line project and got the very same errors with the following code:
    Code:
    #ifndef MATRIX_H_INCLUDED
    #define MATRIX_H_INCLUDED
    
    #include <list>
    #include <memory>
    
    template < class T >
    class Matrix
    {
    public:
    	Matrix( ) { }
    
    	bool SquareMatrix( );
    	std::auto_ptr< Matrix< T > >& Minor(const Matrix< T >&);
    private:
    	unsigned int Rows, Cols;
    	std::list< std::list< T > > Data;
    	std::auto_ptr< Matrix< T > > Temp;
    };
    
    template < class T >
    bool Matrix< T >::SquareMatrix( )
    {
    	return (Rows == Cols);
    }
    
    template < class T >
    std::auto_ptr< Matrix< T > >& Matrix< T >::Minor(const Matrix< T >& m)
    {
    	
    }
    
    #endif // MATRIX_H_INCLUDED
    I'm using MSVC++ 2003 under Windows XP Pro SP2, by the way.

    Edit: I posted the question on another forum. If they have an answer, I'll let you guys know.
    Last edited by Desolation; 05-08-2007 at 05:13 PM.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Works for me on VC++ 2003. Can you post your entire piece of code?
    Code:
    #include <iostream>
    #include <list>
    #include <memory>
    
    static bool made_one = false;
    
    template < class T >
    class Matrix
    {
    public:
        Matrix( ) : Rows(2), Cols(2), Temp(0)
        {
            if (!made_one)
            {
                made_one = true;
                Temp.reset(new Matrix<T>());
            }
        }
    
        bool SquareMatrix( ) const;
        std::auto_ptr< Matrix< T > >& Minor(const Matrix< T >&);
    private:
        unsigned int Rows, Cols;
        std::list< std::list< T > > Data;
        std::auto_ptr< Matrix< T > > Temp;
    };
    
    template < class T >
    bool Matrix< T >::SquareMatrix( ) const
    {
        return (Rows == Cols);
    }
    
    template < class T >
    std::auto_ptr< Matrix< T > >& Matrix< T >::Minor(const Matrix< T >& m)
    {
        if (m.Temp.get())
            return Temp;
        else if (Temp.get())
            return Temp->Temp;
        return Temp;
    }
    
    int main()
    {
        Matrix<int> m1;
        Matrix<int> m2;
        if (m1.SquareMatrix() && m2.SquareMatrix())
        {
            std::auto_ptr< Matrix< int > >& ap = m1.Minor(m2);
            if (ap.get() == 0)
                std::cout << "Success!\n";
            else
                std::cout << "Failure!\n";
        }
    }
    With a typedef it works too:
    Code:
    #include <iostream>
    #include <list>
    #include <memory>
    
    static bool made_one = false;
    
    template < class T >
    class Matrix
    {
    public:
        typedef std::auto_ptr< Matrix< T > > MATRIX_PTR;
        Matrix( ) : Rows(2), Cols(2), Temp(0)
        {
            if (!made_one)
            {
                made_one = true;
                Temp.reset(new Matrix<T>());
            }
        }
    
        bool SquareMatrix( ) const;
        MATRIX_PTR& Minor(const Matrix< T >&);
    private:
        unsigned int Rows, Cols;
        std::list< std::list< T > > Data;
        MATRIX_PTR Temp;
    };
    
    template < class T >
    bool Matrix< T >::SquareMatrix( ) const
    {
        return (Rows == Cols);
    }
    
    template < class T >
    typename Matrix< T >::MATRIX_PTR& Matrix< T >::Minor(const Matrix< T >& m)
    {
        if (m.Temp.get())
            return Temp;
        else if (Temp.get())
            return Temp->Temp;
        return Temp;
    }
    
    int main()
    {
        Matrix<int> m1;
        Matrix<int> m2;
        if (m1.SquareMatrix() && m2.SquareMatrix())
        {
            Matrix<int>::MATRIX_PTR& ap = m1.Minor(m2);
            if (ap.get() == 0)
                std::cout << "Success!\n";
            else
                std::cout << "Failure!\n";
        }
    }
    Last edited by Daved; 05-08-2007 at 05:41 PM.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    903
    Your code works for me too. What did you change ?

    Thanks a lot, by the way. =)

    PS: I didn't have anything else. I just had a blank .cpp file which only included matrix.h so that it would compile the code.

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    903
    I copied my old code into a new project and added an empty main() function and it compiled. Does anyone know why it does so ?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with DX9 Errors!!!
    By Tommaso in forum Game Programming
    Replies: 7
    Last Post: 06-28-2006, 02:51 PM
  2. Errors with header files in OpenGL using VisualC++
    By wile_spice in forum Game Programming
    Replies: 3
    Last Post: 06-22-2006, 08:56 AM
  3. Weird Errors in VS 2003
    By Devil Panther in forum Windows Programming
    Replies: 1
    Last Post: 10-01-2005, 06:16 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21