C++ constructor not working?

Discussion in 'OT Technology' started by A Cow, Oct 22, 2006.

  1. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,039
    Likes Received:
    97
    Location:
    Vancouver BC
    ok.. for some reason i keep getting the error

    Error 1 error C2653: 'grid' : is not a class or namespace name c:\documents and settings\administrator\desktop\skipingstone\skipingstone\grid.cpp 14


    here is my code (well.. grid.h and grid.cpp)

    GRID.H
    Code:
    #include "cell.h"
    #include "stdafx.h"
    
    class grid
    {
    public:
    	grid();
    	void makegrid();
    
    
    private:
    	cell gridlayout[16][16];
    
    
    };
    GRID.CPP
    Code:
    #include "grid.h"
    #include "stdafx.h"
    
    #include <cstdlib> 
    #include <ctime> 
    #include <iostream>
    
    using namespace std;
    
    
    //==================================================================================================================
    // constructor
    //==================================================================================================================
    grid::grid()
    {
    	srand((unsigned)time(0)); 
    }
    //==================================================================================================================
    //==================================================================================================================
    
    //==================================================================================================================
    //==================================================================================================================
    grid::makegrid()
    {
    	//fill up every other thing with something
    
    	for(int i = 1; i<16; i++)
    	{
    		for(int j = 1; j<16; j+2)
    		{
    			gridlayout[i][j] = rand() % 2;
    			gridlayout[i][j+1] = gridlayout[i][j];
    		}
    	}
    
    	for(int i = 1; i<16; i++)
    	{
    		for(int j = 1; i<16; j++)
    		{
    			cout<<gridlayout[i][j] << " ";
    		}
    		cout<<endl;
    	}
    
    }
    
    //==================================================================================================================
    //==================================================================================================================
    does anyone see why? probably something stupid..

    thanks in advanced
     
  2. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    I'm not 100% on this but have you might try commenting out your line:

    using namespace std;

    in your .cpp file.
     
  3. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    you need to add a return type for your makegrid method implementation in grid.cpp
     
  4. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,039
    Likes Received:
    97
    Location:
    Vancouver BC
    makegrid is void

    and using namespace std; is for cin and cout
     
  5. GunboatDiplomat

    GunboatDiplomat New Member

    Joined:
    Jun 9, 2006
    Messages:
    214
    Likes Received:
    0
    I don't thnk including the std namespace is your problem although I would suggest against it, myself.

    As far as I can tell, it's correct C++ syntax. Are you sure you're including the right "grid.h" ? Type some garbage in the .h file and see if your compiler chokes on it...
     
  6. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    If you file containing main() has a line using namespace std, the you should prolly delete that. I'm not sure you need them both but I'm also not sure that's your problem.
     
  7. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    oh and makegrid needs a return type in your grid.cpp file. It should read:
    Code:
    void grid::makegrid()
    
     
  8. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    Yeah I'm not sure either but when I googled the error code it mentioned namespace redefinition. Just looked in my C++ text and it has the namespace in the class.cpp file but there is not one in the .cpp file containing main() so it could be an issue. However, I don't really understand namespaces.
    Yeah I would add that perhaps you should use the standard start to a .h file as follows:
    Code:
    #ifndef GRID_H
    #define GRID_H
    
    //grid class code
    
    #endif
    
     
  9. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,039
    Likes Received:
    97
    Location:
    Vancouver BC
    stupid c++

    changed
    #include "grid.h"
    #include "stdafx.h"

    #include <cstdlib>
    #include <ctime>
    #include <iostream>

    using namespace std;

    to

    #include "stdafx.h"
    #include "grid.h"
    #include <cstdlib>
    #include <ctime>
    #include <iostream>

    using namespace std;


    and it worked.. gay lol
     
  10. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    WTF?? are you serious? Why the hell would the order matter?
     
  11. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,039
    Likes Received:
    97
    Location:
    Vancouver BC
    lol.. dont ask me.. good ol 2005 :)
     
  12. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,039
    Likes Received:
    97
    Location:
    Vancouver BC
    o one other random thing..

    how do i access the gridlayout array.. :/ not working

    added
    grid mygrid

    then i acces it via mygrid.gridlayout[j] but it tells me it cant covert it..
     
  13. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
  14. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,039
    Likes Received:
    97
    Location:
    Vancouver BC
    duh.. lol kthx :)
     
  15. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,039
    Likes Received:
    97
    Location:
    Vancouver BC
    wait.. pointers dont seem to be working..?
     
  16. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    try

    this.gridlayout[j];
     
  17. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,039
    Likes Received:
    97
    Location:
    Vancouver BC
    ok lol got that working.. its weird.. lol
    kk.. now wtf..
    for(int i = 0; i < 16; i++)
    {
    for (int j = 0; i < 16; j++)
    {
    cout << i << " " << j;
    cout << endl;
    }
    }

    that doenst work.. i says 0.. j goes up till it wraps around.. and then keeps going
     
  18. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    for (int j = 0; i < 16; j++)

    should be j < 16
     
  19. GunboatDiplomat

    GunboatDiplomat New Member

    Joined:
    Jun 9, 2006
    Messages:
    214
    Likes Received:
    0
    I'm going to make two guesses...

    First, you only need stdafx.h in your .cpp files and never in your headers.
    Secondly, stdafx.h must be the first thing in all your .cpp files.


    Are you using MFC? If not then you really shouldn't need stdafx...
     
  20. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,039
    Likes Received:
    97
    Location:
    Vancouver BC
    thx godzilla.. lol.. too mcuh coding for one day

    and stdafx.h has to be in every file.. at least thats what the debuggers says.. and no its just a win32 project with precompiled headers
     

Share This Page