c++ Error with literal initialize pointer with curly brace [duplicate] - c++

This question already has answers here:
How do I activate C++ 11 in CMake?
(17 answers)
Closed 4 years ago.
I'm learning new of c++.
In my text book, there is very simple example this.
int* ip = nullptr; // (ok)
int* ip1{}; // (error)
int* ip2{nullptr}; // (error)
unique_ptr<double> dp{new double}; // (error)
But I got this error with build it.
/Users/bsdo/projects/CPlus/modernbook/include/basis.cpp:81:30: error: expected ';' at end of declaration
unique_ptr<double> dp{new double};
^
;
In file included from /Users/bsdo/projects/CPlus/modernbook/main.cpp:2:
/Users/bsdo/projects/CPlus/modernbook/include/basis.cpp:81:30: error: expected ';' at end of declaration
unique_ptr<double> dp{new double};
^
;
/Users/bsdo/projects/CPlus/modernbook/main.cpp:10:13: error: expected ';' at end of declaration
int* ip1{nullptr};
^
;
Why only curly brace literal throw error??
My env is Mac book pro 2015, Cmake-3.11, Apple LLVM version 9.1.0 (clang-902.0.39.1).

Just make cmake compile c++11..
Configuration is write this line in CmakeLists.txt
set (CMAKE_CXX_STANDARD 11)

Related

Error with declaring variables with {} in C++

I'm using C++. I tried to declare a variable like this:
int my_num {5};
I got an error in the logs:
"error: expected ';' at end of declaration"
I get the same error when I do this:
int my_arr [] {1,2,3,4};
I am new to C++ (normally work with JS). Any idea what's going on here?
c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)

Why is g++ not able to compile tuples? [duplicate]

This question already has answers here:
error: ‘i’ does not name a type with auto [duplicate]
(2 answers)
Closed 6 years ago.
#include <iostream>
#include <tuple>
#include <string>
using namespace std;
int main(){
tuple<string, string, string> x;
x = make_tuple("hi", "a", "b");
cout << get<0>(x) << endl << endl;
}
I've been having difficulties with my program, so I wrote a simpler one and even this does not work. I do not understand why there is a problem after reviewing the documentation several times. It also compiles fine on XCode but for some reason breaks down on g++.
Here is the full error message:
test.cpp:6:3: error: use of undeclared identifier 'tuple'
tuple x;
^
test.cpp:6:9: error: unexpected type name 'string': expected
expression
tuple x;
^
test.cpp:7:3: error: use of undeclared identifier 'x'
x = make_tuple("hi", "a", "b");
^
test.cpp:7:7: error: use of undeclared identifier 'make_tuple'
x = make_tuple("hi", "a", "b");
^
test.cpp:8:11: error: reference to overloaded function could not be
resolved; did you mean to call it? cout << get<0>x << endl << endl;
The command I am using is g++ test.cpp
Try #include <string>.
Possibly (depending on your version og gcc) you also need -std=c++11 on the command line.
The tuple is fine; what you're trying to make it a tuple of is not.
You did not #include <string>!
Thus the word "string" means nothing to your compiler, and it has no idea what you want it to do. It can't even tell that you meant it to be a type, so it can't tell that by the word "tuple" you meant "std::tuple". So on, and so forth…

Clang Apple C++11 correct setup for CMake

