I'm initialising vector using the below method
vector<int> num1{1, 9, 3, 7, 0, 7, 7, 2, 1};
and it is throwing error as below,
Multiplication.cpp:29:18: error: expected ';' at end of declaration
vector<int> num1{1, 9, 3, 7, 0, 7, 7, 2, 1};
^
You haven't included the vector library,
just add
#include<vector>
at the top of your code
it is running check here
if you did that too, then too,
it is a compiler problem, try using a different complier or online complier like ideone
Related
I'm having a strange issue, and being new to c++ isn't helping. I wrote the following to make a simple 2D matrix:
#include <iostream>
#include <vector>
std::vector< std::vector<int> > grid(){
std::vector< std::vector<int> > vect;
vect = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
return vect;
}
When I run this on a site like cpp.sh, it works perfectly fine, but when I use g++ on my macbook, I get the following error:
username % g++ main.cpp
main.cpp:6:13: error: expected expression
vect = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
^
1 error generated.
I've updated my gcc, and I've run with both g++ and gcc – both have the same issue. I installed gcc with homebrew and i've just been running g++ main.cpp and then executing a.out.
Note: I'm on MacOS Monterey on M1
Thanks for the comments! For anyone else having similar issues:
On macOS Monterey gcc/g++ (by default) uses the 1998 version, where my code requires the 2011+ versions.
To compile with the newer version, #wlk showed that I could use clang++ -std=c++11 to specify the version.
By adding alias g++="clang++ -std=c++20" to my .zshrc file, I can run g++ with the 2020 version – without issue.
Does the following C++ program contain any undefined behavior?
int
main()
{
struct entry
{
uint32_t hash;
uint32_t idx;
};
entry arr[31] = {
{ 7978558, 0}, { 9241630, 1}, { 65706826, 2},
{ 639636154, 3}, {1033996244, 4}, {1225598536, 5},
{1231940272, 6}, {1252372402, 7}, {2019146042, 8},
{1520971906, 9}, {1532931792, 10}, {1818609302, 11},
{1971583702, 12}, {2116478830, 13}, { 883396844, 14},
{1942092984, 15}, {1274626222, 16}, { 333950222, 17},
{1265547464, 18}, { 965867746, 19}, {1471376532, 20},
{ 398997278, 21}, {1414926784, 22}, {1831587680, 23},
{ 813761492, 24}, { 138146428, 25}, { 337412092, 26},
{ 329155246, 27}, { 21320082, 28}, {1751867558, 29},
{1155173784, 30},
};
std::sort(std::begin(arr), std::end(arr),
[](entry a, entry b) { return a.hash <= b.hash; });
}
When I compile with gnu c++ compiler or any clang/llvm after 12.0.0, the program works fine. However, when I compiled it with clang version 12.0.0 (the default compiler shipped on my Mac laptop), it crashed inside of std::sort() as following:
$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.2)
Target: x86_64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
$ g++ -g -std=c++11 bug.cc
$ ./a.out
Segmentation fault: 11
Also if I change it to use std::vector instead of fixed size array. The std::sort() will never return when compiled with clang 12.0.0
Yes, the comparator is not a strict weak ordering which violates the preconditions of std::sort, resulting in undefined behavior.
For two arguments a and b (possibly identical), a strict weak ordering comp should never evaluate both comp(a,b) and comp(b,a) to true. In other words, it should model the behavior of the built-in <, not <=.
So in your code it should be <, not <=, to make it a strict weak ordering.
This question already has answers here:
Compiling C++11 with g++
(6 answers)
Closed 2 years ago.
When I initialize a vector in C++ like this:
#include <string>
#include <vector>
using std::vector;
using std::string;
int main() {
vector<int> v1 = {1, 2, 3, 4};
return 0;
}
I got this error:
prog1.cpp:8:15: error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
vector<int> v1 = {1, 2, 3, 4};
^ ~~~~~~~~~~~~
1 error generated.
Any idea why this happens?
Initializer lists were introduced from C++ 11 onwards, so you'll need to specify that (or a higher) version while compiling (-std=version) if your using an older standard for default compilation.
I just came across a weird problem that happens ONLY on MSVC with Clion but not on other compilers(I tried gcc on Linux and Visual Studio both no such problem, with the same code).
With these codes:
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int>v = {1,2,3,4,5};
make_heap(v.begin(), v.end());
v.push_back(6);
push_heap(v.begin(), v.end());
}
an error "In instantiation of function template specialization 'std::push_heapstd::_Vector_iterator<std::_Vector_val<std::_Simple_types<int > > >' no type named 'value_type' in 'std::indirectly_readable_traitsstd::_Vector_iterator<std::_Vector_val<std::_Simple_types<int > > >'" will then be shown
is it a bug of Clion or MSVC?
P.S.
I can still build and run it so it might not be a compiler error; (Making me even more confused)
It looks like you cannot intialize vector with the following command:
vector<int>v = {1,2,3,4,5};
Change it to:
vector<int> vect{ 1, 2, 3, 4, 5 };
Compile and run the code and see if it still has problems.
EDIT:
Some people are saying it is unlikely however look at the link:
What is the easiest way to initialize a std::vector with hardcoded elements?
If you scroll down to the second answer it says:
If your compiler supports C++11, you can simply do:
std::vector<int> v = {1, 2, 3, 4};
As you did not tell us your compiler version and environment it is very hard to determine if this is the problem. Also note that:
This is available in GCC as of version 4.4.
Unfortunately, VC++ 2010 seems to be lagging behind in this respect.
So if you are using an older version of VC++ then you are out of luck...
Undefined symbols for architecture x86_64:
"standardDeviation(double*)", referenced from: _main in main.o
ld: symbol(s) not found for architecture x86_64 clang: error: linker
command failed with exit code 1 (use -v to see invocation)
#include <iostream>
using namespace std;
double standardDeviation(double []);
int main(){
double array[] = {2, 11, 4, 5, 9, 5, 4, 12, 7, 8, 9, 3, 7, 4, 12, 10,9, 6, 9, 4};
standardDeviation(array);
return 0;
}
double standardDeviation(double arrary){
cout << arrary;
return 0.0;
}
Your definition of standardDeviation is looking for a double which you are calling array. You are trying to pass in a array of doubles. The way c++ handles a array is it saves a location pointer to the variable name and when you give it a input (ex array[2]) it moves the space of that many doubles over in memory.
So what the compiler is complaining about is that you told it a double was coming and it got a pointer to a double.
Here's some info on pointers and arrays which should help flesh out what I stated above.
To fix this you should change your prototype to double standardDeviation(double array[]); and then copy that down to your function definition. (As noted by KevinDTrimm above)