I have something like this:
union MyBBox3D
{
struct
{
float m_fBox[6];
float m_fCenter[3];
float m_fDiagonalLen;
float m_fNormalizeFactor;
float m_fScaling[3];
};
struct
{
float m_fMin[3];
float m_fMax[3];
float m_fCenter[3];
float m_fDiagonalLen;
float m_fNormalizeFactor;
float m_fScaling[3];
};
struct
{
float m_fMinX, m_fMinY, m_fMinZ;
float m_fMaxX, m_fMaxY, m_fMaxZ;
float m_fCenterX, m_fCenterY, m_fCenterZ;
float m_fDiagonalLen;
float m_fNormalizeFactor;
float m_fScalingX, m_fScalingY, m_fScalingZ;
};
};
It compiles well with vs2008 and intel compiler 12.0, but cant be compiled with gcc4.6.3, it gives the following errors:
In file included from Mesh/MyMeshTool.cpp:17:0:
Mesh/MyMeshTool.h:68:28: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fCenter [3]’
Mesh/MyMeshTool.h:59:28: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fCenter [3]’
Mesh/MyMeshTool.h:69:17: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fDiagonalLen’
Mesh/MyMeshTool.h:60:17: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fDiagonalLen’
Mesh/MyMeshTool.h:70:20: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fNormalizeFactor’
Mesh/MyMeshTool.h:61:20: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fNormalizeFactor’
Mesh/MyMeshTool.h:71:32: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fScaling [3]’
Mesh/MyMeshTool.h:62:32: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fScaling [3]’
Mesh/MyMeshTool.h:78:17: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fDiagonalLen’
Mesh/MyMeshTool.h:60:17: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fDiagonalLen’
Mesh/MyMeshTool.h:79:20: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fNormalizeFactor’
Mesh/MyMeshTool.h:61:20: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fNormalizeFactor’
How can I solve this problem? Thanks in advance!
I think in this case (where the separate structures making up the union share identifier names), you're probably better off not using anonymous structures.
Either that or make the names unique across the entire union.
Anything else is just asking for trouble :-)
Recent GCC do accept unamed fields if you pass the -fms-extensions program option to gcc invocation, but this is an extension do the standard C99 specification.
You could also use preprocessor tricks with care, e.g.
union myunion {
struct {
int fa_u1;
int fb_u1;
} u1;
#define fa u1.fa_u1
#define fb u1.fb_u1
struct {
double fc_u2;
char fd_u2[8];
} u2;
#define fc u2.fc_u2
#define fd u2.fd_u2
}
Then you can code x.fa instead of x.u1.fa_u1 etc. Use preprocessor tricks with care and caution (remember that a #define has full translation unit scope). In practice you'll want the fa, u1, fa_u1 names to be long and unique.
Related
Can anyone please explain how to use and access string in a union inside a structure with the help of unrestricted union?
#include <iostream>
#include <string>
using namespace std;
typedef struct {
int height;
int width;
} Page;
typedef struct {
int test;
union {
Page page;
int intVar;
string stringVar;
} VarUnion;
} VariableDataStruct;
int main()
{
VariableDataStruct structeg;
structeg.VarUnion.stringVar = "Hello";
return 0;
}
Currently getting following errors on compilation:
unionstring2.cc: In function ‘int main()’:
unionstring2.cc:22:24: error: use of deleted function ‘VariableDataStruct::VariableDataStruct()’
VariableDataStruct structeg;
^
unionstring2.cc:11:16: note: ‘VariableDataStruct::VariableDataStruct()’ is implicitly deleted because the default definition would be ill-formed:
typedef struct {
^
unionstring2.cc:11:16: error: use of deleted function ‘VariableDataStruct::::()’
unionstring2.cc:13:19: note: ‘VariableDataStruct::::()’ is implicitly deleted because the default definition would be ill-formed:
union {
^
unionstring2.cc:16:11: error: union member ‘VariableDataStruct::::stringVar’ with non-trivial ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]’
string stringVar;
^
unionstring2.cc:11:16: error: use of deleted function ‘VariableDataStruct::::~()’
typedef struct {
^
unionstring2.cc:13:19: note: ‘VariableDataStruct::::~()’ is implicitly deleted because the default definition would be ill-formed:
union {
^
unionstring2.cc:16:11: error: union member ‘VariableDataStruct::::stringVar’ with non-trivial ‘std::basic_string<_CharT, _Traits, _Alloc>::~basic_string() [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]’
string stringVar;
^
unionstring2.cc:22:24: error: use of deleted function ‘VariableDataStruct::~VariableDataStruct()’
VariableDataStruct structeg;
^
unionstring2.cc:18:11: note: ‘VariableDataStruct::~VariableDataStruct()’ is implicitly deleted because the default definition would be ill-formed:
} VariableDataStruct;
^
unionstring2.cc:18:11: error: use of deleted function ‘VariableDataStruct::::~()’
The error you're getting is not about accessing union, it's about not being able to instantiate your struct:
error: use of deleted function ‘VariableDataStruct::VariableDataStruct()’
You need to provide a constructor for your struct that sensibly initializes the union.
Unions with members with non-trivial special member functions (constructor, assignment, destructors) (such as std::string) must define these special functions as well. Since this union does not provide the designation which member is currently in use, those special member functions cannot be defined.
Use std::variant<Page, int, std::string> instead.
I am keeping track if instances of my class using std::vector to store pointers to all of the class objects. I'm wrapping things up and want to remove the pointer in the destructor... but I am getting the following error:
Brazos.cpp:15:89: error: cannot convert 'std::vector::iterator {aka __gnu_cxx::__normal_iterator >}' to 'const char*' for argument '1' to 'int remove(const char*)'
instanceAddress.erase(std::remove(instanceAddress.begin(), instanceAddress.end(), this) instanceAddress.end());
it seems I may need to dereference the iterator... Here is my code:
std::vector<Brazos*> Brazos::instanceAddress;
Brazos::Brazos(Mano mano)
{
instanceAddress.push_back(this);
_mano = mano;
}
Brazos::~Brazos(void)
{
instanceAddress.erase(std::remove(instanceAddress.begin(), instanceAddress.end(), this) instanceAddress.end());
}
You're missing a comma:
instanceAddress.erase(std::remove(instanceAddress.begin(), instanceAddress.end(), this), instanceAddress.end());
^
Also, the error message refers to int std::remove(const char*), so make sure you have #include <algorithm> for the correct std::remove.
On compiling the following C program, GCC emits a warning message which is somewhat confusing.
Program Source
#include <stdio.h>
typedef struct {
int x;
} dummy_t;
void myfunc (dummy_t *pointer)
{
printf("x = %d\n", pointer->x);
}
int main ()
{
dummy_t d = { 10 };
/* INCORRECT. */
myfunc((struct dummy_t *)&d);
/* Correct. */
// myfunc((dummy_t *)&d);
return 0;
}
Compilation
bash$ gcc c.c
c.c: In function ‘main’:
c.c:17:20: warning: passing argument 1 of ‘myfunc’ from incompatible pointer type
myfunc((struct dummy_t *)&d);
^
c.c:7:6: note: expected ‘struct dummy_t *’ but argument is of type ‘struct dummy_t *’
void myfunc (dummy_t *pointer)
Notice how both the expected type and the argument type are reported to have the same value struct dummy_t *. This is confusing.
Shouldn't the expected type be dummy_t *?
The above program is a simplified version of the actual code where I faced this problem.
GCC Version
bash$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
You're right that the error message is confusing. A newer version gives a much better error message:
note: expected 'dummy_t * {aka struct <anonymous> *}' but argument is
of type 'struct dummy_t *'
As you can see, dummy_t and struct dummy_t are different types. With this declaration:
typedef struct {
int x;
} dummy_t;
You are typedef'ing an anonymous struct. However, later when you do struct dummy_t, you are forward declaring a new struct named dummy_t. Clearly, these are two different types, hence the error.
While looking at Thread and interfaces C++, I noticed something a little strange with my Clang.
I have c++ --version output of
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix
Compiling the following
#include <thread>
class Foo {
public:
void operator()() { }
};
int main() {
Foo *foo = new Foo();
std::thread t(foo);
t.join();
delete foo;
}
with c++ thread.cpp yields the following sensible error:
In file included from thread.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:369:5: error: called object type 'Foo *' is not a function or
function pointer
(*__p)();
^~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:377:42: note: in instantiation of function template
specialization 'std::__1::__thread_proxy<Foo *>' requested here
int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Fp>, __p.get());
^
thread.cpp:10:17: note: in instantiation of function template specialization 'std::__1::thread::thread<Foo *>' requested here
std::thread t(foo);
^
1 error generated.
Total sense - Foo * isn't a function pointer.
But, compiling it with c++ -std=c++11 thread.cpp gives this cryptic error:
In file included from thread.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:332:5: error: attempt to use a deleted function
__invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:342:5: note: in instantiation of function template
specialization 'std::__1::__thread_execute<Foo *>' requested here
__thread_execute(*__p, _Index());
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:354:42: note: in instantiation of function template
specialization 'std::__1::__thread_proxy<std::__1::tuple<Foo *> >' requested here
int __ec = pthread_create(&__t_, 0, &__thread_proxy<_Gp>, __p.get());
^
thread.cpp:10:17: note: in instantiation of function template specialization 'std::__1::thread::thread<Foo *&, void>' requested here
std::thread t(foo);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:1027:5: note: '~__nat' has been explicitly marked
deleted here
~__nat() = delete;
^
1 error generated.
What's causing this weird error message about a deleted destructor? Should I consider it a bug in Clang to have such an odd message?
Your actual error message is the same in the two cases: whatever template is used to implement std::thread, it cannot be specialized for Foo*.
~__nat() = delete; is only a random difference between the old and new standard, one of the uninteresting things that fail because of the type error.
I'm using gcc.
I want to create a queue of my own datatype.
In the following code, when I declare struct outside main(), it works fine but it gives compile-time errors when that struct is defined inside.
#include <queue>
using namespace std;
int main()
{
struct tempPos {int a; int b;}; //....(1)
queue<tempPos> b; //works only if tempPos is defined outside main
queue<int> x; //works fine anyways
return 0;
}
Following are the errors.
test.cpp: In function ‘int main()’:
test.cpp:10:15: error: template argument for ‘template<class _Tp> class std::allocator’ uses local type ‘main()::tempPos’
test.cpp:10:15: error: trying to instantiate ‘template<class _Tp> class std::allocator’
test.cpp:10:15: error: template argument 2 is invalid
test.cpp:10:18: error: invalid type in declaration before ‘;’ token
Compilation failed.
C++ forbids using locally-defined classes with templates because they have no linkage. The standard says:
14.3.1/2: .A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.