I am trying to configure CMake to compile for OS X Target.This is IMac with OS X Yosemite v 10.10.2
Clang version:
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.1.0
When I compile without putting any compiler flags I am getting one error for this chunk of code in one the sources:
static const char* LogLevelStr[] {
"TRACE " ,
"INFO " ,
"WARNING" ,
"ERROR " ,
"FATAL " ,
};
error: definition of variable with array type needs an
explicit size or an initializer
I am compiling this code on Windows and GCC and it is completely fine so I don't understand why Clang complains here.So I decided,maybe I have to set C++11 support flags because I use this standard in the code a lot.
Setting
set (CMAKE_CXX_STANDARD 11)
or
set(CMAKE_CXX_FLAGS " -std=c++11")
Adds even more weird errors like these:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:51:52:
error:
expected ';' at end of declaration list
_LIBCPP_INLINE_VISIBILITY operator bool() const _NOEXCEPT
^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:87:57:
error:
expected ';' at end of declaration swap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT
^ >/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolc>hain/usr/bin/../include/c++/v1/__bit_reference:87:58:
error:
C++ requires a type specifier for all declarations >swap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT
^ >/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__config:338:21:
note:
expanded from macro '_NOEXCEPT'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:89:10:
error:
expected '(' for function-style cast or type construction
bool __t = __x;
~~~~ ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:92:2:
error:
expected ';' after top level declarator } ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:1111:47:
error:
expected ';' at end of declaration list
_LIBCPP_INLINE_VISIBILITY __bit_iterator() _NOEXCEPT
The error block from above the compiler spits at the point it is trying to parse include
Now,I tried also to set:
set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
Same errors.
What do I miss here?
UPDATE:
I don't understand why some people marked this question for closing.Anyway,here is the problem in more details.I tried all those C++11 flags.I also added '=' to that static array.But most of the errors come after that.And it looks like root of those is .At the very first place where gets parsed it goes down into another class called __bit_reference and there at line 51 the compiler complains
on the following line
_LIBCPP_INLINE_VISIBILITY operator bool() const _NOEXCEPT
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__bit_reference:51:52:
Expected ';' at end of declaration list
Most of the other errors are also in some ways connected to stl containers.
So my question is still valid.How do I get my source code to compile with the latest Clang on OS X including C++11 support.I am trying to do that with Xcode and have the same issues.
Xcode compiler output(some of it):
CompileC
/Users/michaeliv/Library/Developer/Xcode/DerivedData/xxxxxTest-hdkkzwyyppywsjgmoyuphranqtok/Build/Intermediates/xxxxxxTest.build/Debug/xxxxxxTest.build/Objects-normal/x86_64/XXXMath.o
/Users/XXXXXXX/Documents/XXXXX/xxxxxx/src/XXXMath.cpp normal x86_64
c++ com.apple.compilers.llvm.clang.1_0.compiler
cd /Users/xxxxxx/Desktop/xxxxTest
export LANG=en_US.US-ASCII
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-x c++ -arch x86_64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=gnu++11 -stdlib=libc++ ....
It's difficult to say for sure since you aren't posting a reproducible problem.
However, the code sample you posted contains a flaw:
static const char* LogLevelStr[] {
"TRACE " ,
"INFO " ,
"WARNING" ,
"ERROR " ,
"FATAL " ,
};
should be changed to
static const char* LogLevelStr[] = {
"TRACE " ,
"INFO " ,
"WARNING" ,
"ERROR " ,
"FATAL " ,
};
I'm not 100% on this as regards the C++11 standard, however, this is my understanding.
When you write static const char* foo[] = { "foo", "bar", "baz", }; this is aggregate initialization and not list initialization or any other kind. In your code sample, it looks like you are trying to initialize an array using C++11 list-initialization. However, to my knowledge this is not possible, and the "brace-or-equal" syntax described in chapter 8 of the standard does not apply.
This answer refers to "clause 8" to argue that list-initialization of C-style arrays is not permitted by the standard: Initializing a member array in constructor initializer
IMO that is the problem and if I were you I would use an = there, even if it would be allowed to omit it in C++11 or some future standard.

C++ constexpr error statement showing error [duplicate]

This question already has answers here:
constexpr not compiling in VC2013
(4 answers)
Closed 9 years ago.
Please help me with this error.
I am trying to make a sample that explains constexpr keyword in c++. I am using Visual Studio 2013.
Following is the code of my cpp file.
#include <iostream>
#include <stdexcept>
const int sampleconstant = 5;
constexpr int constTest(void)
{
return sampleconstant;
}
int main()
{
std::cout << constTest();
getchar();
return 0;
}
This shows compile time error as follows:
Error 1 error C2144: syntax error : 'int' should be preceded by ';'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
I may be doing something silly here. But really can't figure out this error. Code looks ok to me. If any one can help me with error please help.
As already told you, it is not supported on VS.
You can see at this link a list of featured supported by the compilers: http://wiki.apache.org/stdcxx/C++0xCompilerSupport

Error C2059: syntax error 'constant' [duplicate]

This question already has answers here:
Why am I getting "error: expected '}'" in C++ but not in C?
(3 answers)
Closed 9 years ago.
I have the following code in a header file:
enum {false,true};
and I have my main function in main.c. if I change the extention to main.cpp
I get the following error:
Error C2059: syntax error 'constant'
Im using visual c++, any Idea why`?
true and false are keywords representing constant values in C++. You cannot use them to name things such as enum values.
As an example, the following would compile
enum { false_, true_ };
int main() {}
false and true are reserve words in C++. You can't redefine it as variable.