Error with a function pointer as a map parameter? C++ - c++

I seem to be getting several errors with
map<string,function<XMLSerializable*()>> mapConstructor;
Notably,
la5.cpp: In function ‘int main(int, char**)’:
la5.cpp:21:13: error: ‘function’ was not declared in this scope
la5.cpp:21:43: error: ‘mapConstructor’ was not declared in this scope
la5.cpp:21:43: error: template argument 2 is invalid
la5.cpp:21:43: error: template argument 4 is invalid
la5.cpp:25:58: warning: lambda expressions only available with -std=c++0x or - std=gnu++0x [enabled by default]
la5.cpp:33:26: error: expected primary-expression before ‘*’ token
la5.cpp:33:28: error: expected primary-expression before ‘)’ token
la5.cpp:33:31: error: ‘pFunc’ was not declared in this scope
make: *** [la5.o] Error 1
Unfortunately, I can't seem to find what I've done wrong, as it seems to deal with that map declaration which was given to the class by my instructor. Below is my .cpp
#include <iostream>
#include <map>
#include <string>
#include <functional>
#include "Armor.h"
#include "Weapon.h"
#include "Item.h"
#include "Creature.h"
using namespace std;
XMLSerializable * constructItem()
{
return new Item;
}
int main(int argc, char * argv[])
{
map<string,function<XMLSerializable*()>> mapConstructor;
mapConstructor["Item"] = constructItem;
mapConstructor["Creature"] = []() {return new Creature; };
cout << "Input the class name, then we'll try to construct it." << endl;
string sLookup = " ";
cin >> sLookup;
function<XMLSerializable*()> pFunc = mapConstructor[sLookup];
if(pFunc() == NULL)
{
cout << "Sorry, the object couldn't be constructed." << endl;
}
else
{
cout << pFunc() << " a non NULL value was returned!" << endl;
}
return 0;
}
Any suggestions? I'm unfamiliar with maps, but I believe this should work, right?
Coding in pico, compiling with a makefile using g++.

It looks like you just are forgetting to add -std=c++11 or -std=c++0x to your compiler flags to enable C++11.
-std=c++0x is deprecated, but on older versions of g++, -std=c++11 is not available.

Related

std::function of a value templated method compiles with clang and g++ but not with msvc

The following code compiles with clang v5.0.0 and g++ v8.1.0 but fails with visual studio (2013 and 2017):
#include <string>
#include <functional>
#include <iostream>
template <const char* name>
std::string fct() {
return name;
}
const char toto[] = "toto";
std::function<std::string()> fctptr = fct<toto>;
int main(){
std::cout << fctptr() << std::endl;
}
The error is the following:
main.cpp(11): error C2440: 'initializing' : cannot convert from 'std::string (__cdecl *)(void)' to 'std::function<std::string (void)>'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
I tried to replace the std::function with a typedef to a function pointer, as such:
typedef std::string(*Fctptr)();
Fctptr fctptr = fct<toto>;
However, I got the same error.
Is it a bug with msvc compiler, or is the above code not standard compliant.
FWIW, the following failed to compile using g++ 6.4.0 (g++ -std=c++11).
#include <string>
#include <functional>
#include <iostream>
template <const char* name>
std::string fct() {
return name;
}
const char toto[] = "toto";
std::function<std::string()> fctptr = fct<toto>;
int main(){
std::cout << fctptr() << std::endl;
}
Here's the error message:
socc.cc:11:43: error: the value of ‘toto’ is not usable in a constant expression
std::function<std::string()> fctptr = fct<toto>;
^~~~
socc.cc:10:12: note: ‘toto’ was not declared ‘constexpr’
const char toto[] = "toto";
Changing the definition of toto to
constexpr char toto[] = "toto";
resolved the problem.

Overloading prefix and postfix increment in class timer. Problems while debugging

