#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    29
    Rep Power
    0

    Where to Put a Struct?


    This is probably the easiest question you've ever seen, but I haven't used C++ in a while so I'm a little rusty.

    Anyway, where do I put a struct declaration? I have a struct I want to declare that looks like this:

    Code:
    struct UserData {
    	unsigned int userid;
    	unsigned int ip;
    	unsigned char month;
    	unsigned char day;
    	unsigned char year;
    	unsigned char hour;
    	unsigned char min;
    	unsigned char sec;
    	unsigned char numpages;
    	unsigned short entry;
    	unsigned short exit;
    	unsigned long bw;
    	unsigned short sbw;
    	unsigned short pgdloc;
    	unsigned char country;
    	unsigned char browser;
    	unsigned char os;
    	std::vector<unsigned short> pages;
    }
    I tried to put it into one of my files, like so:

    Code:
    // ImportLog.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "Log Analyzer.h"
    #include "Log AnalyzerDlg.h"
    #include "ImportLog.h"
    #include <vector>
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    using std::vector;
    
    struct UserData {
    	unsigned int userid;
    	unsigned int ip;
    	unsigned char month;
    	unsigned char day;
    	unsigned char year;
    	unsigned char hour;
    	unsigned char min;
    	unsigned char sec;
    	unsigned char numpages;
    	unsigned short entry;
    	unsigned short exit;
    	unsigned long bw;
    	unsigned short sbw;
    	unsigned short pgdloc;
    	unsigned char country;
    	unsigned char browser;
    	unsigned char os;
    	std::vector<unsigned short> pages;
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // CImportLog dialog
    
    
    CImportLog::CImportLog(CWnd* pParent /*=NULL*/)
    	: CDialog(CImportLog::IDD, pParent)
    {
    	//{{AFX_DATA_INIT(CImportLog)
    	m_Directions = _T("");
    	m_LogLocation = _T("");
    	//}}AFX_DATA_INIT
    }
    
    // continues . . .
    But I got the error "'CImportLog::CImportLog' : constructors not allowed a return type". So, where am I supposed to put the struct? Also, if I declare it in one class, will I be able to use it in my other class (.cpp) files?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    lol...what the heck does that struct have to do with the CImportLog constructor you posted? Or, are you far enough removed from C++ to think a constructor is a struct.

    "where am I supposed to put the struct?"

    In a header file, and include it in any .cpp file that references the struct:
    Code:
    my_struct.h
    --------------
    struct UserData
    {
           //members
    }
    
    functions.h
    -------------
    void my_function(int a);
    void another_function(double b);
    
    
    
    my_function.cpp
    -------------------
    #include "my_struct.h"
    #include "functions.h"
    
    void my_function(int a)
    {
         struct UserData A;
         //other code
    }
    
    
    another_function.cpp
    -------------------------
    #include "functions.h"
    #include <iostream>
    using std::cout;
    using std::endl;
    
    void another_function(double b)
    { 
          cout<<b<<endl;
    }
    
    
    
    my_progam.cpp
    -------------------
    #include "functions.h"
    int main()
    {
        int a = 10;
        double b = 20.0;
        
        my_fucntion(a);
        another_function(b);
       
        return 0;
    }
    Last edited by 7stud; August 9th, 2003 at 12:17 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    29
    Rep Power
    0
    I can't describe how stupid I feel right now . . . I spent over an hour baffled by this, and guess what the problem was?

    I FORGOT THE ****ING SEMICOLON AT THE END!!!! AARGH!!!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    I FORGOT THE ****ING SEMICOLON AT THE END!!!

    ...of the constructor declaration in the class definition I presume. Or, was that the semicolon at the end of the class definition? I always forget that one too, so I always type the closing brace with a semicolon BEFORE typing in the members and functions.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    29
    Rep Power
    0
    It just doesn't seem like something you'd need a semicolon for . . . in every other place in the C++ language that a closing bracket is used, there's no semicolon.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    31
    Rep Power
    12
    7stud, i sincerely sympathises with u man....

    I too had pulled some hairs off my poor scalp over the same stupid semi-colon before....
    happy for u man...

IMN logo majestic logo threadwatch logo seochat tools logo