Hey ppl,
I got a little problem :/
I got the following C code in a header file and I am not allowed to change it: - const structTemp tmp = {.x = 5};
The code just works fine for GCC.
Now I got a project made with the Eclipse IDE in C++. And I am linking to the header file. Then I get the following error:
"expected primary-expression before '.' token"
What can I do to handle this problem? :(
I just tried to make a extern "C" {} around the header, but it does not work :(
Anyone got some help for me? :(
Greets
9 6282
I don't believe you. Firstly, because you are omitting the keyword 'struct': in C++, you are allowed to omit the struct keyword, but then you compile it with a C++ compiler like g++ not a C compiler like gcc.
Secondly, because this is not standard C or C++ syntax. You initialize a struct thus: -
struct structTemp
-
{
-
int x;
-
char c;
-
}; // definition
-
-
structTemp tmp1 = {5,'A'}; // initialization (in C++)
-
struct structTemp tmp2 = {5,'A'}; // initialization (in C/C++)
-
struct structTemp tmp3; // delcaration (C/C++)
-
tmp3.x = 5; // initialization
-
tmp3.c = 'A';
-
The struct is defined by something like: SORRY, I now found the correct definition, and its not only struct, it looks like this: -
typedef union
-
{
-
struct
-
{
-
int h;
-
int l;
-
} x1;
-
unsigned int x;
-
} structTemp;
-
Then I got this code - const structTemp tmp = {.x = 5};
It compiles great with
where main.c links the header file with all this code in it.
When I link the header into my Eclipse project C++ code, I got the error from my first post.
In that case the answer is simple: while some C compilers support this method of initializing unions, pre-C99 and C++ does not. You'll have to use assignment instead of initialization.
In that case the answer is simple: while some C compilers support this method of initializing unions, pre-C99 and C++ does not. You'll have to use assignment instead of initialization.
Damn :( Ok, thanks :(
And how to assign a const variable? :(
Banfa 9,065
Expert Mod 8TB
You can't assign to a const variable, only initialise it but you could initialise in either of these 2 ways to get the same effect as your original code that is usable everywhere. -
const structTemp tmp = {5, 0};
-
-
const structTemp tmp = {5};
-
You can't assign to a const variable, only initialise it but you could initialise in either of these 2 ways to get the same effect as your original code that is usable everywhere. -
const structTemp tmp = {5, 0};
-
-
const structTemp tmp = {5};
-
Yeah, thats a good idea. Ill do it this way.
But when the structTemp looks like this: -
typedef union
-
{
-
struct
-
{
-
int h;
-
int l;
-
} x1;
-
unsigned int x;
-
} structTemp;
-
Wouldnt be - const structTemp tmp = {0, 5};
be the right way?
I know they are union, but how should the compiler know, that the 5 is meant for variable x?
Greets,
Edit:
And what how could I initialize x1.h and x1.l?
Edit2:
Ok, I tried it out by myself:
const structTemp tmp = {5}; is right for making x = 5.
And:
const structTemp tmp = {{10, 5}}; is right for making h = 10 and l = 5.
Thx anyway :)
Ok, one last question about mixing C and C++.
If I got a variable called "delete" in my C code, is there a way to make the C++ ignore it?
Its C, but the C++ Compiler gives me an error cos delete is a keyword in C++.
With extern "C" {}; I can't solve the problem :(
Banfa 9,065
Expert Mod 8TB
If I got a variable called "delete" in my C code, is there a way to make the C++ ignore it?
If it is C then compile it with a C compiler.
If it is C++ then you can't use the keyword delete as a variable name.
If you are using a C++ compiler to compile it then it isn't C although it might be code that is compatible with both C and C++ compilers, which is in fact a subset of both languages which still would include not using C++ keywords as variable names.
If it is C then compile it with a C compiler.
If it is C++ then you can't use the keyword delete as a variable name.
If you are using a C++ compiler to compile it then it isn't C although it might be code that is compatible with both C and C++ compilers, which is in fact a subset of both languages which still would include not using C++ keywords as variable names.
Ok. Then I have to rename it. Thanks!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Geiger Ho |
last post by:
Hi all,
Consider:
a.hh
====
struct A {
mytype_t attr1,
mytype_t attr2,
|
by: Ardhendu Nandan |
last post by:
I am trying to compile Gcc compiler in Windows O/S.I have downloaded
entire source Code of this compiler at tries to run it in DEVCPP but I
am gating some linker error and some header files missing...
|
by: bill |
last post by:
I recently realized that I have a structure that I'd like to put more
protection on in the following sense: I'd like to modify
struct foo{
int *x;
} ;
to be:
struct foo {
const int *x;
|
by: Lee |
last post by:
Hi
I am not sure whether i am going off topic in asking this.
It will be great help for me if any one could pinpoint locations where
i could download the free gcc compiler to work on windows xp....
|
by: Bill Pursell |
last post by:
Suppose I have a structure with many members, and I pass a pointer to
that structure to a function. I'd like the prototype of the function
to specify that it will only be changing certain members...
|
by: bramdoornbos |
last post by:
Hello,
I am looking for a solution to interface with C++ classes implemented
in a dll compiled by gcc.
This dll will be however accessed by a visual c++ compiled host (not
made by me).
Both...
|
by: mahesh |
last post by:
Hi all,
I am using Dev C++ IDE for the programming but for the time efficiency,
I tried to use supercomputer (Linux) to run my program, but some of the
function such as
pow(double,double) ,...
|
by: Herhor |
last post by:
I downloaded latest stable MinGW 5.1 but I have have already discovered
that included gcc 3.4.2 is pretty old 2004 version. It is really strange
because current GNU GCC version is 4.2.0!
Is that...
|
by: arnaudk |
last post by:
How does one initialize a const struct member of an object? Since it's const, this has to be done in the constructor initialization list. Unfortunately I can't seem to get the syntax right...
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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...
|
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...
| |