Hi,
I tried to share some data between 2 .cc files hence declared the arrays of structure as extern in one of the .cc files.
However I get abnormal entries in the original array entries otherwise I wouldn't have got.
file1.h
---------
struct Mylistdetails {
int id;
int value;
}
file1.cc
----------
struct Mylistdetails myList_[MAX_NO];
file2.cc
-----------
#include <file1.h>
extern struct Mylistdetails myList_[MAX_NO];
The purpose here is to call these array data into the file2.Here,myLi st_[i].id and myList_[i].value are just unexpected once declared as 'extern'.If not used this extern method the expected values are observed once only file2 is being used.
Could you kindly specify the behaviour of this.Many thanks for your advise on how to tackle such a problem.
18 9331 horace1 1,510
Recognized Expert Top Contributor
did your code compile?
there is a ; missing in -
struct Mylistdetails {
-
int id;
-
int value;
-
}; // missing ;
and when including a file in the current directory it should be in ""
e.g. file2.cc -
#include "file1.h"
-
-
extern struct Mylistdetails myList_[MAX_NO];
-
you could put the extren declaration in the header file
did your code compile?
there is a ; missing in -
struct Mylistdetails {
-
int id;
-
int value;
-
}; // missing ;
It compiled without any problem.
and when including a file in the current directory it should be in ""
e.g. file2.cc -
#include "file1.h"
-
-
extern struct Mylistdetails myList_[MAX_NO];
-
you could put the extren declaration in the header file
well it is not in the current directory, so i specified the path
#include <direct/file1.h>
Yes I tried putting extern declaration in the heade file too(file1.h).St ill doesn't work.
Thanks for your attention.
horace1 1,510
Recognized Expert Top Contributor
well it is not in the current directory, so i specified the path
#include <direct/file1.h>
Yes I tried putting extern declaration in the heade file too(file1.h).St ill doesn't work.
Thanks for your attention.
I guess you are on a unix system, try -
#include "direct/file1.h"
assuming you have a directory direct in your current directory which contains the file1.h
I guess you are on a unix system, try -
#include "direct/file1.h"
assuming you have a directory direct in your current directory which contains the file1.h
I'm working on Linux, and it works normally in #include<direct/file1.h> style.
Could you explain me how could I use, extern with arrays of structure?
Thank you
horace1 1,510
Recognized Expert Top Contributor -
#include <filename> // include file from 'standard' place
-
the file is assumed to be in a 'standard' directory in the file system which the C/C++ compiler automatically searches,. -
#include "filename" // include file from current directory else 'standard' place
-
The second form of #include (filename enclosed in "") searches the current directory for filename and then, if it is not found, the 'standard' directory.
which you use it depends where your direct/file1.h file is - in the 'standard' place or relative to the current directory?
try the following -
// file1.h
-
-
struct Mylistdetails {
-
int id;
-
int value;
-
};
-
-
#define MAX_NO 10
-
-
extern struct Mylistdetails myList_[MAX_NO];
-
-
void setup(); // function prototype - function in file2.cpp
-
-
// file2.cpp
-
-
#include "file1.h"
-
-
void setup()
-
{
-
for(int i=0;i<MAX_NO; i++)
-
myList_[i].id=i;
-
}
-
-
// file1.cpp
-
#include <cstdlib>
-
#include <iostream>
-
#include "file1.h"
-
-
using namespace std;
-
-
struct Mylistdetails myList_[MAX_NO];
-
-
int main(int argc, char *argv[])
-
{
-
setup();
-
for(int i=0;i<MAX_NO; i++)
-
cout << myList_[i].id << endl;
-
return EXIT_SUCCESS;
-
}
-
when i build this
g++ file1.cpp file2.cpp
and run it I get
./a
0
1
2
3
4
5
6
7
8
9
-
#include <filename> // include file from 'standard' place
-
the file is assumed to be in a 'standard' directory in the file system which the C/C++ compiler automatically searches,. -
#include "filename" // include file from current directory else 'standard' place
-
The second form of #include (filename enclosed in "") searches the current directory for filename and then, if it is not found, the 'standard' directory.
which you use it depends where your direct/file1.h file is - in the 'standard' place or relative to the current directory?
try the following -
// file1.h
-
-
struct Mylistdetails {
-
int id;
-
int value;
-
};
-
-
#define MAX_NO 10
-
-
extern struct Mylistdetails myList_[MAX_NO];
-
-
void setup(); // function prototype - function in file2.cpp
-
-
// file2.cpp
-
-
#include "file1.h"
-
-
void setup()
-
{
-
for(int i=0;i<MAX_NO; i++)
-
myList_[i].id=i;
-
}
-
-
// file1.cpp
-
#include <cstdlib>
-
#include <iostream>
-
#include "file1.h"
-
-
using namespace std;
-
-
struct Mylistdetails myList_[MAX_NO];
-
-
int main(int argc, char *argv[])
-
{
-
setup();
-
for(int i=0;i<MAX_NO; i++)
-
cout << myList_[i].id << endl;
-
return EXIT_SUCCESS;
-
}
-
when i build this
g++ file1.cpp file2.cpp
and run it I get
./a
0
1
2
3
4
5
6
7
8
9
In fact I want to retrieve file1 data into file2.If I understand your code correctly it does the other way round.
I tried this way and when I compile I get the following error
file1.h:270: error: storage class specified for 'myList_'
Thank you for your suggestions.
horace1 1,510
Recognized Expert Top Contributor
try this which passes data from file1.cpp to file2.cpp -
// file1.h
-
-
struct Mylistdetails {
-
int id;
-
int value;
-
};
-
-
#define MAX_NO 10
-
-
extern struct Mylistdetails myList_[MAX_NO];
-
-
void setup(); // function prototype - function in file1.cpp
-
void print(); // function prototype - function in file2.cpp
-
-
-
// file2.cpp
-
-
#include <iostream>
-
#include "file1.h"
-
-
using namespace std;
-
-
-
void print()
-
{
-
for(int i=0;i<MAX_NO; i++)
-
cout << myList_[i].id << endl;
-
}
-
-
-
// file1.cpp
-
-
#include <cstdlib>
-
#include <iostream>
-
#include "file1.h"
-
-
using namespace std;
-
-
struct Mylistdetails myList_[MAX_NO];
-
-
void setup()
-
{
-
for(int i=0;i<MAX_NO; i++)
-
myList_[i].id=i;
-
}
-
-
-
int main(int argc, char *argv[])
-
{
-
setup();
-
print();
-
return EXIT_SUCCESS;
-
}
-
try this which passes data from file1.cpp to file2.cpp -
// file1.h
-
-
struct Mylistdetails {
-
int id;
-
int value;
-
};
-
-
#define MAX_NO 10
-
-
extern struct Mylistdetails myList_[MAX_NO];
-
-
void setup(); // function prototype - function in file1.cpp
-
void print(); // function prototype - function in file2.cpp
-
-
-
// file2.cpp
-
-
#include <iostream>
-
#include "file1.h"
-
-
using namespace std;
-
-
-
void print()
-
{
-
for(int i=0;i<MAX_NO; i++)
-
cout << myList_[i].id << endl;
-
}
-
-
-
// file1.cpp
-
-
#include <cstdlib>
-
#include <iostream>
-
#include "file1.h"
-
-
using namespace std;
-
-
struct Mylistdetails myList_[MAX_NO];
-
-
void setup()
-
{
-
for(int i=0;i<MAX_NO; i++)
-
myList_[i].id=i;
-
}
-
-
-
int main(int argc, char *argv[])
-
{
-
setup();
-
print();
-
return EXIT_SUCCESS;
-
}
-
I still get this error while trying to compile.
file1.h:270: error: storage class specified for ‘myList_’
Thank you
horace1 1,510
Recognized Expert Top Contributor
I still get this error while trying to compile.
file1.h:270: error: storage class specified for ‘myList_’
Thank you
I have run the code in my last post on Linux and Windows (using Cygwin). I compile with
g++ file1.cpp file2.cpp
then execute the file a.out or a.exe OK
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Kieran Simkin |
last post by:
I have in my project in main.c a number of arrays defined like this (outside
of any functions):
char muser, mpass;
I'm declaring them in a number of other .c files (inside functions) like
this:
extern char muser, mpass;
However, in one of these functions outside of main.c I need to snprintf into
these buffers. Usually when using snprintf I call it like this:
snprintf(muser, sizeof muser, "text");
|
by: Peter Seaman |
last post by:
I understand that structures are value types and arrays and classes are
reference types. But what about arrays as members of structures i.e. as in
C
struct x
{
int n;
int a;
}
|
by: Jeff |
last post by:
I am struggling with the following
How do I marshal/access a pointer to an array of strings within a structure
Than
Jef
----------------------------------------------------------------
|
by: balasam |
last post by:
Dear friend,
I am having Linux platform.I am declaring the array of
structure in one file and how can access the values of the array of
structure in another file.
Using " extern " ,to extern the structure but when i
access the values of the array of structure ,the compilers displays the
error message.
Program :
|
by: David Fort |
last post by:
Hi,
I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a
function provided in a DLL.
The function takes the address of a structure which it will fill in with
values.
I get an error:
----------------
An unhandled exception of type 'System.NullReferenceException' occured in
| |
by: ruffiano |
last post by:
I have a file called first.cpp that contains the following
declarations:
static const char *MY_STRING1 = "My first string";
static const char *MY_STRING2 = "My second string"
If I wanted to use MY_STRING1 and MY_STRING2 in a header file, would
the following be correct?
extern static const char *MY_STRING1;
extern static const char *MY_STRING2;
|
by: Aston Martin |
last post by:
Hi All,
********************** My Situation **********************
I am working on project that involves passing a structure to unmanaged
code from .Net world (well using C#). Perhaps an example will prove
useful.
structure MyStruct
{
// this is a complicated struct declaration in the sense
|
by: Scoots |
last post by:
I know the usually applied workaround for multiple definitions of
header files, but I have a problem on this one.
This time, I can't just ifndef the header file that defines my
structure.
So I have two classes that I've managed to dodge around this problem
for a while, but now I need the include in both classes. This wasn't
a problem until now, when I'm trying to declare an stl map with a
structure as a data type.
|
by: sharat |
last post by:
Hi all.
I am writing a a code in c++ . i have defined a global structure in a
header file(user define header file) say headerfile1.h and declared a
class in file2.h which is having some public funtion which returns the
struct variable and definition of the fun in in file1.cpp .
I am getting the error in compilation that "struct_var' has
incomplete type and not allowed and return type is incoplete. If i am
trying to return the structure...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |