Trouble compiling a simple C++0x program with lambdas - c++

I am trying to run a simple lambda example.
// lambda.cpp
#include <functional>
//#include <tr1/functional>
int main()
{
// Assign the same lambda expression to a function object.
function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
//function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
}
I'm compiling it like this:
$ g++ -std=c++0x -fpermissive lamdas.cpp
lambdas.cpp: In function ‘int main()’:
lambdas.cpp:10: error: expected primary-expression before ‘=’ token
lambdas.cpp:10: error: expected primary-expression before ‘[’ token
lambdas.cpp:10: error: expected primary-expression before ‘]’ token
lambdas.cpp:10: error: expected primary-expression before ‘int’
lambdas.cpp:10: error: expected primary-expression before ‘int’
lambdas.cpp:10: error: expected ‘;’ before ‘{’ token
How do I get it to compile with no errors?

Did you mean std::function?
Standard library features live in the std namespace.
It's also interesting that your copy/paste is clearly fake; you wrote "lamdas.cpp" then compiled "lambdas.cpp"!

std::function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
or, probably better
auto f2 = [] (int x, int y) { return x + y; };

It looks to me like you forgot -std=c++0x.

Related

Weird C++ error, unqualified ID, redeclaration

I want to create a project, I have 3 files, a test.cpp, something.h and a something.cpp. here they are:
test.cpp:
#include <bits/stdc++.h>
#define f cin
#define g cout
#include "something.h"
using namespace std;
int main(void)
{
int x;
register(x);
return 0;
}
something.h:
#ifndef __SOMETHING__H_
#define __SOMETHING__H_
#include <bits/stdc++.h>
void register(int x);
#endif
something.cpp:
#include "something.h"
void register(int x)
{
std::cout << x << '\n';
}
And here is the error I get:
In file included from test.cpp:4:0:
something.h:5:15: error: expected unqualified-id before ‘int’
void register(int x);
^~~
something.h:5:15: error: expected ‘)’ before ‘int’
test.cpp: In function ‘int main()’:
test.cpp:10:15: error: ISO C++ forbids declaration of ‘x’ with no type [-fpermissive]
register(x);
^
test.cpp:10:15: error: redeclaration of ‘int x’
test.cpp:9:9: note: ‘int x’ previously declared here
int x;
^
In file included from something.cpp:1:0:
something.h:5:15: error: expected unqualified-id before ‘int’
void register(int x);
^~~
something.h:5:15: error: expected ‘)’ before ‘int’
something.cpp:3:15: error: expected unqualified-id before ‘int’
void register(int x)
^~~
something.cpp:3:15: error: expected ‘)’ before ‘int’
Why does it tell me that I redefine x? When I just want to call register with it's value.
register is a reserved word in C++. Therefore, you have to give another (unreserved) name.
more information: Register keyword in C++ - Stack Overflow

Why am i getting missing template arguments before ‘(’ token while using priority queue?

I was implementing an algorithm using priority queue.
Here is my code
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int first[2]={2,-2};
int second[2]={1,-1};
vector<pair<pair<int,int>,int>>vec;
class compare{
public:
bool operator()(pair<pair<int,int>,int>a,pair<pair<int,int>,int>b)
{
return a.second>b.second;
}
};
int main() {
long long int a,b,c,d;
while(cin>>a>>b>>c>>d)
{
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
map<pair<int,int>,bool>visited;
map<pair<int,int>,int>dist;
map<pair<int,int>,pair<int,int>> parent;
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++)
{
dist[make_pair(i,j)]=INT_MAX;
visited[make_pair(i,j)]=false;
}
dist[make_pair(a,b)] = 0;
visited[make_pair(a,b)] = true;
q.push(make_pair(make_pair(a,b),0));
while(!q.empty())
{
pair<int,int> node = q.top().first;
int distance = q.top().second;
q.pop();
//followed by relaxation step
}
}
// your code goes here
return 0;
}
The problem is I am getting the following errors:
rog.cpp: In function ‘int main()’:
prog.cpp:39:17: error: missing template arguments before ‘(’ token
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
^
prog.cpp:39:41: error: expected primary-expression before ‘,’ token
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
^
prog.cpp:39:73: error: expected primary-expression before ‘,’ token
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
^
prog.cpp:39:73: error: expected primary-expression before ‘)’ token
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
I am not able to understand what exactly is the meaning of the error.It wold be of great help if someone could clarify it for me.
You are not using proper syntax. Instead of writing
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
You should write
priority_queue<pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare> q;
For more information, refer to this link: http://en.cppreference.com/w/cpp/container/priority_queue
Change this:
priority_queue(pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare) q;
to this:
priority_queue<pair<pair<int,int>,int>,vector<pair<pair<int,int>,int>>,compare> q;
and you will get this code compiled.
However, this code is unreadable. Consider using a typedef.

