queue from the stl - c++

I am trying to get the following code to compile using g++ 4.2.1 and am receiving the following errors
CODE:
#include <iostream>
#include <queue>
using namespace std;
int main (int argc, char * const argv[])
{
queue<int> myqueue();
for(int i = 0; i < 10; i++)
myqueue.push(i);
cout << myqueue.size();
return 0;
}
ERRORS:
main.cpp: In function ‘int main(int, char* const*)’:
main.cpp:10: error: request for member ‘push’ in ‘myqueue’, which is of non-class type ‘std::queue<int, std::deque<int, std::allocator<int> > > ()()’
main.cpp:12: error: request for member ‘size’ in ‘myqueue’, which is of non-class type ‘std::queue<int, std::deque<int, std::allocator<int> > > ()()’
Any ideas as to why? I tried in Eclipse, X-Code and through the terminal.

C++ FAQ Lite § 10.2
Is there any difference between List x; and List x();?
A big difference!
Suppose that List is the name of some class. Then function f() declares a local List object called x:
void f()
{
List x; // Local object named x (of class List)
...
}
But function g() declares a function called x() that returns a List:
void g()
{
List x(); // Function named x (that returns a List)
...
}
Replace queue<int> myqueue(); by queue<int> myqueue; and you'll be fine.

Related

use of std::less in std::map does not compile

I am unable to understand why does following function not compile
#include <iostream>
#include <map>
int main(){
std::map<int, int, std::less<int>> myMap(std::less<int>());
myMap[2] = 2;
std::cout << myMap[2] << std::endl;
return 0;
}
The error message is as follows -
std_less_check.cpp: In function ‘int main()’:
std_less_check.cpp:6:10: warning: pointer to a function used in arithmetic [-Wpointer-arith]
myMap[2] = 2;
^
std_less_check.cpp:6:14: error: assignment of read-only location ‘*(myMap + 2)’
myMap[2] = 2;
^
std_less_check.cpp:6:14: error: cannot convert ‘int’ to ‘std::map<int, int, std::less<int> >(std::less<int> (*)())’ in assignment
std_less_check.cpp:7:23: warning: pointer to a function used in arithmetic [-Wpointer-arith]
std::cout << myMap[2] << std::endl;
while following compiles successfully
#include <iostream>
#include <map>
int main(){
std::map<int, int, std::less<int>> myMap(std::less<int>{});
myMap[2] = 2;
std::cout << myMap[2] << std::endl;
return 0;
}
Could someone please help me with this?
In the first program, you have a vexing parse. If the compiler can parse a declaration as either a variable or a function, it will choose to parse it as a function.
myMap can be parsed as a function declaration.
It returns a std::map<int, int, std::less<int>>.
It takes an argument of type std::less<int>(), which is itself a function type that returns a std::less<int> and takes no arguments. Note that you can't actually have a function type as an argument; the type is actually a pointer to a function that takes no arguments and returns a std::less<int>.
In the second program, replacing () with {} resolves the ambiguity. Now myMap can no longer be a function declaration, and so it instead declares a variable of type std::map<int, int, std::less<int>>.

explicit conversion to std::function

I'm trying to define explicit conversion from some class to std::function like this:
#include <functional>
class ExpInt { private:
const int value;
public:
ExpInt(const int v):value(v){}
explicit operator std::function<int (void)> ()
{
return [=](void){ return value; };
}
};
int main(int argc, char **argv)
{
auto e = new ExpInt(44);
auto f = static_cast<std::function<int (void)>>(e);
return 0;
}
But when I compile it I get the following error:
$ g++ main.cpp -o main
main.cpp: In function ‘int main(int, char**)’:
main.cpp:16:51: error: no matching function for call to ‘std::function<int()>::function(ExpInt*&)’
auto f = static_cast<std::function<int (void)>>(e);
^
The compiler tells you what's wrong:
error: no matching function for call to ‘std::function<int()>::function(ExpInt*&)’
auto f = static_cast<std::function<int (void)>>(e);
^
A pointer to ExpInt is not convertible to std::function<int (void)>. ExpInt would be convertible, so if you simply indirect through the pointer, that would work:
auto f = static_cast<std::function<int (void)>>(*e);
P.S. You leak the dynamic allocation. Avoid using owning bare pointers.

cannot convert 'int (B::*)(std::string)' to 'int (*)(std::string) ' in assignment pt2function=&B::generate_callback;

