Organizing project properly

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Organizing project properly

Postby nime » Wed Sep 29, 2010 11:29 pm

Hello,
As a C/Cpp beginner I have problems to organize my project properly.
So, I want to have functions which will be used from more than one cpp file in more than one project.
I declared data types and functions from those common file C-stylish in one "il.h" file like this:

Code: Select all
struct myData
{
    char  myFlag[1];
    int   myInt;
    long  myLong;
    float myFloat;
    char  myStr1[12];
    char  myStr2[16];
  };

int myfunction(bool RW, long iRec, struct myData* d);


Myfunction is function in ma il.cpp file for reading or writing (RW) binary record (iRec) from file and return d (data structure) to program (cpp) which calls them.
In my main cpp I also include "il.h" from where I plan to call function and do something with data.

But I get errors like:
C:\Users\John\Desktop\aaa\il.h|12|error: expected ')' before 'RW'
Obviously, I didn't declared something properly.

Will anybody help me to get this to work please?
nime
 
Posts: 7
Joined: Wed Sep 29, 2010 11:10 pm

Re: Organizing project properly

Postby Wizard » Thu Sep 30, 2010 9:01 am

I'm not sure where your problem lies. That code, as far as you've shown, compiles for me just fine.
The actual thing you're trying to do, however, looks like an excellent opportunity to use object oriented programming. You can put your function inside the struct, and then you call the function on the object. Like this:
Code: Select all
class myData
{
public:
    char  myFlag[1];
    int   myInt;
    long  myLong;
    float myFloat;
    char  myStr1[12];
    char  myStr2[16];
    int myfunction(bool RS, long iRec);
};

int myData::myfunction(bool RS, long iRec)
{
  // myfunction is aware of all the stuff in myData here.  You can open your file, load the data, and just set it like so
  myFlag[0] = "Y";
  myInt = 4;
  myFloat = 3.14159;
  // and so on
}

int main()
{
  int iRec = 12345;
  myData data; // here you create a data object; this should be familiar to you
  data.myfunction(true, iRec); // here you call your myfunction which is a part of myData
  // no need to "return" objects or anything like that: since the function is now a part of the object, it can change it directly.  Create a bunch!
  myData data2;
  myData data3;
  myData data4;
  data2.myfunction(..
  data3.myfunction(...
// each one is a different object and the function is called properly on them.
}
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Re: Organizing project properly

Postby nime » Thu Sep 30, 2010 1:01 pm

Hello Wizard,
Thank you on the brief example. Helps me wery much and I will study and aply this for sure.
Now I make a test, open new project and include header file "il.h" (code from first post) and also compiled sucessfuly!
But when I import file with functions code problems are back...
This is my file with function:

Code: Select all

#include "il.h"
#include <stdio.h>

int myfunction(bool RW, long iRec, struct ILRec IL)
    {
        FILE *f;
        f=fopen("junk.dat","wb+");
        if (iRec<1) iRec=1;
        if (!f) return 1;
          {
            fseek(f,(iLen*(iRec-1)),SEEK_SET);
            if (RW)
            {
          //  strcpy(IL.myRowStr,"my string to file");
            fwrite(&IL, iLen, 1, f);
            }
            else
            {
            fread(&IL, iLen, 1, f);
            }
          }
         fclose(f);
         return 0;
    }



Will you try now to find an error please?
I use CodeBlocks IDE (if means something).
nime
 
Posts: 7
Joined: Wed Sep 29, 2010 11:10 pm

Re: Organizing project properly

Postby nime » Thu Sep 30, 2010 1:04 pm

Sorry, this is an old file.
Function is like this: int myfunction(bool RW, long iRec, struct myData* d);
not those which is showed in above code...
nime
 
Posts: 7
Joined: Wed Sep 29, 2010 11:10 pm

Re: Organizing project properly

Postby nime » Thu Sep 30, 2010 2:47 pm

And finally, I had to apologize...
When creating project and files I create file with function as C file. Now I create new cpp file with same code and now compiles. Anyway, this is forum for beginners, isn't it?

And now I try to apply oop concept you suggst and what look's great but in main I get error:
C:\Users\John\Desktop\asdfg\asdfgMain.cpp|108|error: 'struct myData' has no member named 'myfunction'|
how is this possible when function is in class?

I plan to read file in function int myData::myfunction(bool RS, long iRec) and with this I would get data in my struct (class). Is this correct?
nime
 
Posts: 7
Joined: Wed Sep 29, 2010 11:10 pm

Re: Organizing project properly

Postby Wizard » Fri Oct 01, 2010 6:54 am

can you post your code? It's not obvious where the problem is just looking at the error.
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Re: Organizing project properly

Postby nime » Fri Oct 01, 2010 11:26 am

Yes Wiz. I have 3 files involved here.

1. il.h (header included in main.cpp and il.cpp)
Code: Select all
struct myData
{
    char  myFlag[1];
    int   myInt;
    long  myLong;
    float myFloat;
    char  myStr1[12];
    char  myStr2[16];
  };
int iLen=sizeof(struct myData);  // for calculating start byte in binary file etc...
int myFileRW(bool RW, long iRec, struct myData* d);  // myFunction


2. il.cpp, file with my function and now plus one class... (I would like to try both ways).
Code: Select all
#include "il.h"
#include <stdio.h>
#include <stdlib.h>

class cls_Data
{
public:
    char  myFlag[1];
    int   myInt;
    long  myLong;
    float myFloat;
    char  myStr1[12];
    char  myStr2[16];
    int myfunction(bool RS, long iRec);
};

int cls_Data::myfunction(bool RS, long iRec)
{
  // myfunction is aware of all the stuff in myData (cls_Data) here.
  // You can open your file, load the data, and just set it like so
    myFlag = "1";
    myInt = 4;
    myFloat = 3.14159;
  // and so on

Maybe I can call  myFileRW from here to "fill" cls_Data and write cls_Data to file?

};

int myFileRW(bool RW, long iRec, struct myData* d)
{
    {
        FILE *f;
        f=fopen("junk.dat","wb+");
        if (iRec<1) iRec=1;
        if (!f) return 1;
          {
            fseek(f,(iLen*(iRec-1)),SEEK_SET);
            if (RW)
               {fwrite(&d, iLen, 1, f);
                    }
               else
               {fread(&d, iLen, 1, f);
                    }
          }
         fclose(f);
         return 0;
    }
};


3. main file from which I call function an class...
Code: Select all

// stuff with calss, compiler breaks because of cls_Data not defined here...
cls_Data GetData;
bool RW;    //true=Write, false=Read
long iRec;  //Record to read (iRec*iLen)
GetData.myfunction(RW, iRec);
//------------------------------------------------
// this also dont work...
int a;
myData* d;  //error reported here
a=myFile(false, 1, d);


I cant understand what is wrong but I noted that here's several errors.
Purpose of all this is to get struct data in main to fill textboxes or so and after changing to set struct data and write it back to file, I think this is obvious...

Thank you for helping!
nime
 
Posts: 7
Joined: Wed Sep 29, 2010 11:10 pm

Re: Organizing project properly

Postby nime » Sun Oct 03, 2010 1:29 am

However, I can upload files. I see now :?
Attachments
myProj.rar
(1.67 KiB) Downloaded 54 times
nime
 
Posts: 7
Joined: Wed Sep 29, 2010 11:10 pm

Re: Organizing project properly

Postby nime » Tue Oct 05, 2010 1:03 pm

Will anybody fix my project to get it working, please?
nime
 
Posts: 7
Joined: Wed Sep 29, 2010 11:10 pm


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 2 guests