I need to overload increment for class Timer. Members of my class are minutes and seconds.
#include <iostream>
#include <conio.h>
using namespace std;
class Timer
{
private:
int minutes;
int seconds;
public:
Time(){
minutes = 0;
seconds = 0;
}
Time(int m, int s){
minutes = m;
seconds = s;
}
void displayTime()
{
cout << "M: " << hours << " S:" << minutes <<endl;
}
Time operator++ ()
{
++seconds;
if(seconds >= 60)
{
++minutes;
seconds -= 60;
}
return Time(minutes, seconds);
}
Time operator++( int )
{
Time T(minutes, seconds);
++seconds;
if(seconds >= 60)
{
++minutes;
seconds -= 60;
}
return T;
}
};
int main()
{
Time T1(18, 23), T2(19,12);
++T1;
T1.displayTime();
++T1;
T1.displayTime();
T2++;
T2.displayTime();
T2++;
T2.displayTime();
_getch()
}
When I debug, it says
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
main.cpp:13: error: ISO C++ forbids declaration of `Time' with no type
main.cpp:18: error: ISO C++ forbids declaration of `Time' with no type
main.cpp:29: error: ISO C++ forbids declaration of `Time' with no type
main.cpp:29: error: expected `;' before "operator"
main.cpp:40: error: expected `;' before "Time"
main.cpp:40: error: ISO C++ forbids declaration of `Time' with no type
main.cpp:40: error: expected `;' before "operator"
main.cpp:54: error: expected `;' before '}' token
main.cpp: In member function `void Timer::displayTime()':
main.cpp:26: error: `hours' undeclared (first use this function)
main.cpp:26: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp: At global scope:
main.cpp:56: error: new types may not be defined in a return type
main.cpp:56: error: extraneous `int' ignored
main.cpp:56: error: `main' must return `int'
main.cpp: In function `int main(...)':
main.cpp:57: error: `Time' undeclared (first use this function)
main.cpp:57: error: expected `;' before "T1"
main.cpp:59: error: `T1' undeclared (first use this function)
main.cpp:64: error: `T2' undeclared (first use this function)
main.cpp:69: error: expected `;' before '}' token
make.exe: *** [main.o] Error 1
Execution terminated
Object should be of type Timer not Time.
try to match class name and constructor name.
Hour member is not defined in displayTime method.
void displayTime()
{
cout << "M: " << minutes << " S:" << seconds <<endl;
}
Please refer following code
http://ideone.com/m50w2r

auto keyword is not deducing type of string

I've this simple code:
#include <string>
#include <iostream>
using namespace std;
int main() {
string s1 = "rohit";
auto len = s1.size();
cout << len;
}
When I compile this code on Ubuntu 12.04, it shows following error:
test.cc: In function ‘int main()’:
test.cc:8:10: error: ‘len’ does not name a type
auto len = s1.size();
^
test.cc:10:13: error: ‘len’ was not declared in this scope
cout << len;
^
I've got g++ 4.8.1. Is there some changes with the usage of auto keyword in g++ 4.8.1?
The error: ‘len’ does not name a type leads me to believe that you didn't compile in C++11 mode and that it's using the old, C++98 meaning of the keyword auto.

CppCMS URL Dispatching and Mapping fails. Incomplete code?