How to fix expected primary-expression?

#include<iostream>
#include<memory>
class test{
public:
void print()
{
std::cout<<"test print"<<std::endl;
}
};
int main
{
std::auto_ptr<test> t1 (new test);
t1->print();
return 0;
}
I am getting following error:
$g++ 5.cpp --std=c++11
5.cpp:16:22: error: expected primary-expression before ‘t1’
std::auto_ptr<test> t1 (new test);
^
5.cpp:16:22: error: expected ‘}’ before ‘t1’
5.cpp:16:22: error: expected ‘,’ or ‘;’ before ‘t1’
5.cpp:17:2: error: ‘t1’ does not name a type
t1->print();
^
5.cpp:19:2: error: expected unqualified-id before ‘return’
return 0;
^
5.cpp:20:1: error: expected declaration before ‘}’ token
}
^
int main // <-- Notice anything ?
the problem is, that you forgot the parantheses in main.
int main { ...} // this is wrong!
but the right would be
int main() { ... }

default parameters in c++

Consider the following:
int foo(int x , int z = 0);
int foo(int x, int y , int z = 0);
If I call this function like so:
foo( 1 , 2);
How does the compiler know which one to use?
It won't and hence this example will not compile cleanly, it will give you an compilation error.
It will give you an ambiguous function call error.
Online Sample:
int foo(int x , int z = 0){return 0;}
int foo(int x, int y , int z = 0){return 10;}
int main()
{
foo( 1 , 2);
return 0;
}
Output:
prog.cpp: In function ‘int main()’:
prog.cpp:6: error: call of overloaded ‘foo(int, int)’ is ambiguous
prog.cpp:1: note: candidates are: int foo(int, int)
prog.cpp:2: note: int foo(int, int, int)
It doesn't, that's why you get a compiler error.
That is a nice question. But it will not compile because of ambigious call to foo(). You can remove this ambiguity by using different datatypes in function signature.
For more detail about default parameter and function overloading see http://www.smart2help.com/e-books/ticpp-2nd-ed-vol-one/Chapter07.html
Compiler will report Ambiguous function overload. As you cannot figure out which function will b called so does the compiler

Defining boost compressed sparse row graph

I know this might be obvious, but bear with me my generic programming background is weak.
This is the code I am trying to compile :
#define BOOST_GRAPH_USE_NEW_CSR_INTERFACE
#include <boost/graph/compressed_sparse_row_graph.hpp>
#include <vector>
typedef unsigned int uint32_t;
typedef unsigned long uint64_t;
using namespace boost;
using namespace std;
typedef compressed_sparse_row_graph<directedS,void,void,no_property,uint32_t,uint64_t> intl_graph;
typedef std::pair<uint32_t,uint32_t> E;
int main()
{
intl_graph g;
vector<E> the_edges;
uint32_t nv = 3;
uint64_t nedge;
the_edges.push_back(E(0,1));
the_edges.push_back(E(1,2));
g = intl_graph(edges_are_unsorted_t,the_edges.begin(),the_edges.end(),nv); //line 24
}
which results in this error :
boost_eg.cpp: In function âint main()â:
boost_eg.cpp:24: error: expected primary-expression before â(â token
boost_eg.cpp:24: error: expected primary-expression before â,â token
If I change line 24 to this :
intl_graph g_(edges_are_unsorted_t,the_edges.begin(),the_edges.end(),nv);
the error is this :
boost_eg.cpp: In function âint main()â:
boost_eg.cpp:24: error: âthe_edgesâ is not a type
boost_eg.cpp:24: error: expected â,â or â...â before â.â token
Any thoughts?
The reference is over here :
http://www.boost.org/doc/libs/1_49_0/libs/graph/doc/compressed_sparse_row.html
You have to replace
g = intl_graph(edges_are_unsorted_t,the_edges.begin(),the_edges.end(),nv); //line 24
by
g = intl_graph(edges_are_unsorted,the_edges.begin(),the_edges.end(),nv); //line 24