Compilation error for union - c++

can somebody please explain why the following program causing the compilation problem. I have compiled the source code over VS2013.
#include <iostream>
using namespace std;
// Do not work
union myuni
{
string str;
};
void main()
{
}
Does union require the fixed length size while declaring it? The same scenario works fine with structure.

You cannot have a string in a union as the former contains a constructor.
(Although allowed in C++11 this is not supported in VS2013).

Related

Implicit “using namespace std” without writing it in the source code

I have a C++ code that uses endl, vector, complex, and cout occasionally without any namespace qualification. When compiling with GCC 4.9, I get errors like this one:
bfm.h:454:4: error: 'complex' does not name a type
complex<double> inner(Fermion_t x,Fermion_t y);
^
In the very same file, there are lines with an std:: qualifier:
std::complex<Float> dot(Fermion_t x_t, Fermion_t y_t,int sx,int sy);
Throughout the codebase, I saw the following template specializations used
std::complex<double>
std::complex<cFloat>
complex<Float>
complex<double>
complex<T>
complex<S>.
Neither for the regular express struct\s+complex nor class\s+complex I managed to find something in the codebase or the codebase of the base library. Therefore I assume that it indeed is the standard library complex type.
There are a couple of using namespace std scattered around in various header files, but not all of them.
I tried compiling this codebase with GCC 4.9 and Clang 3.7. Both give similar errors about the missing type. Is there some possibility that this has worked with an older version of GCC? I have tried to insert std:: at all the needed points but I have the impression that I do something wrong if I cannot just compile the source checkout that is meant for this type of computer.
you can use selective using... declarations or type aliasing to bring in only the std:: members you need. Like:
using std::complex;
using std::cout;
using std::endl;
cout << "Hello world" << endl; // works
complex<float> x; // works
fstream y; // compile error, no namespace qualification, no using declaration
std::fstream z; // OK
Ah, yes, maybe not that evident, so perhaps worth mentioning. You now that std::endl? Well, it's a stream manipulator, a function in fact.
Which means one can bring in the current block function members from other namespaces as well.
Like:
#include <cmath>
#include <cstdlib>
inline void dummy(float x) {
float y=fabs(x); // Na'a.., fabs is imported into std namespace by <cmath>
using std::itoa; // same is itoa, but we'll be using it
char buff[128];
itoa(42, buff, 10); // OK
}

Eclipse CDT can't resolve a structs function when accessed from a std::map

The situation is as follows: I declare a struct in a header file, and in the accompanying source file I put instances of this struct in a std::map<int, myStruct>.
The code compiles and runs fine, but Eclipse does'nt recognize the function call and keeps it underlined red (can not open declaration either).
Example code:
//myClass.h
struct myStruct{
int returnValue(){
return 4;
}
};
// other class parameters here
and in the source
//myClass.cpp
#include "myClass.h"
#include <iostream>
#include <map>
using std::map;
int main(){
//create map with struct and assign
map<int, myStruct> myMap;
myStruct exampleStruct;
myMap[3]=exampleStruct;
//access struct via [] map operator
std::cout << myMap[3].returnValue() << "\n"; //this line flagged by Eclipse, but works
return 0;
}
Is this behavior of Eclipse known, or is my config wrong?
Also, on a side note, I am not a professional progammer, so style advice etc is very welcome :)
EDIT: fixed typos in myClass.h and myClass.cpp
After fixing typos, the code is fine and is accepted without even a warning. So I only imagine that it could be a problem in Eclipse configuration.
Okay, this is embarrassing. I ran the sample code through eclipse (the real example is very long and depends on some custom libraries) and Eclipse had no issue with the function of the struct inside the map.
So I rebuilt the index of the real project, and now everything is fine. Strange, because Eclipse always seemed to update the index when I created a new class, but not this time... so thanks to all who read and answered, but the problem is solved already.

C++ std::array don't work on my local compiler

