2

I am creating two 2D arrays in one file, say readbundle.cpp. They are quite huge in dimension, as they actually 3D points created from an image, after some heavy mathematics.

Now, how can I use the values from here, in another file, resectioning.cpp? I know this uses the concept of oops and classes. But if you can just temme the probable syntax and how to use them, it would be really helpful. Please help me here. Thanks in advance. I have searched in google, but since I am new to this,I am not sure what am I looking at or where to look. My apologies if you feel this is very rudimentary.

The code is in c++.

They are 2 different files totally. I haven't made them a same program or created any class. This is what I want to know. How to connect them, and how to use the values from one in another.

Suppose X[i][j] is just normally created and defined in readbundle.cpp and after compiling and executing it, I have certain values in X[i][j].

Now I want to use this X[i][j] in the program, resectioning.cpp, which I am compiling separately. I haven't defined any class, or any oops. Please help me how can I achieve this. These two programs aren't connected as of now. They are just normal programs. I know I have to do something public and call the variable somehow. But I just dunno the right way of doing it. Please correct me if am wrong as well.

Edit

suppose the readbundle.cpp is as follows

    #include ...
    .
    . 
    vector<vector<int> >X3D;(declared global)
    .
    .

and the resect.cpp is as follows

    #include ....
    .
    .
    .
    extern vector<vector<int> >X3D //will the values be reflected here?
    //the values changed in *readbundle.cpp*

    int main()
    {
          cout<<X3D[0][0]<<endl;
    }

From the answers, I hope I understood the concept of extern correct. Please help me if am wrong.

