C++ anonymous union redeclaration error - c++

I want to create a struct that can be used to store 3D coordinates or a linear equation.
Here is the code:
struct myStruct {
union {
// coordinates (3d)
struct {
int x,y,z;
};
// linear equation (ax+b)
struct {
int a,b,x;
};
};
};
And I get the following error:
error: redeclaration of ‘int myStruct::<anonymous union>::<anonymous struct>::x’
I'm on linux mint 18.04, g++ (5.4.0), compile with --std=c++11.
I understand the problem. But have few questions.
I saw something related working on windows, why?
What is the best way to implement it so it works well on both (linux/win)?

Just give them names. This should be fine:
struct myStruct {
union {
struct coordinates { int x,y,z; };
struct linear_equation { int a,b,x; };
coordinates coord;
linear_equation lin_eq;
};
};
I also allowed myself to add some members to the union. However, the two structs have members of same types and quantity, so imho entering the trouble of using a union is questionable.

Just to complement the answer by user463035818. You can simplify your union a bit, by declaring the members directly, e.g.:
struct myStruct {
union {
// coordinates (3d)
struct {
int x,y,z;
} coord;
// linear equation (ax+b)
struct {
int a,b,x;
} lin_eq;
};
};

Related

C++ anonymous struct as std::map value

I am using Visual Studio 2019 std:c++ 17 which supports anonymous struct, f.e.
struct S
{
struct { int i; };
};
However, the following code has compile errors.
map<int, struct { int i; }> m;
Can I use anonymous struct as value type of std::map?
You can't do it directly, but indirectly, either
struct { int i; } s;
std::map<int, decltype(s)> a;
or
using MyType = struct { int i; };
std::map<int, MyType> b;
but note that a and b will be of different type. decltype(s) is not the same type as MyType.
Anonymous structure is a structure that is defined within another class (structure). So you can not use such a structure inside a container because in this case it will not be an anonymous structure but will be just an unnamed structure.
In this record (that is incorrect)
map<int, struct { int i; }> m;
there is no anonymous structure. There is an attempt to use an unnamed structure.
You could write for example
#include <iostream>
#include <map>
int main()
{
using S = struct { int i; };
std::map<int, S> m;
}
But as I said there is no anonymous structure.
Here is the definition of the notion anonymous structure (the C Standard, 6.7.2.1 Structure and union specifiers)
13 An unnamed member of structure type with no tag is called an
anonymous structure;

How do I define the same named structure in more than one class?

I've got multiple classes from multiple engineers which I am using and they have the same named structures in the classes. From this I get the error "'struct' type redefinition". How do I get around this?
Example:
// Eng1Class.h
#pragma once
struct Eng1And2SameName
{
unsigned int bottle;
};
class Eng1Class
{
public:
Eng1Class();
~Eng1Class();
};
.
// Eng2Class.h
#pragma once
struct Eng1And2SameName
{
float x, y;
};
class Eng2Class
{
public:
Eng2Class();
~Eng2Class();
};
.
// Main Program
#include "stdafx.h"
#include "Eng1Class.h"
#include "Eng2Class.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Error: error C2011: 'Eng1And2SameName' : 'struct' type redefinition
According to this Compile error "'struct' type redefinition" although it's the first definition for it the #pragma once should fix the issues, but I still see the error. Any insights you can provide?
No, #pragma once prevents the header files from being included more than once - each is included once -> redefinition.
they have the same named structures in the classes
*header files
The're not defined inside the classes (nested), but they could be:
class Eng1Class
{
public:
struct Eng1And2SameName
{
unsigned int bottle;
};
Eng1Class();
~Eng1Class();
};
Or you could enclose the contents of those headers into two differently named namespaces.
Defining a namespace would help
For example as you said error with same struct definition in same namescope .
Reports error
You can do it by defining namesapce
#include<iostream>
using namespace std;
namespace Eng1 {
struct Eng1And2SameName
{
unsigned int bottle;
};
}
namespace Eng2
{
struct Eng1And2SameName
{
float x, y;
};
}
int main()
{
Eng1::Eng1And2SameName a;
Eng2::Eng1And2SameName b;
return 0;
}
Usually engineers working on the same product are coordinated somehow, at least they will use a common source code repository and a common build. Hence, conflicts should have come up earlier.
"uncoordinated" engineers may happen when they work on different products, and if so, each product could have its own namespace. Thereby, you can combine the products without having conflicts:
// in a header:
namespace Eng1Class {
struct Eng1And2SameName
{
unsigned int bottle;
};
class EngClass
{
public:
EngClass();
~EngClass();
};
}
// in the cpp-file
Eng1Class::EngClass::EngClass() {
cout << "hello, Class 1";
}
// in another (or even the same) header
namespace Eng2Class {
struct Eng1And2SameName
{
float x, y;
};
class EngClass
{
public:
EngClass();
~EngClass();
};
}
// in another (or even the same) cpp-file
Eng2Class::EngClass::EngClass() {
cout << "hello, Class 2";
}

