C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-07-2007, 08:13 PM   #1
Registered User
 
Join Date: May 2006
Posts: 894
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
Quote:
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.
Desolation is offline   Reply With Quote
Old 05-07-2007, 11:08 PM   #2
Registered User
 
Join Date: Jan 2005
Posts: 7,137
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.
Daved is offline   Reply With Quote
Old 05-08-2007, 07:06 AM   #3
Registered User
 
Join Date: May 2006
Posts: 894
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.
Desolation is offline   Reply With Quote
Old 05-08-2007, 07:23 AM   #4
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
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
CornedBee is offline   Reply With Quote
Old 05-08-2007, 07:28 AM   #5
Registered User
 
Join Date: May 2006
Posts: 894
Ahah, thanks. I still get the same errors though.
Desolation is offline   Reply With Quote
Old 05-08-2007, 07:32 AM   #6
Registered User
 
Join Date: May 2006
Posts: 894
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..
Desolation is offline   Reply With Quote
Old 05-08-2007, 07:38 AM   #7
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
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
CornedBee is offline   Reply With Quote
Old 05-08-2007, 08:25 AM   #8
Registered User
 
Join Date: May 2006
Posts: 894
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
Desolation is offline   Reply With Quote
Old 05-08-2007, 10:56 AM   #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...
UMR_Student is offline   Reply With Quote
Old 05-08-2007, 11:07 AM   #10
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 05-08-2007, 04:53 PM   #11
Registered User
 
Join Date: May 2006
Posts: 894
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.
Desolation is offline   Reply With Quote
Old 05-08-2007, 05:02 PM   #12
Registered User
 
Join Date: May 2006
Posts: 894
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.
Desolation is offline   Reply With Quote
Old 05-08-2007, 05:37 PM   #13
Registered User
 
Join Date: Jan 2005
Posts: 7,137
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.
Daved is offline   Reply With Quote
Old 05-08-2007, 06:27 PM   #14
Registered User
 
Join Date: May 2006
Posts: 894
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.
Desolation is offline   Reply With Quote
Old 05-08-2007, 06:29 PM   #15
Registered User
 
Join Date: May 2006
Posts: 894
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 ?
Desolation is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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