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...
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.
This question already has answers here:
Visual Studio Code c++11 extension warning
(9 answers)
Visual Studio Code: code not running for C++11
(1 answer)
Closed 10 months ago.
I just set up visual studio code and made a very basic c++ program to test it:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
for (const auto &i : vec) {
cout << i << endl;
}
}
I'm not sure if I did anything wrong, but I get the following errors:
test.cpp:7:17: error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
vector<int> vec = {1, 2, 3, 4, 5};
^ ~~~~~~~~~~~~~~~
test.cpp:8:16: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
for (const auto &i : vec) {
^
test.cpp:8:24: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
for (const auto &i : vec) {
^
2 warnings and 1 error generated.
In cpp standard, it says its using c++20. I'm not sure what compiler I'm using, because in settings it looks like it is using clang, but when it runs, the description says:
cd "/Users/(my name)/Dropbox/Mac/vsCodeProjects/c:c++/" && g++ test.cpp -o test && "/Users/(my name)/Dropbox/Mac/vsCodeProjects/c:c++/"test
Because it says g++ there I'm not sure if it using clang after all, but I can't check the version of g++ because when I do it returns the clang version. I've tried everything I've seen and fiddled a lot with the tasks.json and the cpp standard json but nothing works. Any help is appreciated.
The code:
#include <vector>
int main()
{
std::vector<int> v1 = {12, 34};
std::vector<int> v2 = {56, 78};
//Doesn't work.
v1.push_back(v2[0]);
//Works.
int i = v2[0];
v1.push_back(i);
return 0;
}
For some reason, the first push_back doesn't work, while the second does. Eclipse gives for that line the error:
Invalid arguments ' Candidates are: void push_back(const int &) void push_back(int &&) '
Could someone explain what is happening there? Thanks!
EDIT:
The code actually compiles fine. For some reason, Eclipse doesn't agree that this is valid code.
If I compile the code with g++ 4.7.3 with
g++ test.cpp --std=c++0x
It compiles correctly and if I try to print v1[2];, I get the correct result.
std::cout << v1[2]; // 56
The Eclipse code analyzer tool (CODAN) may just not be right in this situation.
Rely on the output of a C++ (in this case C++11 compatible) compiler.
I can specify -std=c++0x for compilation with my g++ 4.4, and initializer lists are correct, I may use them (in c++98 I can't) but still get errors when try use auto keyword:
std::list< std::vector<int> > li2;
li2.push_back({1, 2, 3}); //push_back vector
li2.push_back({4, 2, 6}); //again, vector implicitly
for (auto& vv : li2) {
for (auto &i : v)
printf("element: %d\n", 8);
}
so I assume I can't use C++11 functionallities with g++4.4. I have 4.4 because of compatibility with CUDA.
This link shows you the different C++11 features supported by GCC. auto appeared in GCC 4.4.
Your real problem is probably that the ranged-based for loop appeared only in GCC 4.6.
I'm using MoSync IDE to build my C++ code for mobile platform. Initially the C++ code was built separately by Visual Studio 2010 without any problems. But when I used MoSync IDE to rebuild the C++ code, it generated some error message. My C++ code uses STL library like std::pair and std::vector classes. Below is the code that was compiled as error in MoSync IDE. MoSync uses GCC 3.4.6. So I assume this is caused by the GCC compiler.
template<typename T>
vector< pair<T, int> > histogram(const vector<T>& x, int numBins)
{
T maxVal, minVal, range, delta, leftEdge, rightEdge;
int i, dummyIdx;
vector<T>::iterator pt;
vector< pair<T, int> > counts(numBins, make_pair(T(), 0));
vector<T> y(x);
//other code ...
}
The error message is:
error: expected `;' before "pt" (line 6)
This template function calculates histogram given the input vector x and numBins, and it returns "counts" as the pair of (bins, counts). Originally I compiled this C++ code in Visual Studio 2010 without any errors. But GCC in MoSync IDE gave me this error message. So this baffles me a lot why this fails to build in GCC.
vector<T>::iterator is dependent type so you need to use typename:
typename vector<T>::iterator pt;
See Where and why do I have to put the "template" and "typename" keywords?