C++: Want to make an easy nested struct

I'm a C++ beginner and trying to make a nested struct which has 2 sub-structs under it.
The code is:
struct Sub_number{
int one;
int two;
};
struct Sub_size{
int width;
int height;
};
struct MainStruct{
struct Sub_number number;
struct Sub_size size;
}main;
and I got [Cannot use dot operator on a type] error from Xcode when I tried to put a value in it like this:
main.number.one = 13;
^
Does anyone has any ideas what's wrong with this code...?
Thank you so so much everyone. As you wrote, the name I've using was the no-good point!! Silly me.. I'll double check when I'm going to ask on StackOverflow next time.
Thanks!
main is the reserved word for main function (starting point of application) you need to change the variable name to something else . This will fix the issue
struct Sub_number {
int one;
int two;
};
struct Sub_size {
int width;
int height;
};
struct MainStruct {
struct Sub_number number;
struct Sub_size size;
}someVariable;
void main() {
someVariable.number.one = 1;
}
your struct name can't be main .main is a unique function name int main().change the struct name to others!

How to set/get value in struct inside a struct

How can i access my struct to get/set value inside of it??
Here my example code
#include <iostream>
using namespace std;
typedef struct t_TES
{
double dTes;
}TES;
struct SAMPLE1
{
struct TES;
};
int main()
{
SAMPLE1 sss;
//How can i get/set dtes value??
sss.TES.dtes=10;
cout<<sss.TES.dtes<<endl;
return 0;
}
Is it posible to assign value like this "sss.TES.dtes=10";
and get the value like by calling this "sss.TES.dtes";
i already try to combine both -> or :: operator to get/set value but always got an compilation error.
Pardon me for my bad english, Thanks..
structs in C++ don't need typedef or the struct keyword for instances, but they do need names for their members. Also, it's a case-sensitive language, so dtes is not the same as dTes. Try:
#include <iostream>
using namespace std;
struct TES
{
double dTes;
};
struct SAMPLE1
{
TES tes;
};
int main()
{
SAMPLE1 sss;
sss.tes.dTes = 10;
cout << sss.tes.dTes << endl;
return 0;
}
You have two problems with the SAMPLE1 structure: The first is that you use struct TES when TES is not actually a structure (it's an alias of a structure). The second problem is that you have to actually declare a member in the SAMPLE1 structure:
struct SAMPLE1
{
t_TES tes;
};
Then you just nest the use of the dot-operator . (like you do now):
SAMPLE1 sss;
sss.tes.dTes = 0.0;
You can't. With struct TES;, you are not declaring a member variable. Try e.g. TES member_name, then you can access it with sss.member_name in your main. Also, you should try to use more descriptive variable names ;-)

C++ struct and array concept (-Wmissing-braces])

I am trying to do something like this..I have 3 header files
1. coordinates.h
typedef struct {
float x;
float y;
float z;
}coordinates3D;
2.plane.h
#include "coordinates.h"
typedef struct{
coordinates3D plane;
} plane3D;
3.pointArray.h
#include "plane.h"
plane3D points[] ={
{1.0f,3.74f,0.2354f},
{6.823f,9.234f,1.097f},
};
and a cpp file
4. main.cpp
#include "pointArray.h"
int main(int argc, char **argv)
{
std::cout<<points[1].plane.x;
std::cout<<points[0].plane.y;
}
Everything is working fine but I get a warning message
warning: missing braces around initializer for 'coordinates3D'
[-Wmissing-braces]
I am not sure how to solve this warning ...
It's because of the nested structures. Simply add a couple of braces around the values:
plane3D points[] ={
{ { 1.0f,3.74f,0.2354f } },
{ { 6.823f,9.234f,1.097f } },
};
The outermost is for the array, the next pair is for the plane3D structure, and the next is for the coordinates3D structure.