From http://cppcms.com/wikipp/en/page/cppcms_1x_tut_url_mapping
After rewriting, hello.cpp code is below:
#include <cppcms/application.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <cppcms/url_dispatcher.h>
#include <cppcms/url_mapper.h>
#include <cppcms/applications_pool.h>
#include <iostream>
#include <stdlib.h>
hello(cppcms::service &srv) :
cppcms::application(srv)
{
dispatcher().assign("/number/(\\d+)",&hello::number,this,1);
mapper().assign("number","/number/{1}");
dispatcher().assign("/smile",&hello::smile,this);
mapper().assign("smile","/smile");
dispatcher().assign("",&hello::welcome,this);
mapper().assign("");
mapper().root("/hello");
}
void number(std::string num)
{
int no = atoi(num.c_str());
response().out() << "The number is " << no << "<br/>\n";
response().out() << "<a href='" << url("/") << "'>Go back</a>";
}
void smile()
{
response().out() << ":-) <br/>\n";
response().out() << "<a href='" << url("/") << "'>Go back</a>";
}
void welcome()
{
response().out() <<
"<h1> Wellcome To Page with links </h1>\n"
"<a href='" << url("/number",1) << "'>1</a><br>\n"
"<a href='" << url("/number",15) << "'>15</a><br>\n"
"<a href='" << url("/smile") << "' >:-)</a><br>\n";
}
But now, it throw an error on compiling:
hello.cpp:10:27: error: ISO C++ forbids declaration of ‘hello’ with no type [-fpermissive]
hello.cpp: In function ‘int hello(cppcms::service&)’:
hello.cpp:11:5: error: only constructors take member initializers
hello.cpp:13:16: error: ‘dispatcher’ was not declared in this scope
hello.cpp:13:43: error: ‘hello’ is not a class or namespace
hello.cpp:13:57: error: invalid use of ‘this’ in non-member function
hello.cpp:14:12: error: ‘mapper’ was not declared in this scope
hello.cpp:16:35: error: ‘hello’ is not a class or namespace
hello.cpp:16:48: error: invalid use of ‘this’ in non-member function
hello.cpp:19:29: error: ‘hello’ is not a class or namespace
hello.cpp:19:44: error: invalid use of ‘this’ in non-member function
hello.cpp: In function ‘void number(std::string)’:
hello.cpp:29:14: error: ‘response’ was not declared in this scope
hello.cpp:29:14: note: suggested alternative:
/usr/local/include/cppcms/http_response.h:31:19: note: ‘cppcms::http::response’
hello.cpp:30:47: error: ‘url’ was not declared in this scope
hello.cpp: In function ‘void smile()’:
hello.cpp:35:14: error: ‘response’ was not declared in this scope
hello.cpp:35:14: note: suggested alternative:
/usr/local/include/cppcms/http_response.h:31:19: note: ‘cppcms::http::response’
hello.cpp:36:47: error: ‘url’ was not declared in this scope
hello.cpp: In function ‘void welcome()’:
hello.cpp:41:14: error: ‘response’ was not declared in this scope
hello.cpp:41:14: note: suggested alternative:
/usr/local/include/cppcms/http_response.h:31:19: note: ‘cppcms::http::response’
hello.cpp:43:39: error: ‘url’ was not declared in this scope
I think the new hello.cpp codes is incomplete (and no idea the complete code), but because it said "rewrite", I just removed the old code and create new one.
I've tried combine with old Hello World but no luck.
In the linked-to example the constructor is defined inside the class (in the header). If you define it in a .cpp file, you will have to add the class name to the constructor and member functions.
hello::hello(cppcms::service &srv) :
cppcms::application(srv)
{
...
void hello::number(std::string num)
{
Otherwise the compiler believes that you are adding some unrelated free functions.

decltype undeclared in mingw g++ 4.7.2

For some reason or other when trying to compile the following code in G++ on mingw
#include <iostream>
#include <string>
#include <cctype>
int main( int argc, char **argv )
{
std::string s( "Hello, World!" );
decltype( s.size( ) ) punct_cnt = 0;
for ( auto c : s )
{
if ( ispunct( c ) )
++punct_cnt;
}
std::cout << punct_cnt << " punctuation characters in " << s << std::endl;
return 0;
}
I get the following error
test.cpp: In function 'int main(int, char**)':
test.cpp:9:23: error: 'decltype' was not declared in this scope
test.cpp:9:25: error: expected ';' before 'punct_cnt'
test.cpp:11:13: error: 'c' does not name a type
test.cpp:17:2: error: expected ';' before 'std'
test.cpp:17:15: error: 'punct_cnt' was not declared in this scope
test.cpp:19:2: error: expected primary-expression before 'return'
test.cpp:19:2: error: expected ')' before 'return'
I have checked and the version of the g++ compiler is 4.7.2, anyone got any ideas how I can resolve other than changing decltype to std::string::size_type?
decltype is a c++11 feature. You need to call gcc this way
g++ -std=c++11