C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 8th, 2003, 09:47 PM
rjahrman rjahrman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 29 rjahrman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?

Reply With Quote
  #2  
Old August 8th, 2003, 11:42 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #3  
Old August 9th, 2003, 12:13 AM
rjahrman rjahrman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 29 rjahrman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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!!!

Reply With Quote
  #4  
Old August 9th, 2003, 12:22 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #5  
Old August 9th, 2003, 01:55 AM
rjahrman rjahrman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 29 rjahrman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #6  
Old August 9th, 2003, 12:51 PM
siddy siddy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Singapore
Posts: 31 siddy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 20 sec
Reputation Power: 6
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...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Where to Put a Struct?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT