CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2009
    Posts
    20

    error: expected constructor, destructor, or type conversion before '&' token

    I am getting the above error when ever i try to write somethin in my cpp file.. if I have the same code inlined in my header file it compiles and works fine.. I am using the g++ compiler for compilation.. Is there any thin particular i need to look for to fix that error

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: error: expected constructor, destructor, or type conversion before '&' token

    It would be helpful if we could see the code.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: error: expected constructor, destructor, or type conversion before '&' token

    Quote Originally Posted by sundar0206 View Post
    I am getting the above error when ever i try to write somethin in my cpp file.. if I have the same code inlined in my header file it compiles and works fine.. I am using the g++ compiler for compilation.. Is there any thin particular i need to look for to fix that error
    Ah, that´s pretty easy to fix
    - Guido

  4. #4
    Join Date
    Mar 2009
    Posts
    20

    Re: error: expected constructor, destructor, or type conversion before '&' token

    Ya its a huge file dunno if you guys would need to see the whole file.... But ya one part of the code is this


    Code:
    	Quat& operator *= (const Quat& q){
    				setValue(
    				m_w*q.get4d_X() +	m_x*q.get4d_W() + m_y*q.get4d_Z() - m_z*q.get4d_Y(),
    				m_w*q.get4d_Y() - m_x*q.get4d_Z() + m_y*q.get4d_W() + m_z*q.get4d_X(),
    				m_w*q.get4d_Z() + m_x*q.get4d_Y() - m_y*q.get4d_X() + m_z*q.get4d_W(),
    				m_w*q.get4d_W()	- m_x*q.get4d_X() - m_y*q.get4d_Y() - m_z*q.get4d_Z()
    			);
    			return *this;
    		}
    this above code works fine when i have it in the header file .. But when i do this in the cpp file
    Code:
    //header file
    	Quat& operator *= (const Quat& q);
    		
    //cppfile
    	Quat& Quat::operator *= (const Quat& q){
    				setValue(
    				m_w*q.get4d_X() +	m_x*q.get4d_W() + m_y*q.get4d_Z() - m_z*q.get4d_Y(),
    				m_w*q.get4d_Y() - m_x*q.get4d_Z() + m_y*q.get4d_W() + m_z*q.get4d_X(),
    				m_w*q.get4d_Z() + m_x*q.get4d_Y() - m_y*q.get4d_X() + m_z*q.get4d_W(),
    				m_w*q.get4d_W()	- m_x*q.get4d_X() - m_y*q.get4d_Y() - m_z*q.get4d_Z()
    			);
    			return *this;
    		}
    it throws me the error.. I can show u the file if u need it.. but I think this is a very obvious thing which I seem to miss .. I am not sure!!!

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: error: expected constructor, destructor, or type conversion before '&' token

    Quat is actually a class template, is it not?
    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
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: error: expected constructor, destructor, or type conversion before '&' token

    Please post the minimal code that reproduces the error.

    I have no problems compiling these two files:

    Header file:
    Code:
    #ifndef QuatH
    #define QuatH
    
    class Quat
    {
    public:
       Quat();
       Quat& operator*=( const Quat& lhs );
    };
    #endif
    Implementation file:
    Code:
    #include "quat.h"
    
    Quat::Quat()
    {
    }
    
    Quat& Quat::operator*=( const Quat& lhs )
    {
       return *this;
    }
    Remove snippets of your code until it compiles and then reinsert the cut parts and try to locate the error.
    - Guido

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: error: expected constructor, destructor, or type conversion before '&' token

    Quote Originally Posted by sundar0206 View Post
    Ya its a huge file dunno if you guys would need to see the whole file....
    If the compiler needs the entire file to generate the error, then we need the same thing the compiler is seeing.
    But ya one part of the code is this
    Just posting a piece of a file isn't going to work, since we have no idea what Qant is, whether it was defined, whether that code is in a function block, nothing.

    Compiler errors are to be diagnosed by providing the smallest, but complete example that duplicates the error. This means we can copy the code right out of the message, and without editing the code, compile it to see the error.

    The reason for this is simple -- by not providing this code, we have to either

    1) come up with our own examples to show you that our example works (look at GNiewerth's post), or

    2) we try to come up with an example that has the exact same error you have by adding or removing code. This wastes time on the people attempting to help.

    The easiest thing to do is for you to diagnose this yourself by coming up with a small complete example. Not only will the example be complete, maybe by creating a smaller example, you'll find the error yourself.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Mar 2009
    Posts
    20

    Re: error: expected constructor, destructor, or type conversion before '&' token

    Thanks guys...

    Will have a look at creating a smaller version of the code.... and try posting it in a bit if I still have the problem

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured