I have just configured c/c++ in netbeans on ubuntu
and when i try to use std::pair it seems that the compiler cannot find it
that is very strange
the default version of c++ is c++11
that a slice of my code
int n, k;
cin >> n>>k;
vector<pair<int,int> > x(n);
thanks in advance
You need to include the right header files a the beginning of your source files so that the compiler know the different types/objects:
#include <iostream> // For std::cin
#include <vector> // For std::vector
#include <utility> // For std::pair
And use the std namespace by default if you want (before main() typically):
using namespace std;
Related
Why the following code doesn't compile,
#include <vector>
using std::vector;
vector<int> v; // Error: too few template arguments, expected 2
but the same code with map (and pair, set, ...) instead of vector works?
#include <map>
using std::map;
map<int, int> m; // OK
And also this code works fine:
#include <vector>
using namespace std;
vector<int> v; // OK
I know that constructor of std::vector has two arguments (type and allocator), but why vector behaviour is so different from other containers?
UPD: I'm sorry, this is my mistake. Actually the code does compile, but CLion marks it as an error. So it is CLion's bug.
It is yet not fixed CLion bug: https://youtrack.jetbrains.com/issue/CPP-5758#u=1454575544687.
As a workaround you can try to use libstdc++ instead of libc++, see https://youtrack.jetbrains.com/issue/CPP-5758#comment=27-2389700.
I have been trying to debug this problem for a while and quite honestly, I just can't see what I'm doing wrong.
Why is there a syntax error?
#include <iostream>;
#include <time.h>;
#include <stdio.h>;
#include <stdlib.h>;
using namespace std;
class Problem3 {
public:
bool isPrime(long double num) {
srand(time(NULL));
return 0;
}
};
The error I'm getting is,
"Function 'srand' could not be resolved."
I'm well aware now that I don't need the semi-colons after 'include' statements
I'm using Eclipse CDT along with MinGW as my compiler
How I resolved the problem:
It had to do with the MinGW compiler I was using. Switching over to Visual Studio solved the problem.
; at the end of the #include directives are the problem in your code. #include directives don't need (wrong to place indeed) semicolons at the end unlike C++ statements.
[Warning] extra tokens at end of #include directive [enabled by default]
It seems any character after > in the directive causes this error/warning.
#include<iostream>a //error
Change to this:
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class Problem3 {
public:
bool isPrime(long double num) {
srand(time(NULL));
return 0;
}
};
int main(){
cout<<"Hello Main";
}
EDIT:
Regarding the linker issue:
One suggestion is C++ expects types to be explicitly casted between types (more than C). So, use a cast to convert time_t which is returned by the time to unsigned int which is the input parameter type of srand. (And of course this might not be the problem with linker error)
Instead of using stdlib.h, try using <cstdlib>, try if it helps. Because it uses namespace.
Apart from that, I have seen this snippet here. Use that pattern if it helps.
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(0)); //use current time as seed for random generator
int random_variable = rand();
cout << "Random value on [0 " << RAND_MAX << "]: "
<< random_variable << '\n';
}
there is already question in SO check if that helps Eclipse Method could not be resolved in a simple program C++
Never use time() to initialize srand()..
EDIT:
Now it seems many people got this kind of problem. I found a question How do I fix Eclipse CDT Error “Function 'isdigit' could not be resolved. He is facing the same problem. The asker suggested a work around to this in his question edit.
Quoted from that question:
I now believe this to be a Code Analysis problem. A better solution is
to edit the Code Analysis options to make "Function could not be
resolved" be a warning instead of an error. That way you can see the
warnings in Problems view, but continue to work. If the function is
REALLY missing, the compiler will tell you! I also have a new theory,
that the problem is with the Code Analyzer following symlinks, because
all of the "missing" functions are in symlinked include files. Would
love any input on this theory.
Hope that points to solve the problem.
; should not be there after #include.
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include files shoule not end with ;
I am a newbie or noob if you prefer it that way in C++ programming and I'm trying to use std:: because someone told me that is a good habit rather than putting in using namespace std; because it pollutes the global namespace. I'm not sure why std::cin >> name; from my code below produce an error no operator '>>' matches these operands below is the full source code.
#include "stdafx.h"
#include <ios>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
int x, y;
std::string name;
std::cin >> name;
std::cin >> x;
return 0;
}
You forgot this:
#include<string>
You're using std::string which is defined in the above header. You need to include it.
If you use anything from the Standard Library, whether it is container or algorithm, make sure that you have included the appropriate headers which define them. Standard library has lots of header files, especially for containers. As a general rule, each container is defined in its own header file.
You forgot to
#include <string>
Also
#include <ios>
is not necessary.
I am using Netbeans 7.1 on Ubuntu 11.04.
The following call
set< Triangle > V;
gives the error message
error: ‘set’ was not declared in this scope
and the following call
vector< Triangle > ans;
gives the error message
error: ‘vector’ was not declared in this scope
This despite my having
#include <vector>
#include <set>
#include <map>
at the beginning of the C++ file.
At help resolving this would be greatly appreciated.
Peter.
Vectors Sets and map are part of the c++ Standard Library so you need to call vector/set/map with
std::vector< Triangle > ans;
or add
using namespace std;
after the include statements.
you forgot about namespace std :
std::set< Triangle > V;
std::vector< Triangle > V;
They live in the std namespace. So, either fully quality the types (std::vector) or use a using statement (using namespace std;).
The latter option pollutes the global namespace. Never do that in a header file (otherwise the entire namespace is imported when you include the header) and only do it in your implementation file if you know that it isn't going to cause any collisions.
#include <vector>
int main(...) {
vector v; // no worky
std::vector v; // ok!
}
Like this question already asked, I'd like to initialize a container using STL where the elements are hard-coded in the cleanest manner possible. In this case, the elements are a doubly nested container:
set<vector<int> > A;
And I'd like (for example) to put the following values in:
A = [[0,0,1],[0,1,0],[1,0,0],[0,0,0]];
C++0x fine, using g++ 4.4.1. STL is preferable as I don't use Boost for any other parts of the code (though I wouldn't mind an example with it!).
This does use g++ 4.4.1, with -std=c++0x
#include <set>
#include <vector>
using namespace std;
int main()
{
set<vector<int>> A = {{0,0,1},{0,1,0},{1,0,0},{0,0,0}};
}
#include <boost/assign/list_of.hpp>
#include <vector>
#include <set>
using namespace std;
using namespace boost::assign;
int main()
{
set<vector<int> > A;
A = list_of
(list_of(0)(0)(1))
(list_of(0)(1)(0))
(list_of(1)(0)(0));
(list_of(0)(0)(0));
return 0;
}