NOTE Will I get the same values X3D from the previous file? Am getting an error in this case. How can I achieve the functionality am looking for?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Lakshmi Narayanan
  • 5,220
  • 13
  • 50
  • 92
  • 3
    Is it different processes, or just different source files in the same program? If the first, then read about [shared memory](http://en.wikipedia.org/wiki/Shared_memory), if the second you should learn about the external [storage class specifier](http://en.cppreference.com/w/cpp/language/storage_duration). – Some programmer dude Jun 06 '13 at 12:37
  • They are 2 different files. I do not know how to make them same program as well @JoachimPileborg. I have these 2 files in a same folder. I hope am clear here, please help me. – Lakshmi Narayanan Jun 06 '13 at 12:38
  • What IDE you are using? – Dmitry Sazonov Jun 07 '13 at 11:06
  • 1
    possible duplicate of [What are extern variables in C?](http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c) – sashoalm Jun 07 '13 at 11:38
  • am using the linux terminal @DmitrySazonov, with g++ compiler – Lakshmi Narayanan Jun 07 '13 at 13:37

2 Answers2

3

You can do this like this:

In your arrayfile you declare the array however it looks like, i.E.

char MyArray[] = { 123, 20, -4, ...};
unsigned int MyArraySize = sizeof(MyArray);

In your sourcefile you can reference it like this:

extern char MyArray[];
extern unsigned int MyArraySize;

If you know the size in advance you can do

char MyArray[10] = ...;

and

extern char MyArray[10];

Update

Here is a sample code to get you started. Create the files as indicated. If you change the names don't forget to change the include directive as well. ;)

ArrayFile.h:

#ifndef ARRAYFILE_H_
#define ARRAYFILE_H_

#define ARRAY_ROWS      5
#define ARRAY_COLUMNS   3


#endif /* ARRAYFILE_H_ */

ArrayFile.cpp:

#include "ArrayFile.h"

char BundleArray[ARRAY_ROWS][ARRAY_COLUMNS] =
{
    { 1, 2, 3 },
    { 2, 4, 5 },
    { 3, 6, 7 },
    { 4, 8, 9 },
    { 5, 0, 6 },
};

ArraySample.cpp

#include <iostream>

#include "ArrayFile.h"

extern char BundleArray[ARRAY_ROWS][ARRAY_COLUMNS];

int main()
{
    unsigned int rows = ARRAY_ROWS;
    unsigned int columns = ARRAY_COLUMNS;

    for(unsigned int x = 0; x < rows; x++)
    {
        std::cout << "Row:" << x << std::endl;
        for(unsigned int y = 0; y < columns; y++)
        {
            std::cout << "Value [" << x << "/" << y << "] = " << (char)((BundleArray[x][y])+0x30) << std::endl;
        }
    }
    return 0;
}

When you created these files, compile them.

harper
  • 13,345
  • 8
  • 56
  • 105
Devolus
  • 21,661
  • 13
  • 66
  • 113
  • the file readbundle.cpp and resectioning.cpp are both in the same folder. The array is declared, defined in readbundle.cpp. Should I use the extern command in resectioning.cpp? will it directly use the array from readbundle with no other link? Please clarify. Sorry for asking again. – Lakshmi Narayanan Jun 06 '13 at 13:05
  • 1
    The compiler doesn't care about directories, only about modules (i.E. object files). If you talk about files you must read them into memory and then use them from wherever you want, but you were talking about source files. The extern command tells the compiler that it will find the data somehwere else so you don't get an error. the linker takes care to fill the proper adress at the linking stage. – Devolus Jun 06 '13 at 13:06
  • Please pardon me, but I don't understand. I haven't created any objects also. I have compiled the readbundle file. Could you please take me from here.? I have not linked both the codes as well. Thus am bit confused how can the compiler look for this variable, around the system, without me specifying any link or the file where it has to look. My apologies for not understanding clearly. – Lakshmi Narayanan Jun 06 '13 at 13:34
  • How are these arrays created? – Devolus Jun 06 '13 at 13:57
  • just normal int. int X[i][j]. No objects, and no class. That is why I was asking how to do it with class, if it is required. My apologies if am not clear. – Lakshmi Narayanan Jun 06 '13 at 21:07
  • If the arrays are very big, it might be better to write them to a file and read them from there. From your qeustions I have the impression that you don't really know how to use a comiler though. Which environment are you using? Linux or Windows, which compiler? – Devolus Jun 07 '13 at 05:00
  • Am using g++, linux. yes, am quite new to this. Could you understand my exact issue? could you please help me.? – Lakshmi Narayanan Jun 07 '13 at 07:01
  • 1
    I created a short sample to demonstrate what you must do. You can take it as it is and fill it with your own data and write whatever processing yoou need in the main function. – Devolus Jun 07 '13 at 10:57
  • 2
    @LakshmiNarayanan I think you are confusing the term 'object files', such as Devolus mentioned, with the OOP programming concept. Object files (typically the files ending with .o or .obj) do *not* refer to objects or classes, but to compiled source files. The object files are then combined by the 'linker' into a single executable. The linker looks at all the object simultaneously, and figures which variables from one 'object' (not program) are referred to in another object such that their compiled code can be fused into the executable. – catchmeifyoutry Jun 07 '13 at 11:15
  • @catchmeifyoutry please check the edit and let me know if am on the right track. – Lakshmi Narayanan Jun 07 '13 at 13:44
  • @LakshmiNarayanan I guess that's ok (except "(declared global)" should be a comment), but the thing is your post still talks about *multiple* programs, while you want to create a *single* program from multiple *source files*. Indeed, your example code also seems to show you have only one main() function, thus resect.cpp is not intended to be a standalone program. Did you succeed in linking the object files into a single executable? Does it work now? – catchmeifyoutry Jun 07 '13 at 18:06
  • my apologies. Please check the code now. It is actually 2 programs. I forgot to add main for resect.cpp. @catchmeifyoutry – Lakshmi Narayanan Jun 08 '13 at 08:53
  • Yes, your updated is similar to what I posted for you. And don't call it "programs". That's two source files, which are compiled into a single program. It's confusing if you use such a wrong terminilogy. – Devolus Jun 08 '13 at 12:57
2

I assume that the two files you mention are both source files and you want to complile them seperately and run them as part of the same program.

Look into how your linker works and have a look at static variables.

nurdglaw
  • 2,107
  • 19
  • 37
  • yes you are right. they are totally different files and I am planning to compile them separately. Right now, I haven't created class as well, as am not pretty sure how to put them to good use. I will look into them, thanks. But am not able to click on the links you have provided. – Lakshmi Narayanan Jun 06 '13 at 12:41
  • 1
    @LakshmiNarayanan - They weren't links, they are terms I suggest you put into Google. The `linker` is the program that combines files generated by seperate runs of the compiler into a single program, and `static` variables are one way of passing information between seperate compilation units. Note also, that this isn't a very object-oriented mechanism, but you have to start somewhere :-) Good luck! – nurdglaw Jun 06 '13 at 12:45