CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24
  1. #16
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: problems with my code, not sure why

    Quote Originally Posted by Krishnaa
    Why dont you attach actual code files ?
    Didn't you see? It's loooong...
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  2. #17
    Join Date
    May 2002
    Posts
    1,435

    Re: problems with my code, not sure why

    If the code is too long to post then my suggestion would be to compile it and post all of the error messages. You originally only posted what you called errors 2-4. What was error 1? You may have decided that it wasn't relevant but you should not do that. Most of the experts on this board understand error messages and can determine the most likely causes.

    In addition to posting all of the error messages you should post the code where the first error occurs. We can start with error #1 and go from there.
    Last edited by 0xC0000005; March 28th, 2007 at 09:40 AM.

  3. #18
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645

    Re: problems with my code, not sure why

    Zip the whole project and attach the zip file.

  4. #19
    Join Date
    Feb 2002
    Posts
    4,640

    Re: problems with my code, not sure why

    Quote Originally Posted by krmed
    I'm not 100% sure about this because I've never seen it, but your player class does not have a constructor! (This could be your problem.)

    Normally you'll have in the header:
    Code:
    player class.h:
    
    class player{
    public:
    player();
    ~player(); 
    float size; //the size of the player
    float x;
    float y;
    float z;
    int score;
    int lives;
    float time;
    };
    Along with that, you would have an implementation (or it could be included in the header if needed.
    Code:
    class player{
    public:
    player(){ };
    ~player(){ }; 
    ...
    I suspect that the error really occurred on the line preceding the one you point to, which would indicate it could not create an object of the player class.

    Hope that helps.
    The compiler will automatically generate a constructor, copy constructor and assignment operator if you do not provide one. However, if you do provide a constructor that takes any number of parameters, you must also provide a default ctor (the compiler will not auto generate one for you).

    Viggy

  5. #20
    Join Date
    Mar 2007
    Posts
    9

    Re: problems with my code, not sure why

    i tried to upload the zipped folder but it was 4mb after being zipped, and too big to upload. in the compilation, there are alot of warnings about various things, such as truncating a double to a float etc. and the first 'error' is a warning.

    this warning is:
    _______________________________________
    Warning 1 warning C4305: 'initializing' : truncation from 'double' to 'float' y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 28
    _______________________________________
    and all the errors are:
    _______________________________________
    Error 2 error C2143: syntax error : missing ';' before '.' y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 37
    ____
    Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 37
    ____
    Error 4 error C2371: 'me' : redefinition; different basic types y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 37
    ____
    Error 5 error C2143: syntax error : missing ';' before '.' y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 38
    ____
    Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 38
    ____
    Error 7 error C2371: 'me' : redefinition; different basic types y:\0501334\my documents\2nd year\graphics programming\new folder\myproj\main.cpp 38
    _____________________________________________

    after that there are just warnings about my shady coding

  6. #21
    Join Date
    May 2002
    Posts
    1,435

    Re: problems with my code, not sure why

    This is VERY IMPORTANT: As Hobson and VladimirF have already suggested, you must locate the other variable named 'me' that is conflicting with the one of type 'player' that you have shown in your post.

    I can generate the same errors you show with this code:
    Code:
    int me;
    player me;
    me.size=2;
    me.y=5;
    You can delete the Debug and/or Release folders in your project (the ones that contain object and executable files) before zipping it. I really doubt that you have 4MB of compressed source code.

    And another thought: is the code that you show defined outside of a function?
    Code:
    #include "player.h"
    
    player me;
    me.size=2; //line 37 here
    me.y=5; //line 38
    
    void SomeFunction()
    {
    }
    If so, that could account for your at least part of the problem.
    Last edited by 0xC0000005; March 29th, 2007 at 04:37 AM.

  7. #22
    Join Date
    Mar 2007
    Posts
    9

    Re: problems with my code, not sure why

    i already tried changing 'me' to other variable names, but the same error occured. i also searched the code for any text " me" (with the space) and found no other variable called 'me'.
    i have just taken the actault code, instead of thewhole project (thats why it was so big), and zipped it, and attached it to this post.
    Attached Files Attached Files

  8. #23
    Join Date
    May 2002
    Posts
    1,435

    Re: problems with my code, not sure why

    I was adding to my post the part about making sure that the code causing the errors was not outside of a function and now I see that my suspicion was correct.

    It is OK to declare 'player me' outside of a function but initialization of its parameters must be done inside of a function. try moving the two lines that set size and y inside of WinMain().

    Better yet, start learning the concept of working with classes by adding an appropriate constructor that takes arguments for size and y:
    Code:
    class player{
    public:
    float size; //the size of the player
    float x;
    float y;
    float z;
    int score;
    int lives;
    float time;
    
    player(float size, float y)
      : size(size), y(y) 
    {
    }
    };
    
    
    // Now you can declare your player like this:
    player me(2,5);
    My example of adding a constructor is very crude. You should really set all variables to an initialized state even if they aren't included in the parameter list:
    Code:
    player(float size, float y)
      : size(size), y(y), x(0), score(0)... 
    {
    }
    And it would be a good idea to declare a default constructor in case a player is created without parameters:
    Code:
    player(float size, float y)
      : size(size), y(y), x(0), score(0)... 
    {
    }
    player()
      : size(4), x(1), y(1)...
    {
    }
    
    player me(2,5);   // size=2, y=5
    player you;         // size=4, y=1
    Last edited by 0xC0000005; March 29th, 2007 at 04:57 AM.

  9. #24
    Join Date
    Mar 2007
    Posts
    9

    Re: problems with my code, not sure why

    thanks very much! it compiles and runs now! (unfortunatly i still have alot of work to do on it ) didnt realise that you wre unable to initialise class parameters outside of functions, cheers to everyone who gave this a look!

Page 2 of 2 FirstFirst 12

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