C++ compiler throws error on dynamic array with default values - c++

Mac g++ throws a compilation error with this code:
#include <iostream>
using namespace std;
int main(int argc, char ** argv) {
int * p = new int[5] {1,2,3};
return 0;
}
I tried an online compiler and it compiles and runs without errors.
Is there something wrong with mac compiler? Can I do something to change how it works or should I install another c++ compiler?
Edit:
The error:
test.cpp:6:25: error: expected ';' at end of declaration
int * p = new int[5] {1,2,3};
Compiler target and version:
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Compile command:
g++ test.cpp

There's no fundamental reason not to allow a more complicated initializer it's just that C++03 didn't have a grammar construct for it. In the next version of C++, you will be able to do something like this.
int* p = new int[5] {0, 1, 2, 3, 4};
You can try adding -std=c++11 to your command line and that should be working all fine.

Related

Compiler, library, or user error? Eigen::Array, GCC 12.1, "array subscript [...] is partly outside array bounds"

After updating to GCC 12.1, I got a array subscript ‘__m256d_u[0]’ is partly outside array bounds error (or rather warning with -Werror) in my project, so I tried isolating the problem.
Here's an MWE, which I also put on godbolt (vector type is __m512d_u instead, but otherwise it's the same error):
#include <Eigen/Dense>
#include <iostream>
using Eigen::Array;
Array<double, 3, 2> foo(){
Array<double, 2, 2> a;
a.setRandom();
Array<double, 3, 2> b;
b.col(0).tail(2) = a.col(1);
// b.col(0).template tail<2>() = a.col(1);
return b;
}
int main(){
std::cout << foo() << '\n';
return 0;
}
Relevant compile options are -Wall -Wextra -Werror -O3 -march=native, and the error message notes note: at offset [16, 24] into object ‘a’ of size 32.
The error does not occur under the following circumstances:
on GCC 11.3 or older,
when removing -march=native
when using -O1 or below
when replacing the line b.col(0).tail(2) = a.col(1); with b.col(0).template tail<2>() = a.col(1);
So it looks like GCC sees the 3x2 array and the 2x2 array, and doesn't realise that only two entries are accessed each.
My question now is: Who should this be reported to? GCC, Eigen? Or is it a user bug?
Bonus points for telling me what the 24 in the error note (offset [16, 24]) is. The 16 is the start, is the 24 the read size?
EDIT: Example can be further simplified by using Array3d and Array2d, see here.

Compiler Error on std::sort function (GCC 4.4)