I follow this example array::size, but it's don't work on my Dev C++ IDE, and I don't know why.
I tried to run on Code Blocks but it's still the same.
I think code is correct but how to fix this problem
here is my code
#include <iostream>
#include <array>
using namespace std;
void useArray()
{
array<int,4> myInts;
myInts[0]=1;
cout<<"Size of Array: "<<myInts.size();
}
int main()
{
useArray();
system("pause");
}
And this is compiler error:
std::array is C++11 only. Dev-C++ does not support C++11.
If you're just looking for options to Visual Studio this article is the right starting place: http://www.cplusplus.com/articles/36vU7k9E/
In the case that you're using a compiler that does not support C++11 (using the flag: -std=c++11) you always have the chance to use a Boost alternative.
Boost comes with the Boost Array Library which is an STL conform Array. The generated code will be similar to a C Array while having the opportinity to use additionnal methods on it e.g determinate the array size. All this basically works as the C++11 version of an Array.
It lives in the 'boost' namespace instead of 'std' therefore you need to include the following header to use it:
#include <boost/array.hpp>
You should then set a typedef declaration for the Boost Array:
typedef boost::array<std::string, 3> array;
// The 3 stands for the number of elements the array can hold
Implementing one of these Arrays is then fairly simple:
array a;

Using template functions in Visual Studio C++ 6.0

0Dear StackExchange Community,
I have for two hours tried to find the source of the problem but failed completly. Research=google search also did not provide any viable solutions. At least I was able to discover that under VS 6.0 one cannot split the declaration and implementation of a template function between header and .cpp-file.
Perhaps my approach is inherently flawed or it is VS 6.0 that is being particulary obnoxious this time.
Here is the test code I wrote.
#include "stdafx.h"
#include <string>
#include <iostream>
class TestClass{
public:
template<class T> inline bool isNull(T& inObject){
return 0; // edited because of the answer by Joachim Pileborg :)
// initial code was: return (inObject != NULL) ? 0:1;
}
};
using namespace std;
int main(int argc, char* argv[])
{
cout<<TestClass::isNull<string>("test");
return 0;
}
Running this code causes the following error:
fatal error C1001: INTERNER COMPILER- FEHLER
(Compiler-File "msc1.cpp", Row 1794)
Does anybody have an idea what I am doing wrong here?
P.S. : this time i really endevoured to ask the question as precisly as possible also providing a concrete example. Please let me know if there is anything else I should have added.
P.SS: I know that visual studio 6.0 ist pretty old but I am forced to use it at work. Running the same code with the a new compiler (at home) did not cause any errors. This is why I assume that the problem is mainly caused by the whims of VS 6.0.
Thanks in advance for you help !!
JD
Unless you define a custom casting operator that returns a pointer, an object instance can never be equal to NULL.
Apart from facts noted in comments and answers, internal compiler error happens in situation, when there's a bug in the compiler, that prevents it from compiling valid code.
Microsoft usually fixes these bugs in IDE hotfixes or in newer versions of compilers. Try to modify the structure of code such that it does the same thing, but looks differently - it's the only way to avoid the Internal Error problem.
There are several troubles in your code:
I rewrote it this way:
comparing the adress of the reference you're passing (you have edited your question but you wrote inObject==NULL in the body of your function and it couldn't compile either)
using const string& so has to be able to call TestClass::isNull<string>("test");
you must define your function as static if you want to call it the way you do
I'm not sure but the character '<' following your word template looked badly encoded in my IDE, so I replaced it with a commonplace <, it compiled better
it's a way of coding, but prefer using typename than class when defining templates
prefer using true and false instead of 1 and 0 ( you edited your question but you still return 0...)
=>
#include <string>
#include <iostream>
class TestClass{
public:
template<typename T>
static bool isNull(const T& inObject)
{
return (&inObject == NULL) ? true : false;
}
};
using namespace std;
int main(int argc, char* argv[])
{
cout<< TestClass::isNull<string>("test");
return 0;
}
Now it compiles fine.

Why a variable length array compiles in this c++ program?

It is said that arrays are allocated at compile time, then the size must be const and available at compile time.
But the following example also works, Why?
#include <iostream>
#include <vector>
using namespace::std;
int main()
{
vector<int> ivec;
int k;
while(cin>>k)
ivec.push_back(k);
int iarr[ivec.size()];
for (size_t k=0;k<ivec.size();k++)
{
iarr[k]=ivec[k];
cout<<iarr[k]<<endl;
}
return 0;
}
Compile your code with -pedantic.
Most compilers support variable length arrays through compiler extensions.
The code works due to the compiler extensions, However as you noted the code is non standard conforming and hence non portable.