?
Solved

Trouble Passing a structure around?

Posted on 2003-02-28
6
Medium Priority
?
194 Views
Last Modified: 2010-04-01
Hi,
My question is this, I have multiple files that need to use one structure it looks like:
typedef struct StorageInfo {
char Mask[MAX_PATH];
char Directory[MAX_PATH];
char Condition[MAX_PATH];

int month;
int year;
int day;

char newDirectory[MAX_PATH];
}StorageInfo;

I declared it in Search.h, now Files.cpp and Parse.cpp need to use it as well as Search.cpp. I included Search.h in all the the .cpp files above. But Yet I m getting errors in Parse.cpp like 'StorageInfo' undeclared. Even though I included Search.h both in Parse.h and Parse.cpp

I created the structure in Files.cpp as follows,
StorageInfo * s = NULL;
s = (StorageInfo * ) malloc(sizeof(StorageInfo));
I need to be able to pass it from class functions to global functions and back. I had it as *& so it gets modified and stays modified. But I can't get it compile.
Then I get weird erros in Parse.cpp like
'<Unknown>' function-style intializers appears to be function definition
I have never seen these errors before.
Any ideas on how to get my program to love my structure? lol
Thanks.
P.S. This in VC++ 6.0 MFC Project... But i posted the question here because it isn't mfc releated I dont' think.
Thanks again.
0
Comment
Question by:cod3po3t
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:bcladd
ID: 8044215
Are you really using C++ or are you using C? If you are using C++ it is probably clearer to forgo the typedef:

struct StorageInfo {
char Mask[MAX_PATH];
char Directory[MAX_PATH];
char Condition[MAX_PATH];

int month;
int year;
int day;

char newDirectory[MAX_PATH];
};

Now, in C++, you can use the new operator to allocate a new StorageInfo on the heap. Then use a typedef for the pointer:

typedef StorageInfo * StoragePtr;

Now it should be easy to declare a variable and use it:

StoragePtr s = NULL;
s = new StorageInfo;

No constructor is provided so C++ compiler will generate one for you.

Use delete to get release the information:

delete s;

If you must use malloc, you should still use the first declaration above as it should get rid of the reported compiler error. (note: above code is untested but should be correct).

-bcl
0
 

Author Comment

by:cod3po3t
ID: 8044713
After doing what you said I m only getting errors in the parse files. Below are the files.

parse.h
#ifndef _parse_h
#define _parse_h

#include "info.h"

bool parseDate(StoragePtr s);
bool parseMask(StoragePtr s);
bool parseDirectory(StoragePtr s);
#endif

Parse.cpp
bool parseDate(StoragePtr s)
{
return true;
}

bool parseDirectory(StoragePtr s)
{
return true;
}
bool parseMask(StoragePtr s)
{
     return true;
}

info.h
#ifndef _info_h
#define _info_h
struct StorageInfo {
char Mask[MAX_PATH];
char Directory[MAX_PATH];
char Condition[MAX_PATH];

int month;
int year;
int day;

char newDirectory[MAX_PATH];
};
typedef StorageInfo * StoragePtr;
#endif _info_h

I m getting errors like, StoragePtr undefined, syntax error missing ) before identifier s, syntax error: )
All teh errors are in parse.cpp and point to either the prototype or the { each function has the same errors.
Any ideas?
0
 
LVL 11

Expert Comment

by:bcladd
ID: 8045898
You never include parse.h in parse.cpp.

That should fix the problem.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cod3po3t
ID: 8045975
No I didn't paste the includes these are my following includes:
#include "info.h"
#include "parse.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stdafx.h"

Any new ideas?
0
 
LVL 11

Accepted Solution

by:
bcladd earned 180 total points
ID: 8046068
(1) I get no error with Borland C++ 5.5 (after commenting out "stdafx.h"). Studio .NET gives your errors until I rearrange the header files:
 
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stdafx.h"
#include "info.h"
#include "parse.h"

I don't see why this fixes the problem but it does.

Hope this helps, -bcl
0
 

Author Comment

by:cod3po3t
ID: 8046146
You rule man! Thanks a ton!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question