The code below sorting in Visual Studio successfully.
But, in Ubuntu GCC 4.4.7 compiler throws error. It seems it's not familiar with this type of syntax.
How shall I fix this line to make code working in GCC? (the compiler is remote. So I can't upgrade the GCC version either).
What I'm doing right here is:sorting Vector B elements regarding their fitness values
//B is a Vector of class Bird
//fitness is a double - member of Bird objects
vector<Bird> Clone = B;
sort(Clone.begin(), Clone.end(), [](Bird a, Bird b) { return a.fitness> b.fitness; });
//error: expected primary expresssion before '[' token
//error: expected primary expresssion before ']' token...
(Note: this 3 piece of lines compiling successful in MSVC but not in GCC)
my answer is
bool X_less(Bird a, Bird b) { return a.fitness > b.fitness; }
std::sort(Clone.begin(), Clone.end(), &X_less);
It seems to work. Is it a function or not? I don't know its technical name, but it seems to work. I am not much familiar with C++.
You will need to upgrade your C++ as 4.4 is too old to support Lambda. I have Gcc 4.8, but it still requires you enable c++11 that includes lambda functions, so
$ g++ -std=c++11 x.cc
compiles this fine
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
int main()
{
vector<int> Clone;
sort(Clone.begin(), Clone.end(), [](int a, int b) { return a> b; });
}
but still gives errors without -std=c++11 option

Why are std::stoi and std::array not compiling with g++ c++11?

I've been learning C++ and using the Terminal for the last couple of months. My code was compiling and running fine using g++ and C++11, but in the last couple of days it started giving errors and I have had problems compiling since. The only programs I can compile and run depend on older C++ standards.
The errors I first got related to #include < array > in the header file. Not sure why this happened, but I got around it by using boost/array instead. Another error I can't solve is with std::stoi. Both array and stoi should be in the C++11 standard library. I made the following simple code to demonstrate what's going on:
//
// stoi_test.cpp
//
// Created by ecg
//
#include <iostream>
#include <string> // stoi should be in here
int main() {
std::string test = "12345";
int myint = std::stoi(test); // using stoi, specifying in standard library
std::cout << myint << '\n'; // printing the integer
return(0);
}
Try to compile using ecg$ g++ -o stoi_trial stoi_trial.cpp -std=c++11
array.cpp:13:22: error: no member named 'stoi' in namespace 'std'; did you mean
'atoi'?
int myint = std::stoi(test);
~~~~~^~~~
atoi
/usr/include/stdlib.h:149:6: note: 'atoi' declared here
int atoi(const char *);
^
array.cpp:13:27: error: no viable conversion from 'std::string' (aka
'basic_string') to 'const char *'
int myint = std::stoi(test);
^~~~
/usr/include/stdlib.h:149:23: note: passing argument to parameter here
int atoi(const char *);
^
2 errors generated.
I also get these errors at compilation when using gcc or clang++ and with -std=gnu++11 (I guess they all depend on the same file structure). I also get the same error whether I specify std:: in the code, or if I specify using namespace std;
I worry that these issues arose because of the September Command Line Tools update via Xcode or because I installed boost and this somehow messed up my C++11 libraries. Hopefully there is a simple solution.
My system:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-> dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
Thanks for any insight you can offer.
clang has a weird stdlib, you need to add the following flag when you compile
-stdlib=libc++
your snippet works on my mac with
g++ -std=gnu++11 -stdlib=libc++ test.cpp -o test
This answer describes the problem

Simple program won't compile with qtcreator

I installed qt 4.7.4 and gcc 4.6.1. I tried to compile this program but it won't compile for me:
Why cannot I compile this code?
#include <QApplication>
#include <iostream>
using std::cout;
int main(int argc, char** argv)
{
QApplication app(argc,argv);
int a[] = {1,2};
for (auto e : a)
{
cout << e << '\n';
}
return app.exec();
}
Error:
C:...\main.cpp:9: error: 'e' does not name a type
for (auto e : a)
is a range based for loop from the c++11 standard. You need to enable the c++11 in gcc with the -std=c++0x command line.
For me this works (g++ 4.6.1, Qt 4.7.1):
g++ --std=c++0x -I$QTDIR/include/QtGui -I$QTDIR/include \
test.cpp -L$QTDIR/lib -lQtCore -lQtGui
You need --std=c++0x compiler flag.
My guess is that qtcreator (and qmake) does not feed the compiler with the flag instructing it to use C++2011.
First, you want to be sure that your C++ file is compiled with the C++11 dialect (ie using -std=c++0x flag to g++) since you use the auto type inference feature.
Then, I think that your for loop might not be valid. Perhaps you want a to be a std::vector<int>

Error with variable array size using the visual C++ compiler (Visual Studio 2010). How to circumvent this issue?

I am experiencing some troubles compiling a c++ file that worked well as a previous build under GCC.
The issue is, I am using vectors of variable array size:
unsigned int howmany;
std::vector<int>* array_adresses[howmany];
I am currently using the Visual-Studio 2010 C++ compiler to built Matlab 64-bit Mex-Files.
Since VC++ won't allow me to use arrays whose size is unknown at compile time, I am receiving the following error messages:
error 2057: constant expression expected
error 2466:
error 2133: unknown size
Is there any way to build the 64 bit mex file using a GCC-compiler option or build it with a different 64-bit compiler under Matlab?
Thanks in advance!!
howmany needs to be constant, and needs to be a defined amount, like so:
const unsigned int howmany = 5;
std::vector<int>* array_adresses[howmany];
Or you can define it dynamically like this:
unsigned int howmany = 5;
std::vector<int>* array_adresses = new std::vector<int>[howmany];
C++ standard doesn't allow variable-length arrays. Lets take this code:
int main(int argc, char *argv[])
{
int a[argc];
return 0;
}
This compiles fine with g++ foo.cpp, but fails if you require a strict standard compliance.
g++ foo.cpp -std=c++98 -pedantic:
foo.cpp: In function ‘int main(int, char**)’:
foo.cpp:8: warning: ISO C++ forbids variable length array ‘a’
You should use vector<vector<int> *> or vector<int> ** instead as others already suggested.
Simply replace
int ptr[howmany];
with
vector<int> ptr(howmany);
to obtain also automatic deallocation at the end of the scope.