I am new to c++, .I am trying to create a pgm that contains 2 classes ,out of which one class has a member function that would generate a callback function in another class though a function pointer, but i keep getting the following error.
#include <iostream>
#include <string>
using namespace std;
class B
{
private: std::string str1;
public: int generate_callback(std::string str1);
};
int B::generate_callback(std::string str1)
{
if ((str1=="Generate")||(str1=="generate"))
{
Cout<<"Callback generated ";
}
return 0;
}
class A : public B
{
public:
void count(int a,int b);
private: int a,b;
};
void A::count(int a, int b)
{
for ( a=1;a<b;a++){
if(a==50)
{
cout<<"Generating callback ";
goto exit;
}
exit: ;
}
}
int (*pt2function)(string)=NULL;
int main()
{
B obj1;
A obj2;
string str;
cout<<"To generate callback at int i=50 please enter 'generate'";
cin>>str;
obj2.count(1,100);
pt2function=&B::generate_callback;
(obj1.*pt2function)(str);
return 0;
}
The errors :
main.cpp:57: error: cannot convert 'int (B::*)(std::string) {aka int (B::*)(std::basic_string<char>)}' to 'int (*)(std::string) {aka int (*)(std::basic_string<char>)}' in assignment
pt2function=&B::generate_callback;
/home/adt/practice/N_practise/n_pract_2/pract2/main.cpp:58: error: 'pt2function' cannot be used as a member pointer, since it is of type 'int (*)(std::string) {aka int (*)(std::basic_string<char>)}'
(obj1.*pt2function)(str);
^
^
The variable pt2function is a pointer to a non-member function. Such a pointer is not compatible with a pointer to a member-function. Which is what the compiler tells you with the first error: A int (*)(string) is not compatible with a int (B::*)(string).
You need to define pt2function as a pointer to a B member function:
int (B::*pt2function)(string)=NULL;
Now you can initialize or assign a matching member function of B to the variable pt2function.
This also solves the second errors, which basically says that in your current code the variable pt2function is not a pointer to a member function, and therefore can not be used as such.
Pointers to functions and pointers to member functions are really different beasts.
You have mainly two options to get it working in your code:
Change this line:
int (*pt2function)(string)=NULL;
To this:
int (B::*pt2function)(string)=NULL;
That is defining pt2function as a pointer to a member function of B that gets a string and returns an int.
Declare the generate_callback as a static method and invoke it as pt2function(str); in your main function.
In fact, a static member function can be assigned to a pointer to function like the one you have already in use.

Struct in a list doesn't work

I have this code:
hpp:
#include <list>
using namespace std;
class funcionario
{
public:
struct Dia {
int d;
int h;
int id;
int tipo;
};
funcionario ();
void eliminar(int dia, int hora);
private:
list<Dia> agenda;
};
cpp:
#include "myClass.hpp"
funcionario::funcionario(){
agenda = list<Dia> ();
}
void funcionario::eliminar(int dia, int hora) {
list<funcionario::Dia>::iterator it;
it = agenda.begin();
while(it != agenda.end() && (*it).d <= dia) {
if((*it).d == dia && (*it).h == hora) {
agenda.erase(it);
return;
}
++it;
}
}
I get this compiling error:
Funcionario.cpp: In constructor ‘funcionario::funcionario()’:
Funcionario.cpp:5: error: cannot convert ‘std::list<funcionario::Dia, std::allocator<funcionario::Dia> >’ to ‘int’ in assignment
Funcionario.cpp: In member function ‘void funcionario::eliminar(int, int)’:
Funcionario.cpp:9: error: request for member ‘begin’ in ‘((funcionario*)this)->funcionario::agenda’, which is of non-class type ‘int’
Funcionario.cpp:10: error: request for member ‘begin’ in ‘((funcionario*)this)->funcionario::agenda’, which is of non-class type ‘int’
Funcionario.cpp:11: error: request for member ‘end’ in ‘((funcionario*)this)->funcionario::agenda’, which is of non-class type ‘int’
I don't know what I'm doing wrong.
Not sure what you're trying to achieve, but the code just needs to be fleshed out a bit with complete function definitions. I got this to compile:
#include <list>
class myClass
{
public:
myClass();
struct myStruct {
int myInfo;
};
void something();
void doSomething(myStruct & ms);
private:
std::list<myStruct> myList;
};
myClass::myClass(){
myList = list<myStruct> ();
}
void myClass::something() {
std::list<myStruct>::iterator it;
it = myList.begin();
while(it != myList.end()) {
doSomething(*it);
++it;
}
}
Incidentally (or maybe directly relevant, not sure) - the copy-initialization of myList in myClass() is unnecessary, as others have stated. The list<> default constructor will do the right thing, and more efficiently.
This seems to be working on my computer, so may it be a compiler problem? Try with another compiler and tell us if it worked
The initialization you're looking for is analogous to Initializing map and set class member variables to empty in C++? - but actually you'll get an empty list automatically (i.e. by the std::list default constructor).
--Edited to reflect your posting of the original code--
H is not declared anywhere.
and is not a valid C++ keyword or token. Use &&.
Use the local header include form of : #include "myClass.hpp"

object as function argument

I have written a simple program.
I am getting this error:
time.cpp: In function ‘int main()’:
time.cpp:22:9: error: expected ‘;’ before ‘a’
time.cpp:23:4: error: ‘a’ was not declared in this scope
time.cpp:24:4: error: ‘b’ was not declared in this scope
time.cpp:25:4: error: ‘c’ was not declared in this scope
This is my code:
#include<iostream>
using namespace std;
class time
{
int hour;
int min;
public:
void gettime(int h,int m)
{
hour=h;
min=m;
}
void puttime()
{
cout<<hour<<endl<<min;
}
void sum(time x,time y)
{
min=x.min+y.min;
hour=min/60;
min=min%60;
hour=hour+x.hour+y.hour;
}
};
int main()
{
time a,b,c;
a.gettime(2,45);
b.gettime(3,35);
c.sum(a,b);
a.puttime();
b.putime();
c.puttime();
return 0;
}
Remember that there is a standard function named time.
This is the one main reason you should refrain from using namespace std;.
b.putime() must be b.puttime() here. Otherwise this code compiled